douri4459 2016-08-10 09:48
浏览 259

如何将变量传递给模板并在beego中接收变量

My template file:

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

I want pass $option to template, and alternative_src.html code :

{{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}}

but I get problem below:

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

1条回答 默认 最新

  • doudinghan8319 2016-08-10 16:07
    关注

    When you use the {{template}} action and you pass something, that becomes the dot . in the called template. Quoting from the package doc of text/template:

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

    So inside alternative_src.html refer to the option only as a dot . (the dot . will mean the value of the pipeline you passed to the template, which is $option in the caller template):

    {{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}}
    
    评论

报告相同问题?

悬赏问题

  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比