1

I've been following an earlier post to check if an asset exists, which works great when using specific filenames.

Symfony2 and Twig - Check if an asset exists

However instead of checking for a specific image filename, how do you check for an image type? (all files with the .jpg extension)

Example: *.jpg (which doesn't work neither does .jpg)

{% if asset_exists('/images/*.jpg') %}
    <div class="entry-thumbnail">
            <img width="230" height="172" src="{{ asset(['images/', post.image]|join) }}" class="" alt=""/>
    </div>
{% endif %}
Community
  • 1
  • 1
esteemed.squire
  • 524
  • 1
  • 7
  • 20

2 Answers2

0

You'd have to modify the Twig Extension and replace !is_file($toCheck) with count(glob($toCheck)) < 1

glob function (php.net)

Tom Tom
  • 3,680
  • 5
  • 35
  • 40
0

As soon as you are using:

{{ asset(['images/', post.image]|join) }}`

Why not using:

{% if asset_exists(['images/', post.image]|join) %}

Tip: replace ['images/', post.image]|join by using the concat operator ~: 'images/' ~ post.image

Alain Tiemblo
  • 36,099
  • 17
  • 121
  • 153