jstuardo
jstuardo

Reputation: 4383

string comparison in twig

Someone can explain this?

In twig, I need to detect if certain controller is loaded. Depending on it, it should display a HTML block or another.

I have this comparison in twig template:

{% if app.request.attributes.get("_controller") == 'App\Controller\DefaultController::index' %}

Even when controller and action is actually "App\Controller\DefaultController::index" expression evaluates to false showing the block intended to be shown when the controller is other.

Besides explanation, I need to solution please.

by the way, when I print the controller name, using

{{ app.request.attributes.get("_controller") }}

I can check this strange behaviour.

Regards Jaime

Upvotes: 0

Views: 1007

Answers (1)

Chip Dean
Chip Dean

Reputation: 4302

Twig is reading the backslashes in your string as escape characters. To make it work you must escape the backslashes. Like this:

'App\\Controller\\DefaultController::index'

Upvotes: 2

Related Questions