How to replace a string with linebreaks in Jinja2 [duplicate] - html

This question already has answers here:
Passing HTML to template using Flask/Jinja2
(7 answers)
Closed 6 years ago.
I have some data in jinja2 like this
'item1|item2|item3'
And I want to turn it into rendered linebreaks. However, when I replace it with br/ tags, I get the br tags rendered on the page. So
{{ 'item1|item2|item3' | replace("|", "<br/>") }}
renders as
item1<br/>item2<br/>item3<br/>
When I want
item1
item2
item3
on my page. I feel like I'm missing some obvious trick here...

This has to do with autoescaping. Solution that worked for me was:
{% autoescape false %}
{{ 'item1|item2|item3' | replace("|", "<br/>") }}
{% endautoescape %}

Related

how do I fix the twig filter from affecting html tags inside?

I'm learning Drupal8 and Twig with Chaz Chumley's book 'Drupal 8 Theming with Twig'.
When I put in the code provided I don't get the desired result. (Chapter 3, Filters)
The book says to add the following to the page.html.twig file:
{% filter upper %}
<p>{{ name }} is the best cms around.</p>
{% endfilter %}
but the page outputs
<P>DRUPAL IS THE BEST CMS AROUND.</P>
(Showing the html tags on the page as shown here)
Is there something I'm missing to have the twig filter not change the HTML tags? or is the only solution to put the filter inside the tag? but this filter is supposed to "wrap sections of HTML and variables" so why is it affecting HTML tags?
You can put the filter around just the text, so it ends up as:
<p>{% filter upper %}{{ name }} is the best cms around.{% endfilter %}</p>
You can test your twig code here: https://twig.stapps.io/
Can you give this a try:
<p>{{ 'your text'|upper }}</p>
Also, check out https://drupal.stackexchange.com/ if you have any more drupal related questions.

Loop over alphabet in Liquid language

I'm creating a page with Jekyll, which has a built-in Liquid engine. I am familiar with some basic looping, but I can't get this to work.
{% for letter in (65..90) %}
{{ letter }}
{% endfor %}
What I want is the output to look like
A
B
...
Z
But instead I see
<p>65</p>
<p>66</p>
...
<p>90</p>
What I need is a way to convert the ASCII numbers to letters in Liquid, and I need the output to not have <p> tags around each thing generated by the loop.
How can I get Liquid to output something like that second code block?
Okay, I figured it out with some help from this.
What I really need is
{% assign alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" | split: "" %}
{% for letter in alphabet %}
{{ letter }}{% endfor %}
I've tried putting the alphabet in the for loop declaration, but it only seems to work if the alphabet is previously assigned.
The complication with <p> tags was because I'm doing this in a .md document, and the loop ends up putting a newline between each link, which gets interpreted in markdown as new paragraphs. Putting the endfor on the same line as the code gets rid of those newlines.

Repeat loop of Jekyll Data [duplicate]

This question already has answers here:
How do a loop n.times in Liquid templating using an existing for loop
(3 answers)
Closed 7 years ago.
Is it possible to repeat loops of data? Something like so:
{% for competition in site.data.competitions loop:3 %}
Which would loop and export the the data 3 times?
It would just save me making lots of fake data =)
Thanks!
I don't think that there's a Liquid filter to do it, but you can always put a for in a for:
{% for i in (1..3) %}
{% for competition in site.data.competitions %}
...
{% endfor %}
{% endfor %}

Wildcard match in Liquid tag?

I'm trying to filter out part of a post from my site's RSS feed. I want the rest of the post, but just want to remove certain parts.
Is there a way to filter this out somehow? I've looked at these Liquid tags/filters, and the closest I've found were remove and replace, but they seem to match literally.
I'm trying to basically do this in my feed.xml:
{{ post.content | replace:'<span class="no-rss">*</span>','' | cdata_escape }}
Is there some way to use a wildcard in these filters, or achieve this otherwise that I'm not aware of? My Google-fu has run dry on this one.
Two solutions :
using excerpt with posts
Part of the post you want to inject in your feed is at the beginning of your post.
Use excerpt
isolating part of content with liquid filters
If you are in a page (they don't have excerpt), or in a post and want to select specific part of your content, you can use liquid filters.
string.split and array filters.
In any post or page
---
your front matter variables here
---
Head of my document
<!-- start -->
Text to extract
<!-- end -->
Bottom of my document
in your rss
{% for p in site.posts %}
{% if p.content contains "<!--start-->" %}
{% assign extract = p.content | split: "<!--start-->" | last | split: "<!--end-->" | first %}
{{ extract }}
{% else %}
{{ p.content }}
{% endif %}
{% endfor %}
Instead of removing the whole span you could get rid of the style class being applied to it:
{{ post.content | remove: ' class="no-rss"' | cdata_escape }}

Which is this code written in? [duplicate]

This question already has answers here:
HTML {% %} tags?
(4 answers)
Closed 8 years ago.
{% if booking.car_type %}<b>Car Type</b>: {{
I just want to know in which language is the code between the {% %} is written in
Thank you
Django templates ... for sure you must check in Google for much deeper knowledge.