dongwalun2507 2018-08-31 22:46
浏览 50
已采纳

在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
  • 写回答

1条回答 默认 最新

  • dpy33121 2018-10-03 16:56
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ROS Turtlebot3 多机协同自主探索环境时遇到的多机任务分配问题,explore节点
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改
  • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
  • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?
  • ¥40 串口调试助手打开串口后,keil5的代码就停止了
  • ¥15 电脑最近经常蓝屏,求大家看看哪的问题