Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

General

Twig not duplicate rows

plugins

Solved by pamtbaau View solution

Started by Daniel Dobiášovský 5 years ago · 2 replies · 839 views
5 years ago

Hello everyone,

again I am solving a problem that seemed solved and I can't find a solution.

Please, how do I limit the generation of multiple variables below each other so that the result is not duplicated?

My form loads 1-5 folders, if I choose one, its name and link will be generated correctly, but if I choose two or more, the lists of folders below me will be displayed:

Example:

For one file:
(Correct)

TXT
* Show file: File.jpg

For 3 files:
(Problem)

MARKDOWN

* Show file: File.jpg
* Show file: File1.jpg
* Show file: File2.jpg

* Show file: File.jpg
* Show file: File1.jpg
* Show file: File2.jpg

* Show file: File.jpg
* Show file: File1.jpg
* Show file: File2.jpg

Code:

TWIG
<ul>
  {% for val in value %}
  {% if val is iterable %}
  <ul>
    {% for item in form.value(field.name) %}
    <li>Show file: <a href="..//{{ string(item.path) }}" download target="_blank">{{ item.name }}</a></li>
    {% endfor %}
  </ul>
  {% else %}
  <li>{{ string(form.value(field.name))|nl2br }}</li>
  {% endif %}
  {% endfor %}
</ul>

this is the part of code from: "data.html.twig"

I try some like this: https://twig.symfony.com/doc/2.x/filters/merge.html
But not the solution.

How can I fix this, thank you all very much for their time.

5 years ago Solution

@Deight, Like in a previous question, you are looping inside a loop...

The value of val is the same as the value for item. Drop the inner loop and replace item.path and item.name with val.path and val.name.

To debug what is happing inside a Twig template, use {{ dump(variable) }} and track the dumps in the ClockWork tab in the developer console of the browser.

5 years ago

I love you man, i am looping inside the loop, wow! Thank you very much.

Solution is call only variables not variables in variables:

TWIG
{% for val in value %}
<a href="../{{ string(val.path) }}" download target="_blank"> {{ val.name }}</a><br>
{% endfor %}

Suggested topics

Topic Participants Replies Views Activity
General · by Jerry Hunt, 4 days ago
2 80 9 hours ago
General · by pamtbaau, 14 hours ago
1 51 13 hours ago
General · by Andy Miller, 1 day ago
0 44 1 day ago
General · by Marcel, 12 months ago
6 346 5 days ago
General · by Duc , 5 days ago
3 40 5 days ago