Symfony2 : Auto htmlentities using Twig - symfony

I'm displaying some variable retrieved in my database using Twig :
<p>{{ my_variable }}</p>
The thing is this variable may contain html tags, such as "<br />".
Twig seems to automatically call some htmlentities-like function when displaying variables.
Is there any way to disable it so that when I display a variable containing "Hello<br />world !" I get :
Hello
world !
rather than :
Hello<br />world !
Thanks

Use {{ my_variable|raw }} to prevent my_variable from being automatically escaped.
See Twig documentation: http://twig.sensiolabs.org/doc/filters/raw.html

Try using this
{% autoescape false %}{{ my_variable}}{% endautoescape %}

even better: {{ '<br />|raw('html') }} to avoid unescaping other sensible stuff.

If you just want to use linebreaks in the text stored in your database but don't care to use html , you can also use the nl2br filter as in {{ var|nl2br }}. Allows you to use string linebreak character \n in your text. Filter converts it to <br/>

Related

Is it possible to pass twig variables into templates?

I'm using the Timber plugin for my Wordpress site. For a little button component, i'm trying to pass the variable {{ site.link }} to use as my href.
Like so:
{% include 'component/icon-button.twig' with { href: '{{ site.theme.link }}/faq' } %}
icon-button.twig
<button class="icon-button" data-href="{{ href }}">text</button>
But that only results in {{ site.link }}/faq being output as is, as a string, not the actual URL.
How would i do this?
That's just because when you use include in twig you don't need to use double {{ again. The correct syntax would be:
{% include 'component/icon-button.twig' with { href: site.theme.link ~ '/faq' } %}
The "~" is used for string concatenation in twig. You were passing the whole thing as a string and nothing more =)

Translate the value of twig variable

Is it possible to translate the value of twig variables in a template with the 'trans' tag?
Say for instance I am passing a product to my template. This product has a definition with a trans tag e.g {{ product.definition|trans }}. This definition could either be in EN or DE or some other language. How could I translate the definition.
What are you trying to do is not a good way, It would look like this:
messages.en.yml
product:
definition:
some_value1: Some value 1
some_value2: Some value 2
and in template, you would do something like this:
{% set definition_value = product.definition %}
{% set trans_definition = 'product.definition.' ~ definition_value %}
{{ trans_definition|trans }}
it'll work, if it finds the key. What if it cant find it?
That's why you should use DoctrineBehaviors from KnpLabs, which handles all the dynamic translations for you..
If {{ product.definition }} equals 'cellphone' the following should work.
message.language.yml:
'cellphone': This will work!
However if you want to map it with the 'product' key in your message file like this:
product:
'cellphone': This also works
add the key to the twig template like so:
{{('product.'~product.definition)|trans }}

Keep leading 0 in Twig

In my database, I have a field containing the following data : 000010 (the type is integer)
In my twig tpl, I want to display it, then I do : {{ spending.documentName }}
But the browser displays "10". As if Twig was automaticcaly performing a trim on my data.
I tried {{ spending.documentName|raw }} but it doesn't work. I dont find anything on Google about how to keep leading 0 with Twig.
Does anyone know how to proceed ?
I think you must force the format (as your type is integer).
You can use the format filter :
{{ "%06d"|format(spending.documentName) }}
Or better create a twig extension (http://symfony.com/doc/current/cookbook/templating/twig_extension.html):
public function strpad($number, $pad_length, $pad_string) {
return str_pad($number, $pad_length, $pad_string, STR_PAD_LEFT);
}
It's clearer in your template :
{{ spending.documentName | strpad(6,'0') }}
You need to use the format filter.
Since placeholders follows the sprintf() notation, you should be able to convert sprintf('%06d', $integer); in PHP to {{ '%06d'|format($integer) }} in Twig.
Kinda late here, but ran into the same.
With twig 3.x you can use:
{{your.number | format_number({min_integer_digit:'2'})}}

Symfony2 twig translate string containing colon

How can I translate string containing colon in symfony2 using twig? Here's what I have:
Inside twig template:
{% trans with {'%order_no%': order_no} %}Loading offer: %order_no%...{% endtrans %}
Inside translation file:
Loading offer: %order_no%...: Įkeliamas užsakymas %order_no%...
As expected this doesn't work. How can I make it work?
You need to use quotes around the text as such:
'Loading offer: %order_no%...': Įkeliamas užsakymas %order_no%...
edited to show the correct example.

Pass variables to Twig Translator

In messages.en.yml:
variable_name: Welcome to %site_name% - %other_info%
In Twig
{{ 'variable_name'|trans( -here- ) }}
It's the -here- part I'm struggling to know how to pass data in. I must do it this way (above is just an example).
Maybe I'm misunderstanding the question but if I understand correctly, you only need to give an array as the first parameter of the trans filter.
{{ 'variable_name'|trans({'%site_name%': 'My Website'}) }}
And of course, values can be variables if you don't put them between quotes :
{{ 'variable_name'|trans({'%site_name%': 'My Website', '%other_info%': page_name}) }}

Resources