mold

package module
v0.0.0-...-4b83548 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 7, 2019 License: BSD-3-Clause Imports: 3 Imported by: 0

README

Godocs Go Report Card Release

mold

mold your templated to HTML/ TEXT/ PDF easily.

install
go get github.com/mayur-tolexo/mold
Example 1
//Todo model
type Todo struct {
	Title string
	Done  bool
}

//TodoPageData model
type TodoPageData struct {
	PageTitle string
	Todos     []Todo
}

func main() {

	tmpl := `
	<h1>{{.PageTitle}}<h1>
	<ul>
	    {{range .Todos}}
	        {{if .Done}}
	            <li class="done">{{.Title}}</li>
	        {{else}}
	            <li>{{.Title}}</li>
	        {{end}}
	    {{end}}
	</ul>
	`
	
	mold, _ := mold.NewHTMLTemplate(tmpl)

	data := TodoPageData{
		PageTitle: "My TODO list",
		Todos: []Todo{
			{Title: "Task 1", Done: false},
			{Title: "Task 2", Done: true},
			{Title: "Task 3", Done: true},
			{Title: "Task 4", Done: true},
		},
	}

	if err := mold.Execute(data); err == nil {
		mold.PDF(".", "tmp.pdf")
	} else {
		fmt.Println(err)
	}
}
Example 2

//Invoice details
type Invoice struct {
	InvoiceNo   string
	InvoiceDate string
	Currency    string
	AmountDue   float64
	Items       []ItemDetail
	Total       float64
}

//ItemDetail : Item details
type ItemDetail struct {
	Name     string
	Desc     string
	Amount   float64
	Qty      int
	Total    float64
}

func main() {
	mold, _ := mold.NewHTMLTemplate()
	mold.HTMLPath = "invoice.html"

	item := []ItemDetail{
		{
			Name:     "Front End Consultation",
			Desc:     "Experience Review",
			Amount:   150,
			Qty:      4,
			Total:    600,
		},
	}

	data := Invoice{
		InvoiceNo:   "Invoice",
		InvoiceDate: "January 1, 2019",
		Currency:    "Rs.",
		AmountDue:   600,
		Items:       item,
		Total:       600,
	}

	if err := mold.Execute(data); err == nil {
		mold.PDF(".", "invoice.pdf")
	} else {
		fmt.Println(err)
	}
}
Output

Screenshot 2019-05-20 at 4 44 19 PM

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Mold

type Mold struct {
	HTMLTemplate string //html template string
	HTMLPath     string //html template file path

	*wkhtmltopdf.PDFGenerator
	// contains filtered or unexported fields
}

Mold model

func NewHTMLTemplate

func NewHTMLTemplate(tmpl ...string) (m *Mold, err error)

NewHTMLTemplate will create new return new html template mold

func (*Mold) Bytes

func (m *Mold) Bytes() []byte

Bytes will return bytes from mold buffer

func (*Mold) Execute

func (m *Mold) Execute(data interface{}) (err error)

Execute will execute the template

func (*Mold) PDF

func (m *Mold) PDF(path, name string) (err error)

PDF will create pdf

func (*Mold) String

func (m *Mold) String() string

String will return string from mold buffer

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL