0

I want to use a looping functionality in region.html.twig file to I can wrap elements that outputted from {{ content }} section.

Defult region.html.twig

{% if content %}
  <div class="Parent">
    {{ content }}
  </div>
{% endif %}

The schematic generated Output:

<div class="Parent">
  Item_1
  Item_2
  Item_3
</div>

I try to use below code to create a loop and add a wrapper around of each Item outputted from {{ content }}:

My code:

{% if content %}
  <div class="Parent"> 
    {% for item in items %}
      <div class="child-wrapper">{{ item.content }}</div>
    {% endfor %}
  </div>
{% endif %}

The Final outpute that I want to achive:

<div class="Parent">
  <div class="child-wrapper">Item_1</div>
  <div class="child-wrapper">Item_2</div>
  <div class="child-wrapper">Item_3</div>
</div>
11
  • 3
    The code looks fine...what isn't working? Commented Jul 13, 2017 at 5:15
  • @Jason Roman: Thanks for your reply, but whatever I try I can't get any result!!! Commented Jul 13, 2017 at 6:13
  • @MojtabaReyhani any errors or Excetption ? try to inspect with {% dump content items %} to see whats inside and how they structured Commented Jul 13, 2017 at 6:22
  • Why don't you try showing is what your code is currently outputting? Commented Jul 13, 2017 at 6:42
  • 1
    Should be {{ dump(content, items) }} and you need to use the debug URL in Symfony, as in append app_dev.php to your URL. If you use in prod you will get an error, since dump is not recognized. Commented Jul 13, 2017 at 15:10

1 Answer 1

1
If your data is: content = [1, 2, 3, ... , 100]

   Then write
    {% if content %}
      <div class="Parent"> 
        {% for item in content %}
          <div class="child-wrapper">{{ item }}</div>
        {% endfor %}
      </div>
    {% endif %}

If your data is array of assotiated arrays like this: 
content = [
    ['content' => 1],   
    ['content' => 2],   
]

   Then write
    {% if content %}
      <div class="Parent"> 
        {% for item in content %}
          <div class="child-wrapper">{{ item.content }}</div>
        {% endfor %}
      </div>
    {% endif %}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.