Reynier
Reynier

Reputation: 2478

Use twig in asset sentence

It's possible to use a Twig result in asset sentence? See what I'm trying below:

{% for entity in entities %}
    <li title="{{ entity.getName }}" data-id="{{ entity.getId }}" class="categories-first"><img src="{{ asset('bundles/dashboard/img/categories/{{ entity.getName|lower|replace("ó":"o") }}.gif') }}"></li>
{% endfor %}

But it's not loading the image since this {{ entity.getName|lower|replace("ó":"o") }} isn't evaluated, it's possible? How?

Also related to this same topic, it's possible to remove special characters from output? Let said á, é, í, ó, ú and so on?

Upvotes: 0

Views: 341

Answers (2)

hcoat
hcoat

Reputation: 2643

It will work if you use twig's concatenation.

{% for entity in entities %}
    <li title="{{ entity.getName }}" data-id="{{ entity.getId }}" class="categories-first"><img src="{{ asset('bundles/dashboard/img/categories/' ~ entity.getName|lower|replace({"ó":"o"}) ~ '.gif') }}"></li>
{% endfor %}

Upvotes: 2

david
david

Reputation: 3218

You can't put your twig result inside asset. If you put this code below

bundles/dashboard/img/categories/{{ entity.getName|lower|replace("ó":"o") }}.gif

to your asset, then the result for image source is exactly as this code above.

Upvotes: 0

Related Questions