从 Revel 中的 ViewArg 选择模板
Choosing a template from a ViewArg in Revel
使用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
根据@mh-cbon 的评论,发现无法将变量用作template
函数 的输入。
作为解决此问题以更接近所需功能的解决方法,我实施了以下(不太理想的)解决方案:
base_content_view.html
{{template "begin_content.html" . }}
<!-- content here -->
{{template "end_content.html" . }}
begin_content.html
{{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">
end_content.html
</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 -->
{{template "footer.html" .}}
base_content_view.html是一个静态文件,在创建新视图时被复制粘贴为模板。此设计确实打开了其他问题,但作为 template
函数限制的示例解决方法。
使用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
根据@mh-cbon 的评论,发现无法将变量用作template
函数 的输入。
作为解决此问题以更接近所需功能的解决方法,我实施了以下(不太理想的)解决方案:
base_content_view.html
{{template "begin_content.html" . }}
<!-- content here -->
{{template "end_content.html" . }}
begin_content.html
{{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">
end_content.html
</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 -->
{{template "footer.html" .}}
base_content_view.html是一个静态文件,在创建新视图时被复制粘贴为模板。此设计确实打开了其他问题,但作为 template
函数限制的示例解决方法。