Skip to content

Instantly share code, notes, and snippets.

@zhangskills
Created September 17, 2014 14:43
Show Gist options
  • Save zhangskills/62e79001d1bcf40facf3 to your computer and use it in GitHub Desktop.
Save zhangskills/62e79001d1bcf40facf3 to your computer and use it in GitHub Desktop.
revel相关
@zhangskills
Copy link
Author

模板模拟继承功能:

自定义方法:

revel.TemplateFuncs["block"] = func(blockName, tempName string, data map[string]interface{}) string {
        t, err := revel.MainTemplateLoader.Template(tempName)
        if err != nil {
            log.Errorln(err)
            return ""
        }

        var b bytes.Buffer
        err = t.Render(&b, data)
        if err != nil {
            log.Errorln(err)
            return ""
        }
        data[blockName] = string(b.Bytes())
        return ""
    }

创建模板页(命名为layout.html):

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>{{if .tpl_title}}{{raw .tpl_title}}{{end}}</title>
    {{if .tpl_head}}{{raw .tpl_head}}{{end}}
</head>
<body>
    {{if .tpl_body}}{{raw .tpl_body}}{{end}}
</body>
</html>

页面:

{{block "tpl_body" "引入页.html" .}}
{{template "layout.html" .}}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment