2017-10-24 4 views
0

Meine Neufassung zu Twig funktioniert nicht. Was ist falsch?PHP-Variable auf Twig umschreiben

PHP Quelle:

<?php foreach ($datas as $data) { ?> 
      <?php if ($data['information_id'] == $info_id) { ?> 
       <a href="<?php echo $data['href']; ?>" class="list-group active"><?php echo $data['title']; ?></a>    
      <?php } else { ?> 
      <a href="<?php echo $data['href']; ?>" class="list-group"><?php echo $data['title']; ?></a> 
      <?php } ?> 
      <?php } ?> 

Rewrite Zweig:

{% for datas in data %} 
      {% if {{ attribute(data,information_id) }} == info_id %} 
      <a href="{{ data.href }}" class="list-group active">{{ data.title }}</a> 
      {% else %} 
      <a href="{{ data.href }}" class="list-group">{{ data.title }}</a> 
      {% endif %}   
     {% endfor %} 
+0

Bitte geben Sie weitere Einzelheiten an. Zum Beispiel, was hast du bisher versucht? Bitte lesen Sie https://stackoverflow.com/help/how-to-ask – Cleared

+1

'{{data.information_id}}'/'{{data ['information_id']}}'/'{{Attribut (data, information_id)}} ' – DarkBee

Antwort

2

Versuchen:

{% for data in datas %} 
     {% if attribute(data,information_id) == info_id %} 
      <a href="{{ data.href }}" class="list-group active">{{ data.title }}</a> 
     {% else %} 
      <a href="{{ data.href }}" class="list-group">{{ data.title }}</a> 
     {% endif %}   
{% endfor %} 

Sie brauchen nur Variablen in {{ }} wickeln, wenn man sie auf die sind die Ausgabe Seite. Nicht in der Mitte einer {% if %} Aussage.

Auch ich habe rund {% for datas in data %} zu {% for data in datas %} ausgetauscht. Ich nehme an, datas ist das Array, und data ist jedes einzelne Element.

https://twig.symfony.com/doc/2.x/tags/for.html