I have a page say index.html and a file with liquid code called tags. I want to pass a parameter which I specify in index.html to a liquid call in tags. Namely,
index.html
<div>
{% include tags param="site.categories" %}
</div>
Tags
{% assign tags_list = {{ include.param }} %}
...
The {% assign tags_list = {{ include.param }} %} does not work for some reason. Is it possible to do so because this allows me to use tags file for multiple purposes? Instead of writing liquid calls in every page Iwould be able to just do {% include tags param="something" %}. Thanks.
Well, {% include tags param="site.categories" %} is not passing the site.categories hash but the "site.categories" string.
The right syntax is :
{% include tags.html param=site.categories %}
Related
I'm building an admin for Flask and SQLAlchemy, and I want to pass the HTML for the different inputs to my view using render_template. The templating framework seems to escape the HTML automatically, so all <"'> characters are converted to HTML entities. How can I disable that so that the HTML renders correctly?
To turn off autoescaping when rendering a value, use the |safe filter.
{{ something|safe }}
Only do this on data you trust, since rendering untrusted data without escaping is a cross-site scripting vulnerability.
MarkupSafe provides Jinja's autoescaping behavior. You can import Markup and use it to declare a value HTML safe from the code:
from markupsafe import Markup
value = Markup('<strong>The HTML String</strong>')
Pass that to the templates and you don't have to use the |safe filter on it.
From the Jinja docs section HTML Escaping:
When automatic escaping is enabled everything is escaped by default
except for values explicitly marked as safe. Those can either be
marked by the application or in the template by using the |safe
filter.
Example:
<div class="info">
{{data.email_content|safe}}
</div>
When you have a lot of variables that don't need escaping, you can use an autoescape override block:
{% autoescape false %}
{{ something }}
{{ something_else }}
<b>{{ something_important }}</b>
{% endautoescape %}
For handling line-breaks specifically, I tried a number of options before finally settling for this:
{% set list1 = data.split('\n') %}
{% for item in list1 %}
{{ item }}
{% if not loop.last %}
<br/>
{% endif %}
{% endfor %}
The nice thing about this approach is that it's compatible with the auto-escaping, leaving everything nice and safe. It can also be combined with filters, like urlize.
Of course it's similar to Helge's answer, but doesn't need a macro (relying instead on Jinja's built-in split function) and also doesn't add an unnecesssary <br/> after the last item.
Some people seem to turn autoescape off which carries security risks to manipulate the string display.
If you only want to insert some linebreaks into a string and convert the linebreaks into <br />, then you could take a jinja macro like:
{% macro linebreaks_for_string( the_string ) -%}
{% if the_string %}
{% for line in the_string.split('\n') %}
<br />
{{ line }}
{% endfor %}
{% else %}
{{ the_string }}
{% endif %}
{%- endmacro %}
and in your template just call this with
{{ linebreaks_for_string( my_string_in_a_variable ) }}
Use the safe filter in your template, and then sanitize the HTML with the bleach library in your view. Using bleach, you can whitelist the HTML tags that you need to use.
This is the safest, as far as I know. I tried both the safe filter and the Markup class, and both ways allowed me to execute unwanted JavaScript. Not very safe!
In one of my posts I added this custom variable:
exclude-from-indexes: true
In the layout for the category this post appears in, I have:
{% for post in site.categories[page.category] %}
{% if post.exclude-from-indexes!=true %}
<li>{{ post.title }}</li>
{% endif %}
{% endfor %}
However, the page is still included.
If I add:
{{ post.exclude-from-indexes }}
The variable is not shown.
I have tried different key names, but this doesn't appear to work. If I do:
{{ post | inspect }}
The key is not shown.
Am I accessing or declaring the custom variables in the wrong way? Or am I accessing some strange type of "post" that doesn't have the custom variables?
I noticed in another page, if I do:
{% for post in site.posts %}
{{ post.exclude-from-indexes }}
{% endfor %}
The variable is shown. So I think it's something to do with the data stored in site.categories[page.category]...
Turns out it was the keyname. Using:
excludefromindexes=true
Fixes it.
I have blog posts to which I add tags in the YFM like this:
tags: amp
I want Liquid to check whether the tag == amp and if so, to add a link in the blog-post.html layout file. I tried the code below:
{% if page.tags == "amp" %}
link
{% endif %}
But nothing gets outputted
The tags attribute in the YFM should actually be stored as an array since there can be multiple tags, as seen in the docs.
tags: [amp, foo, bar]
When checking the tags, use the contains liquid filter.
{% if page.tags contains 'amp' %}
link
{% endif %}
I can use django html logic (not sure what it is called) to post something on the page if the url is exactly something (see below). How do I get the if statement to be true for all child pages. E.g. if the path is '/chat/', then '/chat/1/1', '/chat/members', etc. Is it possible to use the name space somehow?
HTML
{% if request.get_full_path in "/chat/" %}
<p>found it</p>
{% endif %}
URL Namespace
url(r'^chat/', include("chat.urls",namespace='chat')),
App URL Namespace
url(r'^$', chat_view, name='chats'),
You can do it like this!
{% url 'chat:chats' as first_url %}
{% if first_url not in request.path%}
<p>You are not in the chats namespace</p>
{% endif %}
I would like my outermost Liquid template file to reference a JavaScript file only when the page requires it. I created a page variable, page.slideshow, which controls this:
default.html:
{% if page.slideshow %}
<script type="text/javascript" src="/js/slideshow.js"></script>
{% endif %}
Now I have a template that "inherits" from default.html, which allows the page to specify in its YAML frontmatter a list of images (page.images). Only when this list has more than one element do I want slideshow.js to be included in the page.
My attempt at this looks something like:
other.html
---
layout: default
---
{% assign imagecount = page.images | size %}
{% if imagecount > 1 %}
{% assign page.slideshow = true %}
{% endif %}
However, this does not seem to affect the rendering of default.html, because the script tag is not included in the final page.
As a workaround, I can modify the frontmatter of each page that lists multiple images to explicitly set slideshow: Yes, or change default.html to check page.slideshow or (page.images | size) > 0 but I'd prefer to structure it closer to what I attempted above.
I've had a play about and you can't really do it the way you've suggested. It seems page variables just cant be modified once they're set. You might be able to set it with Jekyll plugins, but that's a whole other thing.
The cleanest way I have found to do it is to put all the logic in your main, outermost template.
{% assign imageCount = page.images | size %}
{% if page.slideshow or imageCount > 1 %}
{{ page.images | size }}
<script src="/js/slideshow.js"></script>
{% endif %}