How to render variable without special characters - symfony

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.

Related

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
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'

Displaying accented characters in twig

I've been having problem displaying accented characters (and somtimes apostrophes too) in twig. The site's been recently hosted and I was experiencing none of this on my local server. This problem's been encountered before (there's a question about it here but using raw doesn't work for me). In twig, I have something like this:
{{ entry.textFr|striptags|raw }}
Despite using the raw filter, I can't get those characters to be displayed right (I'm also aware that the raw tag is dangerous but I fully trust the content of the entry entity).
More profoundly, I'm using symfony2 and stfalcon's tinymce bundle, which seems to convert those characters automatically before they are persisted in the db. None of this was the case on my local server.
Does someone have an idea?
Edit
In particular, it seems that 'é', 'è', 'à', 'ê', 'ô', as well as ellipsis and apostrophes are displayed as é, è, à, etc. I've just tried using the replace filter like this |replace({é': 'é', 'è': 'è', 'à': 'à', //ect }) and it seems to work but it's kind of dirty
Edit 2
Nevermind. I just made a twig filter with html_entity_decode. Didn't wanna use that solution but couldn't find anything else.
I had the same problem, and as I see, lots of people are still having it.
After long researches, I solved with Convert Encoding this way:
{{ mytext|striptags|trim|convert_encoding('UTF-8', 'HTML-ENTITIES') }}
("trim" can be omitted for the post purpose, it was just used to have a cleaner string, in my case)
Working with Laravel - October CMS I solved this issue adding a charset metatag in each file, the parent with "head" closure and childrens without.

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 }}

HTML Formatting not working in symfony 2

I have a symfony 2 form in back-end where I have rich text field with tiny mce editor. The data type of this field is varchar in database table.
When I prints its content on front side then formatting does not work. It shows plain text rather than html formatted content.
How it prints:
< p >< strong >Hello< /strong >< /p >
How it should print:
Hello
Any solution?
To print a variable which contains HTML characters, as code string use escape (which is default in Symfony)
{{ content|escape }}
To print a variable which contains HTML characters, as a none code string use raw
{{ content|raw }}

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