Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

in twig file after split the value

in twig file after spliting the value i’m getting output like this in a browser

<select class="form-control" id="eventCustomerID" name="eventCustomer">
    <option value="64" name="64">64</option>
    <option value="demou22_6hhh" name="demou22_6hhh">demou22_6hhh</option>
</select>

but i need an output to be like this

<select class="form-control" id="eventCustomerID" name="eventCustomer">
    <option value="64" name="64">demou22_6hhh</option>
</select>

this is my code in a twig file

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<select class="form-control" id="eventCustomerID" name="eventCustomer">
    {% set tags = 64|demou22_6hhh|split('|',1) %}
        {% for x in tags %}
            <option value="{{ x }}" name="{{ x }}">{{ x }}</option>
        {% endfor %}
</select>

I am stuck with this can someone help me out with this issue.

>Solution :

To achieve your required output, you shouldn’t use a for loop, but access the elements directly.

<select class="form-control" id="eventCustomerID" name="eventCustomer">
    {% set tags = "64|demou22_6hhh"|split('|') %}
    <option value="{{ tags[0] }}">{{ tags[1] }}</option>
</select>

demo


Note about the second argument you are passing to the filter split

Not sure why you are passing 1 as the second argument. By setting this parameter to 1, you are limiting the output to one element in the array. More information here.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading