standard

package
v1.7.5 Latest Latest
Warning

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

Go to latest
Published: Jun 10, 2024 License: Apache-2.0 Imports: 18 Imported by: 2

README

模板引擎

特点

  1. 支持继承

  2. 支持包含子模板

  3. 支持golang原生模板语法(详细用法可参阅golang模板语法简明教程

  4. 自动注册模板函数 hasBlock(blocks ...string) boolhasAnyBlock(blocks ...string) bool

    • hasBlock(blocks ...string) bool - 是否在扩展模板中设置了指定名称的Block
    • hasAnyBlock(blocks ...string) bool - 是否在扩展模板中设置了指定名称中的任意一个Block
  5. 支持多重继承

模板继承

用于模板继承的标签有:Block、Extend、Super 例如,有以下两个模板:

  1. layout.html:
{{Block "title"}}-- powered by webx{{/Block}}
{{Block "body"}}内容{{/Block}}
  1. index.html:
{{Extend "layout"}}
{{Block "title"}}首页 {{Super}}{{/Block}}
{{Block "body"}}这是一个演示{{/Block}}

渲染模板index.html将会输出:

首页 -- powered by webx
这是一个演示

注意:

  1. Super标签只能在扩展模板(含Extend标签的模板)的Block标签内使用。
  2. 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

Constants

This section is empty.

Variables

View Source
var Debug = false

Functions

func New

func New(templateDir string, args ...logger.Logger) driver.Driver

func NewTplInfo

func NewTplInfo(t *htmlTpl.Template) *tplInfo

Types

type CacheData added in v1.6.0

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

func NewCache added in v1.6.0

func NewCache() *CacheData

func (*CacheData) GetOk added in v1.6.0

func (c *CacheData) GetOk(cacheKey string) (*CcRel, bool)

func (*CacheData) Remove added in v1.6.0

func (c *CacheData) Remove(cacheKey string)

func (*CacheData) Reset added in v1.6.0

func (c *CacheData) Reset()

func (*CacheData) Set added in v1.6.0

func (c *CacheData) Set(cacheKey string, r *CcRel)

type CcRel

type CcRel struct {
	Rel map[string]uint8
	Tpl [2]*tplInfo //0是独立模板;1是子模板
	// contains filtered or unexported fields
}

func NewRel added in v1.6.0

func NewRel(cachedKey string) *CcRel

func (*CcRel) GetOk added in v1.6.0

func (c *CcRel) GetOk(cacheKey string) (uint8, bool)

func (*CcRel) Remove added in v1.6.0

func (c *CcRel) Remove(cacheKey string)

func (*CcRel) Reset added in v1.6.0

func (c *CcRel) Reset()

func (*CcRel) Set added in v1.6.0

func (c *CcRel) Set(cacheKey string, r uint8)

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) Close

func (a *Standard) Close()

func (*Standard) ContainsFunctionResult added in v1.6.0

func (a *Standard) ContainsFunctionResult(c echo.Context, tmplOriginalName string, content string, clips map[string]string) string

func (*Standard) ContainsSubTpl

func (a *Standard) ContainsSubTpl(c echo.Context, content string, subcs map[string]string) string

func (*Standard) Debug

func (a *Standard) Debug() bool

func (*Standard) Fetch

func (a *Standard) Fetch(tmplName string, data interface{}, c echo.Context) string

func (*Standard) Init

func (a *Standard) Init()

func (*Standard) InitRegexp

func (a *Standard) InitRegexp()

func (*Standard) Logger

func (a *Standard) Logger() logger.Logger

func (*Standard) Manager added in v1.4.3

func (a *Standard) Manager() driver.Manager

func (*Standard) MonitorEvent

func (a *Standard) MonitorEvent(fn func(string))

func (*Standard) ParseBlock

func (a *Standard) ParseBlock(c echo.Context, content string, subcs map[string]string, extcs map[string]string)

func (*Standard) ParseExtend

func (a *Standard) ParseExtend(c echo.Context, content string, extcs map[string]string, passObject string, subcs map[string]string) (string, [][]string)

func (*Standard) RawContent

func (a *Standard) RawContent(tmpl string) (b []byte, e error)

func (*Standard) Render

func (a *Standard) Render(w io.Writer, tmplName string, values interface{}, c echo.Context) error

Render HTML

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 (a *Standard) SetContentProcessor(fn func([]byte) []byte)

func (*Standard) SetDebug added in v1.2.0

func (a *Standard) SetDebug(on bool)

func (*Standard) SetFuncMap added in v1.1.0

func (a *Standard) SetFuncMap(fn func() map[string]interface{})

func (*Standard) SetLogger

func (a *Standard) SetLogger(l logger.Logger)

func (*Standard) SetManager added in v1.1.0

func (a *Standard) SetManager(mgr driver.Manager)

func (*Standard) SetTmplPathFixer added in v1.4.3

func (a *Standard) SetTmplPathFixer(fn func(echo.Context, string) string)

func (*Standard) Tag

func (a *Standard) Tag(content string) string

func (*Standard) TmplDir

func (a *Standard) TmplDir() string

func (*Standard) TmplPath added in v1.6.0

func (a *Standard) TmplPath(c echo.Context, p string) string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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