plet

package module
v0.0.0-...-7b38a17 Latest Latest
Warning

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

Go to latest
Published: May 1, 2018 License: MIT Imports: 7 Imported by: 0

README

Go Report Card godocs

Plet

Plet is a simple template package for web apps. Plet wraps html/template and provides helper functions.

Terminologies

Layout: layout templates serves as base for content templates.

Content: content templates is where you define the sections of your templates.

Basic Usage

layout template

tmplt/layout/basic/basic.html: layout template. Note that for layout templates, folder name must match the template declaration.

{{ define "basic" }}
<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8">
		<meta name="viewport" content="width=device-width, initial-scale=1">
		{{ template "head" . }}
	</head>
	<body>
		{{ template "content" . }}

		{{ template "foot" . }}
	</body>
</html>
{{ end }}

content templates

tmplt/content/simple/head.html

{{ define "head" }}
<title>Plet | Template Management Package</title>
{{ end }}

tmplt/content/simple/content.html

{{ define "content" }}
<h1>Hello World!</h1>
{{ end }}

tmplt/content/simple/foot.html

{{ define "foot" }}
<p>Hello, I'm a footer!</p>
{{end}}

basic.go:

import (
	"log"
	"os"

	"github.com/steven-ferrer/plet"
)

const (
	contentDir = "tmplt/content/"
	layoutDir  = "tmplt/layout/"
)

//very simple example of using plet package
func main() {
	//new template
	t := plet.New(contentDir+"simple", layoutDir+"basic")

	//initialize template, this will compile the template
	err := t.Init()
	if err != nil {
		log.Fatal(err)
	}

	err = t.Execute(os.Stdout, nil)
	if err != nil {
		log.Fatal(err)
	}
}

More Examples

For more examples please see examples folder.

Issues and Question

If you found a bug or have a question, please feel free to open an issue.

Contributing

Please feel free to contribute by submitting a PR.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	//ErrTemplateNotExist returned when not template is not found
	//in the map of templates
	ErrTemplateNotExist = errors.New("template not found")
)

Functions

This section is empty.

Types

type Template

type Template struct {
	//content template directory
	//define the templates here and use
	//them in the layout template
	ContentDir string
	//layout template directory
	//content templates that are used
	//in the layout template must be defined
	//in the content template
	LayoutDir string
	//file name extension of template files
	Ext string

	//set to true to enable hot reload
	HotReload bool
	// contains filtered or unexported fields
}

Template wraps the html/template to add functionality

func New

func New(contentDir, layoutDir string) Template

New returns a template using the provided layout and content directory

func NewBasic

func NewBasic() Template

NewBasic returns a new empty Template if you use this, you have to manually add the ContentDir (required) and LayoutDir and Ext which are both optional

func (*Template) Execute

func (t *Template) Execute(wr io.Writer, data interface{}) (err error)

Execute wraps Template.ExecuteTemplate

func (*Template) Init

func (t *Template) Init() error

Init performs the initialization contentDir: is the directory of content templates layoutDir: is the directory of layout templates ext: is for file extension (e.g. .html, .txt etc.) .html is used if ext is empty Note: if you even forgot to call Init, it will be called when you call Execute But it is strongly suggested that you initialize it before everything else since so that it can be verified if the template files exists

type Templates

type Templates struct {
	//set to true if you want all the
	//tempaltes contained in here to enable hot reload
	HotReload bool
	// contains filtered or unexported fields
}

Templates is a map of plet.Template

func NewTemplates

func NewTemplates() *Templates

NewTemplates returns a new Templates which actually is just a map of Template

func (*Templates) Add

func (tmplts *Templates) Add(tmplt *Template) error

Add adds a template to the template map if template is previously added, it will be overwritten

func (*Templates) Get

func (tmplts *Templates) Get(name string) (*Template, error)

Get returns the template given a name

func (*Templates) Init

func (tmplts *Templates) Init() (err error)

Init initializes all the template in the map

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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