douyanjing8287 2019-04-30 02:22
浏览 64

使用HTML模板:定义要覆盖范围的字符串数组

In Jinja2 (Python Flask) templates, I'm able to create a static navigation menu by defining a list of tuples with code similar to the following:

{% for item in [('', 'Home'), ('menu1', 'Menu1'), ('menu2', 'Menu2')] %}
<li><a href="{% if item[0] == '' %}/{% else %}{{ '/%s/' % item[0] }}{% endif %}">{{ item[1] }}</a></li>
{%- endfor %}

I'd like to create something similar in Go HTML templates. I assume the equivalent to a list of tuples would be an array/slice of arrays of strings, i.e., something like

{{ $items := { {"", "Home"}, {"menu1", "Menu1"}, {"menu2", "Menu2"} } }}
{{ range $items }}
<li><a href="{{if .[0] == \"\"}}/{{else}}{{ \"/.[0]/\" }}{{end}}">{{ .[1] }}</a></li>
{{end}}

However, at runtime, specifically when Go tries to parse the template files it panics with unexpected "{" in command (it used to panic with unexpected "{" in range when I used a range directly).

So, is it possible to declare an array of arrays of strings in a template?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

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