I would like to output {# in html, but this is the beginning of a Jinja2 comment. This is not coming from a template variable. Just plain html in my template.
Any help ?
Try this...
{{ '{#' }}
http://jinja.pocoo.org/docs/templates/#escaping
Hope this helps!
Related
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
I tried using square brackets like this:
{{ [my_variable] }}
... which is not working. The square brackets are only working together with other text in the curly brackets:
{{ some_other_text.[my_variable] }}
Thanks!
You don't need the square brackets.
If your frontmatter looks like this:
---
my_variable: foo
---
Then you inject the variable like this: {{ page.my_variable }}.
Notice the page. prefix before the variable name.
For more information, look at the docs.
Solved it with a hook / plugin as described here:
https://github.com/gemfarmer/jekyll-liquify
http://acegik.net/blog/ruby/jekyll/plugins/howto-nest-liquid-template-variables-inside-yaml-front-matter-block.html
... still wonder why {{ [my_variable] }} is not possible in liquid/jekyll...
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
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>
I have the following code in a liquid template:
{{ sample.url }}
Coming from jinja2 I was hoping for something like this:
{{ sample.url|urlize }}
Does this exist in liquid?
I've seen the liquid documentation and couldnt find any reference to that, but as the documentation sayed you can write your own filter to do that.
Take a look here.