Documentation ¶
Overview ¶
Package view is a simple view renderer based on the `html/template`, but much simpler to use.
Source code and other details for the project are available at GitHub:
https://github.com/gookit/view
Usage please see example.
Example ¶
// equals to call: view.NewRenderer() + r.MustInitialize() r := NewInitialized(func(r *Renderer) { // setting default layout r.Layout = "layout" // equals to "layout.tpl" // templates dir. will auto load on init. r.ViewsDir = "testdata" }) // fmt.Println(r.TemplateNames(true)) bf := new(bytes.Buffer) // render template string _ = r.String(bf, `hello {{.}}`, "tom") fmt.Print(bf.String()) // hello tom // render template without layout _ = r.Partial(bf, "home", "tom") bf.Reset() // render with default layout _ = r.Render(bf, "home", "tom") bf.Reset() // render with custom layout _ = r.Render(bf, "home", "tom", "site/layout") bf.Reset() // load named template string r.LoadString("my-page", "welcome {{.}}") // now, you can use "my-page" as an template name _ = r.Partial(bf, "my-page", "tom") // welcome tom bf.Reset() // more ways for load templates r.LoadByGlob("some/path/*", "some/path") r.LoadFiles("path/file1.tpl", "path/file2.tpl")
Output:
Index ¶
- Constants
- func AddFunc(name string, fn interface{})
- func AddFuncMap(fm template.FuncMap)
- func Execute(w io.Writer, tplName string, v interface{}) error
- func Initialize(fns ...func(r *Renderer))
- func LoadByGlob(pattern string, baseDirs ...string)
- func LoadFiles(files ...string)
- func LoadString(tplName string, tplString string)
- func LoadStrings(sMap map[string]string)
- func Partial(w io.Writer, tplName string, v interface{}) error
- func Render(w io.Writer, tplName string, v interface{}, layouts ...string) error
- func Revert()
- func String(w io.Writer, tplString string, v interface{}) error
- type M
- type Renderer
- func (r *Renderer) AddFunc(name string, fn interface{})
- func (r *Renderer) AddFuncMap(fm template.FuncMap)
- func (r *Renderer) Execute(w io.Writer, tplName string, v interface{}) error
- func (r *Renderer) Initialize() error
- func (r *Renderer) IsValidExt(ext string) bool
- func (r *Renderer) LoadByGlob(pattern string, baseDirs ...string)
- func (r *Renderer) LoadFiles(files ...string)
- func (r *Renderer) LoadString(tplName string, tplString string)
- func (r *Renderer) LoadStrings(sMap map[string]string)
- func (r *Renderer) LoadedTemplates() []*template.Template
- func (r *Renderer) MustInitialize()
- func (r *Renderer) Partial(w io.Writer, tplName string, v interface{}) error
- func (r *Renderer) Render(w io.Writer, tplName string, v interface{}, layouts ...string) error
- func (r *Renderer) String(w io.Writer, tplString string, v interface{}) error
- func (r *Renderer) Template(name string) *template.Template
- func (r *Renderer) TemplateFiles() map[string]string
- func (r *Renderer) TemplateNames(format ...bool) string
- func (r *Renderer) Templates() *template.Template
- type TplDelims
Examples ¶
Constants ¶
const DefaultExt = ".tpl"
DefaultExt name
Variables ¶
This section is empty.
Functions ¶
func AddFuncMap ¶ added in v1.0.2
AddFuncMap add template func map
func Initialize ¶ added in v1.0.2
func Initialize(fns ...func(r *Renderer))
Initialize the default instance
func LoadByGlob ¶ added in v1.0.2
LoadByGlob load templates by glob pattern.
func LoadFiles ¶ added in v1.0.2
func LoadFiles(files ...string)
LoadFiles load custom template files.
func LoadString ¶ added in v1.0.2
LoadString load named template string.
func LoadStrings ¶ added in v1.0.2
LoadStrings load multi named template strings
Types ¶
type Renderer ¶
type Renderer struct { // Debug setting Debug bool // Layout template name Layout string // Delims define for template Delims TplDelims // ViewsDir the default views directory ViewsDir string // ExtNames allowed template extensions. eg {"tpl", "html"} ExtNames []string // FuncMap func map for template FuncMap template.FuncMap // DisableLayout disable layout. default is False DisableLayout bool // AutoSearchFile // TODO: auto search template file, when not found on compiled templates. default is False AutoSearchFile bool // contains filtered or unexported fields }
Renderer definition
func NewInitialized ¶
NewInitialized create a new and initialized view renderer
func NewRenderer ¶
NewRenderer create a new view renderer
func (*Renderer) AddFuncMap ¶
AddFuncMap add template func map
func (*Renderer) Initialize ¶
Initialize templates in the viewsDir, add do some prepare works. Notice: must call it on after create Renderer
func (*Renderer) IsValidExt ¶
IsValidExt check is valid ext name
func (*Renderer) LoadByGlob ¶
LoadByGlob load templates by glob pattern. Usage:
r.LoadByGlob("views/*") r.LoadByGlob("views/*.tpl") // add ext limit r.LoadByGlob("views/**/*")
func (*Renderer) LoadFiles ¶
LoadFiles load custom template files. Usage:
r.LoadFiles("path/file1.tpl", "path/file2.tpl")
func (*Renderer) LoadString ¶
LoadString load named template string. Usage:
r.LoadString("my-page", "welcome {{.}}") // now, you can use "my-page" as an template name r.Partial(w, "my-page", "tom") // welcome tom
func (*Renderer) LoadStrings ¶
LoadStrings load multi named template strings
func (*Renderer) LoadedTemplates ¶
LoadedTemplates returns loaded template instances, including ROOT itself.
func (*Renderer) MustInitialize ¶
func (r *Renderer) MustInitialize()
MustInitialize compile templates and report error
func (*Renderer) Render ¶
Render a template name/file and write to the Writer. Usage:
renderer := view.NewRenderer() // ... ... // will apply global layout setting renderer.Render(http.ResponseWriter, "user/login", data) // apply custom layout file renderer.Render(http.ResponseWriter, "user/login", data, "custom-layout") // will disable apply layout render renderer.Render(http.ResponseWriter, "user/login", data, "")
func (*Renderer) TemplateFiles ¶
TemplateFiles returns loaded template files
func (*Renderer) TemplateNames ¶
TemplateNames returns loaded template names