duanji6997 2014-06-26 11:22
浏览 52
已采纳

如何在Beego的模板文件中获取范围索引

For example

{{ range .cars }} 
{{.color}} , ${{.price}} 
{{ end }}

The array size of 'cars' is 10. How do I get the index value?

When the index is 5, I want do some process.

I am using Beego web framework.

  • 写回答

1条回答 默认 最新

  • doutu1889 2014-06-26 17:27
    关注

    First of all, the answer to your question was given in the comment by topskip.

    range $index, $element := pipeline
    

    See the documentation at golang.org/pkg/text/template/#hdr-Variables.

    Secondly, bear in mind that templates rapidly get too complex when you start putting logic in, such as treating the 5th element differently.

    With this in mind, it is a good idea instead to try to design the view models so that they are simple to render. More logic might be needed in your controllers, but it's usually easier to test there. Alternatively, consider using helper utilities written in Go.

    Less logic in the templates will make things generally easier.

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

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器