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

1

8

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?

Gynecoid answered 17/6, 2018 at 3:24 Comment(0)
G
2

I guess this is the only way:

{% for element in xcassets|split:" " %}
    {% if forloop.first %}
        {{ element }}
    {% endif %}
{% endfor %}
Gynecoid answered 1/9, 2020 at 0:57 Comment(1)
For better readability put this inside a {% macro %}Glennisglennon

© 2022 - 2024 — McMap. All rights reserved.