在Revel中从ViewArg选择模板

Using the Revel framework, is it possible to chose a template based on the value of a ViewArg?

I defined a base controller which provides a method that renders content within 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")
}

An example controller implementing this is as follows

type MyController struct {
    Controller
}

func (c MyController) Index() revel.Result {
    bananas := "This is bananas"
    return c.RenderView("App/Bananas.html", bananas)
}

I then try to render "App/Bananas.html" within layout.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" .}}

This outputs the following result

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
doufei0933
doufei0933 模板函数不允许输入变量。它必须是静态文本模板名称。您可以在模板包源代码中检查此行为,负责解析模板名称的函数仅识别字符串或原始字符串golang.org/src/text/template/parse/parse.go#L582。还要注意,模板节点的字段名称是字符串,而不是管道,请参阅golang.org/pkg/text/template/parse/#TemplateNode
大约 2 年之前 回复

1个回答

Based on a comment by @mh-cbon, it was discovered that its not possible to use a variable as input to the template function.

As a workaround to this problem to get closer to the desired features, I implemented the following (less than ideal) solution:

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 is a static file which is copy pasted as a template when making new views. This design does open other issues however serves as an example workaround for the limitation of the template function.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问