Search code examples
symfonytwigassetic

Assigning output of a twig function to a twig variable


I have the twig/assetic code below:

{% if post.media %}
    {% image '@ApplicationMyBundle/Resources/images/AjaxLoader-298x179.gif' output='/images/AjaxLoader-298x179.gif' %}
        <img src="{{ asset_url }}" data-src="{% path post.media, 'news_size_4' %}" />
    {% endimage %}
{% endif %}

The real image path is in {% path post.media, 'news_size_4' %}, I need to get this output in to a variable so i can use it elsewhere. {{ asset_url }} is no good as that only gives me the ajaxloader image.

Is there anyway this can be done?


Solution

  • As suggested by Hakim in a comment, you can use the set Twig tag.

    Define the variable:

    {% set myPath %}
        {% path post.media, 'news_size_4' %}
    {% endset %}
    

    Display this variable:

    {{ myPath }}