html

package
v0.35.0 Latest Latest
Warning

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

Go to latest
Published: Dec 7, 2020 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package html 提供输出 HTML 内容的 content.MarshalFunc 函数

tpl := template.ParseFiles(...)
mgr := html.New(tpl)
content.AddMarshal("text/html", mgr.Marshal)

func handle(ctx *web.Context) {
    ctx.Render(200, html.Tpl("index", map[string]interface{}{...}), nil)
}

Index

Constants

View Source
const Mimetype = "text/html"

Mimetype HTML 的 mimetype 值

Variables

This section is empty.

Functions

This section is empty.

Types

type HTML

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

HTML 模板管理

func New

func New(tpl *template.Template) *HTML

New 声明 HTML 变量

tpl 可以为空,通过之后的 SetTemplate 再次指定

func (*HTML) Marshal

func (html *HTML) Marshal(v interface{}) ([]byte, error)

Marshal 针对 HTML 内容的 content.MarshalFunc 实现

func (*HTML) SetTemplate

func (html *HTML) SetTemplate(tpl *template.Template)

SetTemplate 修改模板内容

type Template

type Template struct {
	Name string // 模块名称
	Data interface{}
}

Template 传递给 content.MarshalFunc 的参数

因为 content.MarshalFunc 限定了只能有一个参数, 而模板解析,除了传递的值,最起码还要一个模板名称, 所以采用 Template 作了一个简单的包装。

func Tpl

func Tpl(name string, data interface{}) *Template

Tpl 声明一个 *Template 变量

其中 name 表示需要引用的模板名称, 而 data 则是传递给该模板的所有变量。

Jump to

Keyboard shortcuts

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