Turn off escaping in Symfony 2 / twig - symfony

I'm creating a form using the form builder in Symfony. I am adding an attribute into the twig file for my checkboxes which contains an ampersand and pound sign, Symfony is automatically escaping the ampersand which stops it displaying correctly. Is there anyway to turn off escaping on a per case basis in either the twig file or the controller, or switch it off completely in the config?
{{ form_widget(form.checkbox, { 'attr': {'data-icon-checkmark': '󰀦', 'data-icon-checkmark-checked': '󰀧'} }) }}
I have found a few topics on this for 1.X versions of Symfony, but nothing for 2.
Thanks!

Probably what you need is the raw tag or filter. Also take a look at the autoescape tag.
To turn autoescaping off globally, set the autoescape option to false in config.yml:
twig:
# ...
autoescape: false

You can use |raw filter.
http://symfony.com/doc/current/book/templating.html#output-escaping-in-twig

Related

Is it possible to set a new entity directly from twig?

Sometime i need to check if an user can comment from a voter, obviously at this point of the code the comment does not yet exist, but still i need to check if the user is allowed to comment or not.
From the controller i would usually do $this->isGranted('create', new Comment()) but how i'm supposed to do the same thing in twig? I can't find a clear explaination, and i obviously can't do something like {% if is_granted('create', new comment()) %}.
Is there a way to do this without hacking an object from the controller such as rendering a new Comment() from the controller? Doing so will allow me to avoid a lot of spaghetti code in my voters.
you can extend twig with Symfony Twig Extensions
Look at this documentation link:
Symfony Twig Extensions
Inside your twig you can use is_granted like this:
{{ is_granted(role, object = null, field = null) }}

How to get the field_image path with Twig in Drupal 8

To make an animated slider, I overwrite a fields view template file in my own template named: views-view-fields--slider.html.twig
Inside that file I have access to a fields array to print out all the fields that are in my content-type slide.
The problem is that I need to get the path of the image file instead of the image itself, because I need the path for CSS background-image styling.
The following doesn't work:
<div class="jumbotron " ref="" style="background-image:url(?????);">
{{ file_url(fields.field_image.entity.fileuri) }}
</div>
I tried everything I good find on Google:
fields.field_image.entity.uri
fields.field_image.entity.uri.value
fields.field_image.entity.url
fields.field_image.entity.url.value
etc.
Has anyone an idea how it is done in Drupal 8 ?
Since you are in views-view-unformatted you should already have access individualy to each field of your content type Slide with {{ row.content }} and because of that you have to do:
{{ file_url(row.content.field_image.entity.fileuri) }}
Try {{ file_url(row._entity.field_image.entity.uri.value) }}.
It works for me.
<div style="background-image:url({{ file_url(row._entity.field_image.entity.uri.value) }});"></div>
I found this module: image_url_formatter, so in View interface, I can use this to formatter my image filed as path.
It works perfect, but if I turned the twig debug on, it wouldn't work, because it will output debug annotation. I don't know how to solved it yet.
I use fields.field_image.content to show the path, I don't konw whether it's rignt.
Solution without extra module:
In your Views:
1.Add your image filed to the Advanced/Relationship
2.Then in the filed section, change to File Group, you can see URI field,add it, and make sure you check "Display the file download URI"
3.Add your new field in the twig template: {{ field.uri.content }}
Done!

How to trigger a Twig Extension function from a twig variables content

I want to be able to pass into twig some html in a variable and render it as
{{ data.content | raw }}
But within that content variable I would like to be able to have content like this - pulled from a database:
<div>
<p>Text with a Twig Extension function call eg: {{ doSomething('112233') }}</p>
</div>
Is there any way to get that twig extension to fire having come from the Twig variable itself?
Thank you.
There is one possible solution. Look at template_from_string twig function. It says:
New in version 1.11: The template_from_string function was added in Twig 1.11.
The template_from_string function loads a template from a string:
{{ include(template_from_string(data.content)) }}
But doc also says:
The template_from_string function is not available by default. You must add the Twig_Extension_StringLoader extension explicitly when creating your Twig environment.
So, probably it won't work out of box.

Adding a link inside translated content in Twig template

Inside a Twig template I would need to have a translated text that contains a link (the path should be generated by the Router, not statically embedded).
Twig does not allow to render a variable inside a trans block - I'm also aware of the following:
{% trans with {'%name%': 'Fabien'} from "app" %}
Hello %name%
{% endtrans %}
but I can't see how to use that to inject inside the translation a piece like this
privacy policy
(of course, the anchor text should be translated as well)
The approach I've taken is this:
In the translation file:
page.privacy.policy: Please read our %link_start%privacy policy%link_end%
In the twig file:
<p>{{ 'page.privacy.policy' | trans({'%link_start%' : '', '%link_end%' : ''}, 'account') | raw }}</p>
I'm not sure if this can be done using the block syntax you mentioned above as I found it didn't work unless I piped the result of the translation through the 'raw' filter. Also I use message domains to split the translations, hence the 'account' parameter.
I think the closest to your example would be:
<p>{{ 'Please read our %link_start%privacy policy%link_end%' | trans({'%link_start%' : '', '%link_end%' : ''}) | raw }}</p>
EDIT:
The only issue with this approach I've come across is where I need to programmatically follow a translated link in a unit test as there isn't a single translation representing the link text. Although messy I think it would be possible to get round this by providing a separate translation for the link text and substituting it's translated value into the full text as an additional variable.
rebdirdo's solution is not really safe as it's not escaping whole message. It is not working correctly for messages like "don't use <b> tag, use <strong> tag instead. %link_start%Here%link_end% you can find why.", because the tags will be not escaped and will be not visible.
working approach:
translation file:
advises.strong: don't use <b> tag, use <strong> tag instead. %link_start%Here%link_end% you can find why.
twig file:
{{ 'advises.strong'|trans|nl2br|replace({'%link_start%': '', '%link_end%': ''})|raw }}
Note the nl2br filter. It is necessary to put some filter there to make the raw filter working only for the link tags.
This is a better way:
{{ 'Please read our %privacy_policy%'|trans({
'%privacy_policy%': ' ' ~ 'Privacy Policy'|trans ~ ''
})|raw }}
Twig:
{{'body.term'|trans('%link_terms%' :app.request.getSchemeAndHttpHost()~path('terms')},'AcmeTerm')|raw }}
AcmeTerm.yml
body
term: >
<ul>
<li>ReadTerms.</li>
</ul>
where path('terms') is the route
like:
it__RG__terms ANY ANY ANY /it/termini-e-condizioni
en__RG__terms ANY ANY ANY /en/terms-and-conditions

How to reference twig for custom field types in dedicated bundle?

I am (still) trying to introduce http://xoxco.com/clickable/jquery-tags-input into a dedicated bundle. As far, I have a type as a child of text and a data transformer that converts comma-separated strings into arrays of Objects and vice versa.
Now I want to decorate the text field with the JQuery code linked above. As far as I understand, I have to define a block like
{% block manytomanycomboselector_widget %}
{% spaceless %}
{{ block('text_widget') }}
<script>
$(function(){
$("#{{ id }}").tagsInput();
});
</script>
{% endspaceless %}
{% endblock manytomanycomboselector_widget %}
in [MyTypeBundle]Resources/views/Form/fields.html.twig
Now, both the documentation and the answers for this question at StackOverflow state that I have to reference fields.html.twig somewhere either in the template that uses the form or in app/, but this doesn't seem to be necessarily for other field-type bundles, though I cannot see in their code why.
What do I have to configure inside the bundle besides this block in this file?
Also I didn't get where I have to put the css and js requirements for the header and how I deal with general requirements like jQuery itself.
I have the same issue & I resolve it by merging my field template in the twig.form.resources parameter.
So, in the DI extension of my bundle (MyBundle/DependencyInjection/MyBundleExtension.php), I add:
$container->setParameter('twig.form.resources', array_merge(
array('MyBundle:Form:field_widget.html.twig'),
$container->getParameter('twig.form.resources')
));
Be aware, your bundle must be registered after the TwigBundle in your AppKernel.
EDIT:
A form field is not linked to any JS or CSS. So, IMO, you have 2 solutions.
Firstly, you directly wrap your JS & CSS in your field template and your bundle stays stand-alone.
Secondly, you instruct final users that they need to include manually some JSS & CSS each time they use your field type.
The IoFormBundle & GenemuFormBundle uses the second solution like explain in their documentation.

Resources