Christian Edmonds
Christian Edmonds

Reputation: 21

How to access file paths in Twig

I need to get the file path to images in a directory and output them as the src="" in the image. I've attempted to set a variable as the images directory, and then created a loop to run over each item in the directory.

Example:

  {% set files = "/images/case-study/study-thumbnails/" %}
  {% for image in files %}
    <div class="project">
      <a href=""><img src="{{ image }}"></a>
    </div
  {% endfor %}

I know I'm probably way off on this but I'm pretty new to Twig and have been lost in this for hours now.

Upvotes: 0

Views: 464

Answers (1)

MarkSkayff
MarkSkayff

Reputation: 1374

I'd advise you to try and move all that logic to the Controller instead of the view. Then, you won't get a file pointer just by assigning a string path name in Twig. That won't work.

What I'd recommend you is processing all these images paths in the Controller, and storing them in an array. And then you pass this array to the Twig view. Then you'll be free to handle this array in the Twig file.

Upvotes: 1

Related Questions