How to merge string to an array?

62 Views Asked by At

I tried to merge string into twig array but it will show the last same value for all index. What's the proper way to merge a string to an array?

{% set projectArray = [] %}
{% set projectTitleArray = [] %}
{% set projectQuery = entries('projects', 'projects').where('enabled', 1).orderBy('sort_order').get() %}
TITLE - URL:
{% for project in projectQuery %}
    {% set projectArray = projectArray|merge([project.main_image.make.url()]) %}
    {% set projectTitleArray = projectTitleArray|merge([project.title]) %}
    <ul>
        <li>{{project.title}} - {{project.main_image.make.url()}}</li>
    </ul>
{% endfor %}
<hr/>
TITLE:
{% for title in projectTitleArray %}
    <ul>
        <li>{{title}}</li>
    </ul>
{% endfor %}
URL: 
{% for title in projectArray %}
    <ul>
        <li>{{title}}</li>
    </ul>
{% endfor %}
<div class="py-6 bg-slate-100">
    <div class="container my-10 mx-auto">
        <h1 class="section-title-1">
            Projects
        </h1>
    </div>
    <div class="xl:pl-[5rem]">
        <product-projects :title="{{projectTitleArray|json_encode(constant('JSON_PRETTY_PRINT'))}}" :projects="{{projectArray|json_encode(constant('JSON_PRETTY_PRINT'))}}"></product-projects>
    </div>
</div>

Output enter image description here

0

There are 0 best solutions below