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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
    • ¥15 cmd cl 0x000007b
    • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
    • ¥500 火焰左右视图、视差(基于双目相机)
    • ¥100 set_link_state
    • ¥15 虚幻5 UE美术毛发渲染
    • ¥15 CVRP 图论 物流运输优化
    • ¥15 Tableau online 嵌入ppt失败
    • ¥100 支付宝网页转账系统不识别账号
    • ¥15 基于单片机的靶位控制系统