【问题标题】:How can pass variable to a template and receive variable in beego如何将变量传递给模板并在beego中接收变量
【发布时间】:2016-08-10 09:48:45
【问题描述】:

我的模板文件:

{{range $index, $option := .alternatives}}
<div id="splashAlternative{{$index}}" class="col-sm-2">
    <select id="flashSrc_{{$index}}">
    {{template "alternative_src.html" $option}}
    </select>
</div>
{{end}}

我想将$option 传递给模板,并将alternative_src.html 代码传递给:

{{if compare .option ""}}
  <option value="" selected="selected">
  </option>
{{else}}
  <option value=""></option>
{{end}}
{{if compare .option "xxx"}}
  <option value="xxx" selected="selected">xxx</option>
{{else}}
  <option value="xxx">xxx</option>
{{end}}

但我在下面遇到问题:

executing "alternative_src.html" at <.option>: can't evaluate field option in type string

【问题讨论】:

    标签: templates go beego go-templates


    【解决方案1】:

    当您使用{{template}} 操作并传递一些东西时,这将成为被调用模板中的点.。引用text/template的包文档:

    {{template "name" pipeline}}
        The template with the specified name is executed with dot set
        to the value of the pipeline.
    

    所以在alternative_src.html 内部仅将选项引用为点.(点. 将表示您传递给模板的管道的值,即调用者模板中的$option):

    {{if compare . ""}}
      <option value="" selected="selected">
      </option>
    {{else}}
      <option value=""></option>
    {{end}}
    {{if compare . "xxx"}}
      <option value="xxx" selected="selected">xxx</option>
    {{else}}
      <option value="xxx">xxx</option>
    {{end}}
    

    【讨论】:

      猜你喜欢
      • 2017-11-10
      • 1970-01-01
      • 1970-01-01
      • 2016-09-26
      • 1970-01-01
      • 2018-04-07
      • 2019-04-03
      • 2011-05-08
      • 2015-08-24
      相关资源
      最近更新 更多