string comparison in twig - symfony

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

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'

Related

How to render variable without special characters

We are using a variable like this, to render the text without special characters like HELP & SUPPORT,
{{ variable|render|striptags|trim|convert_encoding('UTF-8',
'HTML-ENTITIES') }}
After upgrading from Drupal 8 to 9, we are getting errors like,
Notice: iconv(): Wrong charset, conversion from HTML-ENTITIES' to UTF-8' is not allowed in twig_convert_encoding() (line 1009 of
/var/www/html/stg.flowbusiness.co/vendor/twig/twig/src/Extension/CoreExtension.php)
And the variable is not displaying with convert_encoding function.
So, any suggestions to display the text without special characters in drupal 9.
convert_encoding() in twig is function meant to convert a string from one encoding to another; HTML-ENTITIES is not an encoding; this why the error is raised.
https://twig.symfony.com/doc/2.x/filters/convert_encoding.html
As it's not doing what you're using it for, you should remove that part.
If you want to have the html entities to be rendered correctly, you should remove the striptags part too.
Using |raw filter would normally render chars as you need.
This twig rendering tag
{{ variable|render|striptags|trim|convert_encoding('UTF-8', 'HTML-ENTITIES') }}
would become
{{ variable|raw }}
While & char is an HTML entity, it would be correctly render as raw, without showing special chars.

two values to same key in twig path

I'm attempting to pass an array to a parameter in a twig path.
I need to do include two values for the same key.
I've attempted to do something like this
path('path_name', {'status': array('req', 'ord')}) }}
but twig does not seem to like arrays in paths.
I've tried a number of other options. This doesn't err but of course the second key-value overwrites the first
path('path_name', {'status': 'req', 'status': 'ord'}) }}
It doesn't seem to like this either:
path('path_name', {('status':'req') and ('status': 'ord')})
Any suggestions? I'm hoping to avoid rewriting my methods and stay DRY
For simple lists, you need to use [] to declare your array in Twig.
The following expression will add both parameters to your query string:
{{ path('path_name', {'status': ['req', 'ord']}) }}
The generated path will include status[0]=req&status[1]=ord in its query string.

Wrong quotes handling in JMS Translation bundle

Faced strange quote escaping in JMS Translation bundle (or I'm doing something wrong). The target text in xliff file is like "quoted-text". On result web page it is shown like "quoted-text" because ampersand in html code is printed like & instead of 'as is' - &. Seems like there is excess symbol escaping somewhere between xliff reading and html code generation. Could anybody suggest how to solve this problem?
Solved by adding raw filter to translated string in twig file, like:
{{ 'quoted.text' | trans | desc('text which translation has quotes') | raw }}

Symfony 2.1+ encodes hash/pound "#" in routes

I'm in the process of upgrading from Symfony 2.0 to 2.3. We have routes with hashes defined since we have a single page app.
A route configured via annotations:
/**
* #Route("/app#orders/{id}", name="app_order")
*/
We use Twig to generate emails and use these routes within the Twig templates:
View order
Before upgrading, this worked fine. After the upgrade, the # gets encoded to %23 but the slashes remain intact. This of course generates an invalid URL in the email.
Why are only the hashes encoding and not the slashes? It should be all or nothing. What options do I have here other than doing a string replace?
Things I've already tried doing which don't help:
Setting autoescape to false {% autoescape false %}
Using raw {{ url(...)|raw }}
Using raw and autoescape=false in conjunction
If you take a look at the UrlGenerator code, you can see, the hashtag is not decoded after rawurlencode.
The escaping of the hashtag was added in commit 6039569.
As a workaround you could extend the UrlGenerator class and replace the $decodedChars array with hashtag included. Then tell Symfony to use your generator class:
parameters:
router.options.generator_base_class: Acme\MyBundle\Routing\HashtagDecodedUrlGenerator

Extended use of replace filter in twig?

i checked out the documentation for the replace filter in twig. my problem is that, suppose i have a variable say contvariable, and the content passed through that variable from the controller is dynamic
return $this->render('RodasysFormstudyBundle:Default:addclientname.html.twig', array('contvariable' =>$sometext));
this $sometext variable will contain texts like
$sometext='%Sun% rises in the East';
the text inside the %% should be displayed as an input field in the browser. I did not find any examples in the web like to replace the content inside the %% (what ever the content be whether its sun or the moon). Is this possible to do this using replace filter or should i follow some other method such as to replace the content in controller before sending it to twig..
please help..
You could do something like that (with the 'raw' filter) :
{{ "%foo% rises in the East"|replace({'%foo%': "<input type='text' name='"~foo~"' value='"~foo~"'/>"})|raw }}
foo is a variable sent by your controller, with the value of your choice.

Resources