Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 650
Jerry B. no.1 intern Avatar asked Jun 17 '18 03:06

Jerry B. no.1 intern


1 Answers

I guess this is the only way:

{% for element in xcassets|split:" " %}
    {% if forloop.first %}
        {{ element }}
    {% endif %}
{% endfor %}
like image 62
Jerry B. no.1 intern Avatar answered Oct 06 '22 00:10

Jerry B. no.1 intern