Symfony2 turn autoescaping off in php template - php

There is way to turn autoescaping off in twig template in Symfony2. Like so:
{% autoescape false %}
{{ child.vars.label }}
{% endautoescape %}
How can I do something like this in PHP template? I got expanded choice type in my form, and I need to use images as labels. But Symfony keeps escaping my img tags.

Edit:
Sorry for misunderstood, but php templates have no autoescaping. If you want escaping in php templates you must do:
<?php echo $view->escape($var) ?>
Original answer:
In Twig use raw filter - http://twig.sensiolabs.org/doc/filters/raw.html
{{ child.vars.label | raw }}

Related

Not escaping HTML tags in twig so they display as markup

Quick question for everyone,
Is there something i am missing, I have not been able to find the correct answer to this, or i am reading the results i find wrong.
Bascially, I have a variable in php
ex $var="<b>#Something#</b>";
And i render it via TWIG like
{{ var }}
I don't want it to actually render <b>#Something#</b>,
But i want #Something#
Any ideas where i am failing at the simple task?
You can try raw method of twig
{% autoescape %}
{{ var|raw }} {# var won't be escaped #}
{% endautoescape %}
OR set false to autoescape
{% autoescape false %}
{{ var }}{# var won't be escaped #}
{% endautoescape %}
Reference: https://twig.symfony.com/doc/2.x/filters/raw.html
The default behaviour in Twig is to escape the vars before displaying them.
If you don'y want the var to be escaped, you have to use the raw filter
{{ var|raw }}
see the doc here

How to translate partial/variable in partial in OctoberCMS with rainlab.translate plug-in?

Just started using OcteberCMS and now trying to figure out how to translate with rainlab.translate plug-in partial (no, I can't use here {{ ''|_ }} for some reason).
For example, code of layout:
{% partial "footer" %}
I have 2 files in "partials" dir:
footer.htm and footer.fr.htm
but always footer.htm including, but not footer.fr.htm when I'm switch language to fr.
Or maybe is there some way to pass translated variable to partial?
{% partial "sidebar-contacts" city="Vancouver" country="Canada" %}
{% partial "sidebar-contacts" city="{{ 'Vancouver'|_ }}" country="Canada" %}
Thanks in advance.
It's very simple: just pipe variable |_ into translate plug-in like this:
{% partial "sidebar-contacts" city="Vancouver" country="Canada" %}
In file sidebar-contacts:
<div class="sidebar-contacts">
{{ city|_ }}, {{ country|_ }}
</div>

Twig not rendering HTML tags

I am using twig as templating engine and i am facing issue while displaying HTML data.
I searched on SO and got following solution
{% autoescape true %} {{ detailArticle.artdesc|raw}} {% endautoescape %}
This expression is working on my localhost but giving problem on cPanel the is Live server.
It is not rendering the output. It is displayed as
<span style="font-size: 12pt; font-family: 'Times New Roman', serif">.. so on
Twig version used is
"twig/twig": "~1.16",
Please suggest
You're using autoescape, which buffers the contents of that block, and then filters it (escaping HTML entities and so on). If you want to print out a variable that contains markup, use either this:
{{ detailArticle.artdesc|raw }}
Printing the value as a raw string (no escaping at all), or:
{% autoescape false %}
{{ detailArticle.artdesc }}
{% endautoescape %}
Which is the same as using raw on all variables you're using inside that block

Including id element, or variable, from another TWIG template

I have picked up the basics of using TWIG to create my site. I suppose I know how to use {%extend%} {%block%} {%include%} and {%set%}, in the most general sense.
I would like to include a block of code from within another twig file without including the whole file - as opposed to {% include 'file.twig' %}.
I have figured out how to set a variable in file.twig and output it using {{ variable | raw }}. I would like to do that in another file, like you would with using jQuery's .load function.
I swear the twig documentation does not seem to touch on this, it seems like really obvious and basic functionality. I have messed around with various combinations of include, for, with, in and only, colons and commas and whatever | is, and nothing.
I believe you are looking for horizontal inheritance via the use tag:
The use statement tells Twig to import the blocks defined in blocks.html into the current template (it's like macros, but for blocks)
The confusing part is that by itself, {% use ... won't actually insert the content of the blocks in the referenced template. To do that, you must use the block(...) function:
index.twig
{% use "blocks.twig" %}
{{ block('name') }}
blocks.twig
{% block name %}
<h1>Alex Weissman</h1>
{% endblock %}
{% block hobby %}
<p>Blanchin'</p>
{% endblock %}
For a working example, see my TwigFiddle (yes, it's a real thing!): http://twigfiddle.com/jjbfug

Disabling autoescape using symfony and form_widget()

Using Symfony 2, I'm trying to print out a label by using
form_widget(form.client)
The label I'm trying to print is containing actual HTML markup:
$builder->add('client', 'text', array('label' => 'Client'));
But Symfony autoescapes the label and prints it out as text and not as HTML. So I've tried to use both
form_widget(form.client) | raw
and
{% autoescape false %} form_widget(form.client) {% endautoescape %}
but none of those two methods helped me, the output is still regarded as text and not HTML.
Am I missing some setting or am I using it wrong?
Thanks!
Don't put HTML into your classes! Use Custom Form rendering to customize how your labels are rendered.
I think Twig filters should be inside the parenthesis and not outside - have you tried like this :
form_widget(form.client | raw)
Also, I would recommend adding the tag not in the class but directly in the template :
{{ form_label(form.client) }}

Categories