How to get a SubString of variable using stencil templating tool for swift

912 views Asked by At

I'm using the stencil template language tool for swift found here: https://github.com/stencilproject/Stencil

Using master branch.

The Problem

given the following .json file

{
  "xcassets" : "dev Sources test1"
}

I want to be able to retrieve the first word delimited by " " which is "dev".

What I've Tried

The latest version of Stencil has the Split Function. But the problem is I can't figure out how to access the first element in the resulting array, and it's not in the documentation either.

I've tried the following in the stencil file:

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

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

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

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

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

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

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

None of which worked.

What I'm trying to avoid

I know I can do it this way, but there must be a better way.

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

Anyone have suggestions for a better tool?

1

There are 1 answers

1
Jerry B. no.1 intern On

I guess this is the only way:

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