README
¶
模板引擎
特点
-
支持继承
-
支持包含子模板
-
支持golang原生模板语法(详细用法可参阅golang模板语法简明教程)
-
自动注册模板函数
hasBlock(blocks ...string) bool
和hasAnyBlock(blocks ...string) bool
hasBlock(blocks ...string) bool
- 是否在扩展模板中设置了指定名称的BlockhasAnyBlock(blocks ...string) bool
- 是否在扩展模板中设置了指定名称中的任意一个Block
-
支持多重继承
模板继承
用于模板继承的标签有:Block、Extend、Super 例如,有以下两个模板:
- layout.html:
{{Block "title"}}-- powered by webx{{/Block}}
{{Block "body"}}内容{{/Block}}
- index.html:
{{Extend "layout"}}
{{Block "title"}}首页 {{Super}}{{/Block}}
{{Block "body"}}这是一个演示{{/Block}}
渲染模板index.html将会输出:
首页 -- powered by webx
这是一个演示
注意:
- Super标签只能在扩展模板(含Extend标签的模板)的Block标签内使用。
- Extend标签 必须放在页面内容的起始位置才有效
因为最新增加了对多重继承的支持,所以,现在我们还可以创建一个模板new.html
用来继承上面的index.html
,比如new.html
的内容为:
{{Extend "index"}}
{{Block "body"}}这是一个多重继承的演示{{/Block}}
渲染这个新模板将会输出:
首页 -- powered by webx
这是一个多重继承的演示
也就是说这个新模板具有这样的继承关系:new.html -> index.html -> layout.html (目前限制为最多不超过10级)
包含子模板
例如,有以下两个模板:
footer.html:
www.webx.top
index.html:
前面的一些内容
{{Include "footer"}}
后面的一些内容
渲染模板index.html将会输出:
前面的一些内容
www.webx.top
后面的一些内容
也可以在循环中包含子模板,例如:
{{range .list}}
{{Include "footer"}}
{{end}}
因为本模板引擎缓存了模板对象,所以它并不会多次读取模板内容,在循环体内也能高效的工作。
Include标签也能在Block标签内部使用,例如:
{{Block "body"}}
这是一个演示
{{Include "footer"}}
{{/Block}}
另外,Include标签也支持嵌套。
点此查看完整例子
Documentation
¶
Overview ¶
*
- 模板扩展
- @author swh <swh@admpub.com>
Index ¶
- Variables
- func New(templateDir string, args ...logger.Logger) driver.Driver
- func NewTplInfo(t *htmlTpl.Template) *tplInfo
- type CacheData
- type CcRel
- type Standard
- func (a *Standard) ClearCache()
- func (a *Standard) Close()
- func (a *Standard) ContainsFunctionResult(c echo.Context, tmplOriginalName string, content string, ...) string
- func (a *Standard) ContainsSubTpl(c echo.Context, content string, subcs map[string]string) string
- func (a *Standard) Debug() bool
- func (a *Standard) Fetch(tmplName string, data interface{}, c echo.Context) string
- func (a *Standard) Init()
- func (a *Standard) InitRegexp()
- func (a *Standard) Logger() logger.Logger
- func (a *Standard) Manager() driver.Manager
- func (a *Standard) MonitorEvent(fn func(string))
- func (a *Standard) ParseBlock(c echo.Context, content string, subcs map[string]string, ...)
- func (a *Standard) ParseExtend(c echo.Context, content string, extcs map[string]string, passObject string, ...) (string, [][]string)
- func (a *Standard) RawContent(tmpl string) (b []byte, e error)
- func (a *Standard) Render(w io.Writer, tmplName string, values interface{}, c echo.Context) error
- func (a *Standard) RenderBy(w io.Writer, tmplName string, tmplContent func(string) ([]byte, error), ...) error
- func (a *Standard) SetContentProcessor(fn func([]byte) []byte)
- func (a *Standard) SetDebug(on bool)
- func (a *Standard) SetFuncMap(fn func() map[string]interface{})
- func (a *Standard) SetLogger(l logger.Logger)
- func (a *Standard) SetManager(mgr driver.Manager)
- func (a *Standard) SetTmplPathFixer(fn func(echo.Context, string) string)
- func (a *Standard) Tag(content string) string
- func (a *Standard) TmplDir() string
- func (a *Standard) TmplPath(c echo.Context, p string) string
Constants ¶
This section is empty.
Variables ¶
View Source
var Debug = false
Functions ¶
func NewTplInfo ¶
Types ¶
type CacheData ¶ added in v1.6.0
type CacheData struct {
// contains filtered or unexported fields
}
type CcRel ¶
type Standard ¶
type Standard struct { CachedRelation *CacheData TemplateDir string TemplateMgr driver.Manager DelimLeft string DelimRight string IncludeTag string FunctionTag string ExtendTag string BlockTag string SuperTag string StripTag string Ext string // contains filtered or unexported fields }
func (*Standard) ClearCache ¶
func (a *Standard) ClearCache()
func (*Standard) ContainsFunctionResult ¶ added in v1.6.0
func (*Standard) ContainsSubTpl ¶
func (*Standard) InitRegexp ¶
func (a *Standard) InitRegexp()
func (*Standard) MonitorEvent ¶
func (*Standard) ParseBlock ¶
func (*Standard) ParseExtend ¶
func (*Standard) RenderBy ¶ added in v1.6.0
func (a *Standard) RenderBy(w io.Writer, tmplName string, tmplContent func(string) ([]byte, error), values interface{}, c echo.Context) error
RenderBy render by content
func (*Standard) SetContentProcessor ¶
func (*Standard) SetFuncMap ¶ added in v1.1.0
func (*Standard) SetManager ¶ added in v1.1.0
func (*Standard) SetTmplPathFixer ¶ added in v1.4.3
Click to show internal directories.
Click to hide internal directories.