Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在细枝中的循环中仅显示一次_Php_Symfony_Twig - Fatal编程技术网

Php 在细枝中的循环中仅显示一次

Php 在细枝中的循环中仅显示一次,php,symfony,twig,Php,Symfony,Twig,我有一些照片,我想显示几个月 但我知道我的密码,我每个月都会在每张照片的上面 如何避免这种情况 {%用于媒体中的媒体%} {%如果media.date | date(“m”)==10且media.assetpath不为空%} 十月照片 {%elseif media.date | date(“m”)==11且media.assetpath不为null%} 照片11月 {%else%} 其他月份 {%endif%} {%endfor%}假设medias是一个按日期排序的数组,可以使用临时变量解决此

我有一些照片,我想显示几个月

但我知道我的密码,我每个月都会在每张照片的上面

如何避免这种情况

{%用于媒体中的媒体%}
{%如果media.date | date(“m”)==10且media.assetpath不为空%}
十月照片
{%elseif media.date | date(“m”)==11且media.assetpath不为null%}
照片11月
{%else%}
其他月份
{%endif%}

{%endfor%}
假设
medias
是一个按日期排序的数组,可以使用临时变量解决此问题:

{% set last_month = '' %}
{% for media in medias %}
  {% set month = media.date('F')|lower %}
  {% if last_month and month != last_month %}
    <h2>Photos {{ month }}</h2>
  {% endif %}
  {% set last_month = month %}

  <div class="col-xs-2">
    <img class="img-responsive" src="{{ asset(media.assetpath)  }}"/>
  </div>
{% endfor %}
使用此结构,模板代码将看起来更干净:

{% for month, media in medias %}
  <h2>Photos {{ month }}</h2>
  {% for m in media %}
  <div class="col-xs-2">
    <img class="img-responsive" src="{{ asset(m.assetpath) }}"/>
  </div>
  {% endfor %}
{% endfor %}
{%月份,媒体中的媒体%}
照片{{月}
{媒体%中的m的%s}
{%endfor%}
{%endfor%}

假设
medias
是一个按日期排序的数组,则可以使用临时变量解决此问题:

{% set last_month = '' %}
{% for media in medias %}
  {% set month = media.date('F')|lower %}
  {% if last_month and month != last_month %}
    <h2>Photos {{ month }}</h2>
  {% endif %}
  {% set last_month = month %}

  <div class="col-xs-2">
    <img class="img-responsive" src="{{ asset(media.assetpath)  }}"/>
  </div>
{% endfor %}
使用此结构,模板代码将看起来更干净:

{% for month, media in medias %}
  <h2>Photos {{ month }}</h2>
  {% for m in media %}
  <div class="col-xs-2">
    <img class="img-responsive" src="{{ asset(m.assetpath) }}"/>
  </div>
  {% endfor %}
{% endfor %}
{%月份,媒体中的媒体%}
照片{{月}
{媒体%中的m的%s}
{%endfor%}
{%endfor%}

很大程度上取决于
媒体的组成。我认为你迭代每一种媒体,只检查一个静态数字。我没有看到本月的支票。看看
循环。首先也是
。这主要取决于
媒体的组成。我认为你迭代每一种媒体,只检查一个静态数字。我没有看到本月的支票。看一下
循环。第一个也是
。+1表示答案的后半部分。前半部分的问题是,您不知道媒体阵列是如何排序的,因此,您可能会在下半部分得到类似于
一月
三月
一月
,…+1的答案。前半部分的问题是,您不知道媒体阵列是如何排序的,因此可能会得到类似于
一月
三月
一月
。。。