【问题标题】:How to get a SubString of variable using stencil templating tool for swift如何使用模板模板工具快速获取变量的子字符串
【发布时间】:2018-06-17 03:24:03
【问题描述】:

我正在使用此处找到的 swift 模板语言工具: https://github.com/stencilproject/Stencil

使用主分支。

问题

给定以下.json 文件

{
  "xcassets" : "dev Sources test1"
}

我希望能够检索由" " 分隔的第一个单词,即“dev”。

我的尝试

最新版本的 Stencil 具有 Split Function。但问题是我不知道如何访问结果数组中的第一个元素,而且它也不在documentation 中。

我在模板文件中尝试了以下内容:

{{xcassets|split:" "[0]}}

{{{{xcassets|split:" "}}[0]}}

{{xcassets|split:" ".first}}

{{xcassets|split:" "}}.first}}

{{xcassets|split:" "|[0]}}

{{xcassets|split:" "|.first}}

{{xcassets|split:" "|first}}

这些都不起作用。

我要避免的事情

我知道我可以这样做,但一定有更好的方法。

{% for element in xcassets|split:" " %}
    {% if forloop.first %}
        {{ element }}
    {% endif %}
{% endfor %}

有人对更好的工具有建议吗?

【问题讨论】:

    标签: templating swift-stencil


    【解决方案1】:

    我想这是唯一的方法:

    {% for element in xcassets|split:" " %}
        {% if forloop.first %}
            {{ element }}
        {% endif %}
    {% endfor %}
    

    【讨论】:

    • 为了更好的可读性,把它放在一个 {% macro %}
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-19
    • 1970-01-01
    • 2018-10-23
    • 2019-02-18
    相关资源
    最近更新 更多