Martin
Martin

Reputation: 621

Simple Twig Logic issue

I'am using following code to set selectedGallery to a defaultValue. The default Value should be galleryData's first Element. Sadly it doesn't work. selectedGallery does not exist after the snipped run through... Thanks for help

{% if selectedGallery is not defined %}
    {% for gallery in galleryData|keys|slice(0, 1) %}
        {% set selectedGallery = gallery %}
        //if i access galleryData here, it exists ?!
    {% endfor %}
{% endif %}

later the same file:

<div id="{{idPref}}PictureBox" class="backA">
    {% block pictureBox %}
        {% for picture in galleryData[selectedGallery] %}
            {{selectedGallery}}
        {% endfor %}
    {% endblock %}

Symfony says that the variable doesn't exists.

Upvotes: 0

Views: 54

Answers (1)

jdharandas
jdharandas

Reputation: 526

Try:

First:

{% if selectedGallery is not defined %}
    {% set selectedGallery = galleryData|keys|first %}
{# ... #}

http://twig.sensiolabs.org/doc/filters/first.html

Later:

<div id="{{idPref}}PictureBox" class="backA">
    {% block pictureBox %}
        {% for picture in attribute(galleryData, selectedGallery) %}
            {{picture}}
        {% endfor %}
    {% endblock %}
{# ... #}

http://twig.sensiolabs.org/doc/functions/attribute.html

Upvotes: 2

Related Questions