tplex

package
v0.0.0-...-800783e Latest Latest
Warning

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

Go to latest
Published: Feb 20, 2016 License: Apache-2.0 Imports: 16 Imported by: 0

README

#模板引擎

##特点

  1. 支持继承
  2. 支持包含子模板

##模板继承 用于模板继承的标签有: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标签内使用。

##包含子模板

例如,有以下两个模板:
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>

    Copyright 2016 Wenhui Shen <www.webx.top>

    Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Index

Constants

This section is empty.

Variables

View Source
var Debug = false

Functions

func Del

func Del(key string)

func FixDirSeparator

func FixDirSeparator(dir string) string

func Reg

func Reg(key string, val func(string) TemplateEx)

Types

type CcRel

type CcRel struct {
	Rel  map[string]uint8
	Tpl  [2]*htmlTpl.Template //0是独立模板;1是子模板
	Sub  string
	Self string
}

type TemplateEx

type TemplateEx interface {
	Init(...bool)
	SetFuncMapFn(func() map[string]interface{})
	Render(io.Writer, string, interface{}, map[string]interface{}) error
	Fetch(string, interface{}, map[string]interface{}) string
	RawContent(string) ([]byte, error)
	MonitorEvent(func(string))
	ClearCache()
	Close()
}

func Create

func Create(key string, tmplDir string) TemplateEx

func New

func New(templateDir string) TemplateEx

type TemplateMgr

type TemplateMgr struct {
	Caches       map[string][]byte
	Mutex        *sync.Mutex
	RootDir      string
	NewRoorDir   string
	Ignores      map[string]bool
	CachedAllows map[string]bool
	IsReload     bool
	Logger       *log.Logger
	Preprocessor func([]byte) []byte

	TimerCallback func() bool

	OnChangeCallback func(string, string, string) //参数为:目标名称,类型(file/dir),事件名(create/delete/modify/rename)
	// contains filtered or unexported fields
}

func (*TemplateMgr) AllowCached

func (self *TemplateMgr) AllowCached(name string) bool

func (*TemplateMgr) CacheAll

func (self *TemplateMgr) CacheAll(rootDir string) error

func (*TemplateMgr) CacheDelete

func (self *TemplateMgr) CacheDelete(tmpl string)

func (*TemplateMgr) CacheTemplate

func (self *TemplateMgr) CacheTemplate(tmpl string, content []byte)

func (*TemplateMgr) ClearCache

func (self *TemplateMgr) ClearCache()

func (*TemplateMgr) Close

func (self *TemplateMgr) Close()

func (*TemplateMgr) CloseMoniter

func (self *TemplateMgr) CloseMoniter()

func (*TemplateMgr) GetTemplate

func (self *TemplateMgr) GetTemplate(tmpl string) ([]byte, error)

func (*TemplateMgr) Init

func (self *TemplateMgr) Init(logger *log.Logger, rootDir string, reload bool, allows ...string) error

func (*TemplateMgr) Moniter

func (self *TemplateMgr) Moniter(rootDir string) error

func (*TemplateMgr) OnChange

func (self *TemplateMgr) OnChange(name, typ, event string)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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