symfony2 twig escaping < character - html

I am confused.
Input : Sentence with <character
My code:
{{ Status.StatusStatus }}
Output:
Sentence with <character
Which is good... but i need to enable the "anchor html" like "
<a href='/app_dev.php/something/' class='showProfile green bold'>something </a>
So i modify the code as: {{ Status.StatusStatus|raw }} what fixed the anchor problem and now is "converted" - clickable but from that input i get that output:
Input : Sentence with <character
Output: Sentence with
Any ideas how to allow <something situation?

The answer is that you write your TWIG to have the HTML markup you need, and print varaibles where you need
{{ Status.StatusStatus }}

you can write your own twig extension to whitelist some html Elements:
https://stackoverflow.com/a/8026130/1847340

Related

How to match a hexadecimal value in liquid templates with regex?

Desired result
Content editors can enter hexadecimal values in Shopify for a background-color. How can you check with Liquid template's control flow if a given input is a valid hexadecimal?
The regex that needs to be implemented:
^#(?:[0-9a-fA-F]{3}){1,2}$
The Liquid snippet where it should be added:
<div class="a-module" style="background-color: {{ bg_color }};">
...
</div>
The variable bg_color may only be a hexadecimal including the pound # character.
How can above be achieved with regex?
Other solutions?
If this is not possible what are other methods to match a string in liquid templates?
Research
I could not find much about regex in liquid templates.
There is this issue: Regex for matching a tag in a Liquid template : ">" inside html tag
But I do not understand how it is to be implemented.
There is no regex in Shopify liquid, so there is no way to check it that way.
A better approach will be to use one of the color filters.
You can do something like this:
{% assign is_color = bg_color | color_to_rgb %}
{% if is_color %}
It's color
{% endif %}
Where if the color is valid it will return a rgb output, but if it's not it will return a null result making the variable false.
If you really need to use regex, the only option is to rely on Javascript and handle it once the DOM is ready.
What about create your own filter. And in this filter checking hexadecimal.
/^#[0-9a-f]{3,6}$/igm
https://github.com/Shopify/liquid/wiki/Liquid-for-Programmers#create-your-own-filters

How to add a line break to a post title?

---
layout: post
title: Markdown:<br>Style Guide
---
I expect this to produce
<h1>Markdown:<br>
Style Guide</h1>
But there are two issues. The colon breaks the Yaml. Hence I need to put " " around it.
---
layout: post
title: "Markdown:<br>Style Guide"
---
But what I can't solve is the line break in the title. It displays <br> instead of actually breaking the line. Is there any way?
You probably use a default Jekyll template which uses {{ post.title | escape }}.
If you remove the escape filter, all your html will be preserved.
It now reads : {{ post.title }} or {{ page.title }} depending on the context.

Django - <ul> content from db is not rendered as HTML, is pure text

Not sure what is wrong here. I have some html content in my tables.
Table column:
(ul)(li)one(/li)(li)two(/li)(li)three(/li)(/ul)
Template:
(div class="plst")
(p)Pros:(/p)
{{ review.plist }}
(/div)
Please replace the () with <>
In the output i am seeing
(ul)(li)one(/li)(li)two(/li)(li)three(/li)(/ul)
as such and not
onetwothree
Don't know what is happening here. Thanks for your time.
You need to apply the safe template tag filter.
<div class="plst">
<p>Pros:</p>
{{ review.plist | safe }}
</div>
The safe filter turns off escaping of <, >, and &. With escaping off, they are passed straight through to the output.

Django passing HTML objects into template as plain text

This feels like a really silly issue, but I'm confused by the behavior of django-zinnia, a blog creation module.
When I test enter a plain text post, it appends each sentence with html < p > tags the browser doesn't read as html.
Example, if I enter this into the database (no html):
The entry from the db renders on page itself like this as if the < p > markup was plain text:
Within Zinnia, these html tags are being generated as part of the {{ object_content }} object in _entry_detail_base.html
<div class="entry-content">
{{ object_content }}
</div>
I've looked through the entry.py models within Zinnia and I'm having trouble identifying where these tags are coming from or how they're being passed in in a way the browser doesn't interpret them for what they are (html). Is there a filter I can apply that might solve this? thanks
That's the default behavior for Django templates. Use {{ object_content|safe }} or {% autoescape off %} {{ object_content }} {% endautoescape %} (for multiple variables) to prevent html entities from being escaped.
Note that using the safe filter doesn't automatically mean the output is not escaped if you use another filter after it.
Check the Zinnia's source code: https://github.com/Fantomas42/django-blog-zinnia/blob/master/zinnia/templates/zinnia/_entry_detail_base.html
It's using |safe template tag:
<div class="entry-content">
{{ object_content|safe }}
</div>

symfony2 twig whitelist html tags

I pass a variable to my twig template in Symfony2, this variable may contain <br /> html tags, I have tried to create an extension (function), but the variable still gets escaped.
How can I output a twig variable that allows the <br /> tag? Is there a simple solution to just allow a whitelist of allowed tags in certain templates?
I've searched about twig sandboxes, but I'm not sure if that is my solution.
edit: I still want the variable to be escaped, but to allow exclusively the <br /> tag.
Actually, you can use native PHP function strip_tags by following:
{{ var|striptags('<br>')|raw }}
you can allow multiple tags with following code:
{{ var|striptags('<br><p>')|raw }}
You can do like that :
{{ text | striptags('<p><b><br') | raw }}
For instance,
<br>
won't escape
<br> and <br />
and
<p>
won't escape
<p> and </p>
etc.
Initially I thought it should be possible to write custom escaper strategies so you could do something like this:
{{ var|escape('html-custom') }}
Unfortunately it's not the case. Only available strategies are html and js. They're hard coded in the twig_escape_filter() function defined in a Twig_Extension_Core class file.
It seems that your only option is to write custom estension with a new filter:
{{ var|raw|customescape }}
Here's an example of custom twig extension and how to register it in Symfony: Symfony2 Twig extension
{{ var|striptags('<br>')|raw }}
works fine, but I don't know how to pass an array to the strip_tags php function with this twig filter.
both
{{ var|striptags(['<br>', '<b>'])|raw }}
and
{% set allow = ['<br>', '<b>'] %}
{{ var|striptags(allow)|raw }}
throw an "Array to string conversion" exception during the rendering of a template.
Be also carefull that strip_tags php function doesn't escape html attribute like "onclick".
{{ var|nl2br }}
and/or
{{ var|raw|nl2br }}
nl2br reference