使用 Revel 框架,是否可以根据 ViewArg 的值选择模板?
我定义了一个基本控制器,它提供了一种在views/Layout.html中呈现内容的方法
type Controller struct {
*revel.Controller
}
func(c *Controller) RenderView(view string, extraViewArgs ...interface{}) revel.Result {
// ... omitted source
c.ViewArgs["ContentTemplateName"] = view
return c.RenderTemplate("layout.html")
}
实现它的示例控制器如下
type MyController struct {
Controller
}
func (c MyController) Index() revel.Result {
bananas := "This is bananas"
return c.RenderView("App/Bananas.html", bananas)
}
然后我尝试在 layout.html 中呈现“App/Bananas.html”
{{set . "title" "Home"}}
{{template "header.html" .}}
<div class="container">
<div class="row">
{{template "flash.html" .}}
</div>
<!-- Left column of Content -->
<div class="col-md-9 col-sm-8 col-xs-12">
{{template .ContentTemplateName .}}
</div>
<!-- //Left Column of Content -->
<!-- Right column of summary -->
<div class="col-md-3 col-sm-4 hidden-xs">
<div class="container">
{{template "sidebar.html" .}}
</div>
</div>
<!-- //Right column of summary -->
</div>
{{template "footer.html" .}}
这将输出以下结果
ERROR 2018/08/31 17:46:10 template.go:338: Template compilation error (In layout.html around line 10):
unexpected ".ContentTe"... in template clause
ERROR 2018/08/31 17:46:10 server.go:99: Template Compilation Error (in layout.html:10): unexpected ".ContentTe"... in template clause
Qyouu
相关分类