我使用以下代码将selectedGallery设置为defaultValue。默认值应该是galleryData的第一个Element。 可悲的是,它不起作用。剪切后运行中,selectedGallery不存在... 谢谢你的帮助
{% 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 %}
以后同一个文件:
<div id="{{idPref}}PictureBox" class="backA">
{% block pictureBox %}
{% for picture in galleryData[selectedGallery] %}
{{selectedGallery}}
{% endfor %}
{% endblock %}
Symfony说这个变量不存在。
答案 0 :(得分:2)
尝试:
首先:
{% if selectedGallery is not defined %}
{% set selectedGallery = galleryData|keys|first %}
{# ... #}
http://twig.sensiolabs.org/doc/filters/first.html
字母:
<div id="{{idPref}}PictureBox" class="backA">
{% block pictureBox %}
{% for picture in attribute(galleryData, selectedGallery) %}
{{picture}}
{% endfor %}
{% endblock %}
{# ... #}