为了账号安全,请及时绑定邮箱和手机立即绑定

从 Revel 中的 ViewArg 选择模板

从 Revel 中的 ViewArg 选择模板

Go
DIEA 2023-03-29 15:32:03
使用 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 clauseERROR 2018/08/31 17:46:10 server.go:99: Template Compilation Error (in layout.html:10): unexpected ".ContentTe"... in template clause
查看完整描述

1 回答

?
Qyouu

TA贡献1786条经验 获得超11个赞

作为解决此问题以更接近所需功能的解决方法,我实施了以下(不太理想)解决方案:


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。


查看完整回答
反对 回复 2023-03-29
  • 1 回答
  • 0 关注
  • 70 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信