Kasra Khosravi
Kasra Khosravi

Reputation: 23

Symfony2 Twig Conditional Template Name

I'm looking for a way to check in my twig template if the name of the template contains a special word. If that is the case I want to proceed with assigning some stuff. Here is a general idea I have in mind.

{% if [sth like app.request.template_name or sth like that]  in `product` %}
 // Do some stuff
{% endif % }

Can you guys help me with this?

Upvotes: 1

Views: 134

Answers (1)

LMS94
LMS94

Reputation: 1036

If you're creating separate template files then you'll know the names and can hardcode the values:

template_1.html.twig:

{% set some_var = 1 %}
{% set another_var = 2 %}

template_2.html.twig:

{% set custom_var = 5 %}

Update:

If you want the template name, you can use:

{% if 'product' in _self.getTemplateName() %}
    {# Do stuff #}
{% endif %}

Upvotes: 1

Related Questions