pagoda

package module
v0.0.0-...-190cdb3 Latest Latest
Warning

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

Go to latest
Published: Apr 5, 2016 License: MIT Imports: 7 Imported by: 0

README

Pagoda

GoDoc Go Report Card Coverage

A simple template manager for go

Features

  • Automatic template retrieval
  • Simplified template execution
  • Template caching
  • Template change detection

Examples

###Executing a template named home/index.html from your template directory

templateManager, _ := pagoda.NewTemplateManager(myTemplateDirectory)

http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
    templateManager.Execute("home/index", w, nil)        
})

###Executing a template with sub-templates

templateManager, _ := pagoda.NewTemplateManager(myTemplateDirectory)

http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
    templateManager.Execute("index", w, []string{"Pagani Huayra", "Ferrari LaFerrari", "McLaren P1"})
})

index.html

Note: when loading sub-templates, use pagoda_template instead of template

<html>
    <body>
        <h1>My Garage</h1>
        <ul>
        {{ range . }}
            {{ pagoda_template "car" . }}
        {{ end }}
        </ul>
    </body>
</html>

###Layout pages Pagoda's layout feature allows you to use a site-wide template to maintain a consistant layout across all pages

templateManager, _ := pagoda.NewTemplateManager(myTemplateDirectory)

layoutTemplateManager := templateManager.UseLayoutTemplate("layout")

http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
    layoutTemplateManager.Execute("home", w, []string{"Pagani Huayra", "Ferrari LaFerrari", "McLaren P1"})
})

layout.html

Use {{pagoda_layout_placeholder .}} to set the location you want your template to render in the layout template

<html>
    <head>
        <title></title>
    <head>
    <body>
        {{ pagoda_layout_placeholder . }}
    </body>
</html>

home.html

<h1>My Garage</h1>
<ul>
{{ range . }}
    <li>{{.}}</li>
{{ end }}
</ul>

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type LayoutTemplateManager

type LayoutTemplateManager struct {
	*TemplateManager
	// contains filtered or unexported fields
}

LayoutTemplateManager loads and executes templates with a layout page

func (*LayoutTemplateManager) Execute

func (layoutTemplateManager *LayoutTemplateManager) Execute(templateName string, writer io.Writer, data interface{}) (err error)

Execute a template named templateName

func (*LayoutTemplateManager) GetTemplate

func (layoutTemplateManager *LayoutTemplateManager) GetTemplate(templateName string) (tpl *template.Template, err error)

GetTemplate gets a template from the templateFolder based on the templateName

type TemplateManager

type TemplateManager struct {
	// contains filtered or unexported fields
}

TemplateManager automatically loads, retrieves and executes templates

func NewTemplateManager

func NewTemplateManager(templateFolder string) (templateManager *TemplateManager, err error)

NewTemplateManager creates a new TemplateManager based on templateFolder

func (*TemplateManager) Close

func (templateManager *TemplateManager) Close() error

Close cleans up resources

func (*TemplateManager) Execute

func (templateManager *TemplateManager) Execute(templateName string, writer io.Writer, data interface{}) (err error)

Execute a template named templateName

func (*TemplateManager) Funcs

func (templateManager *TemplateManager) Funcs(funcs template.FuncMap)

Funcs adds template functions

func (*TemplateManager) GetTemplate

func (templateManager *TemplateManager) GetTemplate(templateName string) (tpl *template.Template, err error)

GetTemplate gets a template from the templateFolder based on the templateName

func (*TemplateManager) UseLayoutTemplate

func (templateManager *TemplateManager) UseLayoutTemplate(layoutTemplateName string) *LayoutTemplateManager

UseLayoutTemplate allows templates to be wrapped with a layout template

Jump to

Keyboard shortcuts

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