Loop over alphabet in Liquid language - jekyll

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.

Related

Jinja non rendering blocks adds whitespaces to next rendering block

I have the following import on a jinja template that is rendered by ansible:
labels:
{% set name = 'jellyfin' %}
{% set port = '8096' %}
{% filter indent(width=8, first=True) %}
{% include './labels.yml.jinja' %}
{% endfilter %}
As you can see, there are some non rendering blocks to set some local variables and stuff. When the template is rendered by ansible, the first row of the template is shifted by the same amount of spaces that the non rendering blocks sum + 2 (32 in this case = 5 blocks with 6 spaces + 2).
For example:
labels:
# this are the labels, fools
- "traefik.enable=true"
- "traefik.backend=jellyfin"
I can remove the indentation of ALL the {% %} blocks, and then it will work properly, but looks ugly and off place.
Even if I add a minus sign to remove whitespaces to the non rendering blocks like this:
{% set name = 'jellyfin' -%}
{% set port = '8096' -%}
{% filter indent(width=8, first=True) -%}
{% include './labels.yml.jinja' -%}
{% endfilter -%}
The first line is still shifted (albeit much less)
How can I fix this?
I just had to add #jinja2: lstrip_blocks: "True" to the very top of the importing template, and that did the trick.
I gathered that information from this article, explaining my exact problem and showing a solution: https://radeksprta.eu/posts/control-whitespace-in-ansible-templates/
But I have to say that the default behaviour is very confusing.

How to execute text from a flask form as if it were html [duplicate]

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!

Jeykyll, what is the difference between {%- do-someting -%} and {% do-someting %}?

In the Jeykll in documentation to Liquid I found the notation {% … %} but in some plugins or example code snippets I saw also {%- … -%} (with a dash after and before percentage symbol). What is the right usage?
a) {%- include header.html -%}
b) {% include header.html %}
Or is it even dependent from the command / option / function which I am using inside the block?
The dashes give you the ability to control the whitespace around your tags. This often isn’t necessary for HTML generation, but can come in handy for certain uses in pre-formatted text. Or, if you are just picky about what your final HTML looks like ;)
Check out the docs here: https://shopify.github.io/liquid/basics/whitespace/

Sublime text multiple language syntax highlight?

Is there something like multiple language highlight syntax in Sublime Text? For example my code might look like this:
{% extends "template.html" %}
{% block content %}
{% if task == 'archimed_spiral' %}
<p>
$\frac{2}{3}$
</p>
{% elif task == 'gcd' %}
{% endif %}
{% endblock %}
Which is LaTeX inside html inside Jinja2. It gets pretty hard to read it properly.
Note
I know about Jinja2 package for Sublime, so it does highlight Jinja2 + html. Maybe I am just asking for too much..
Yes you can embed syntax highlighting. You basically define a start and end pattern, then include an existing scope. See Sublime Text 2: Different language highlighting based on context? (a la Webstorm).

Highlighting multiple lines

I am using pygments for syntax highlighting and want to highlight some lines in the code. This works fine for highlighting one line but how would I highlight multiple lines?
I have tried comma and space seperated lists to no avail.
{% highlight python hl_lines=7 %} # works
{% highlight python hl_lines=7,8 %} # does not work
{% highlight python hl_lines=7 8 %} # does not work
{% highlight python hl_lines=7-8 %} # does not work
Use a string and separate the line numbers by a space.
{% highlight python linenos hl_lines="1 3 4" %}
def say_hi(to_who):
print "Hello,", to_who
say_hi("World")
{% endhighlight %}
I'm not familiar with using just a {% highlight %} tag by itself. I always use it in combination with {% endhighlight %} to identify code blocks. This works as expected on my Jekyll 0.12.1 install.
{% highlight python %}
def say_hi(to_who):
print "Hello,", to_who
say_hi("World")
{% endhighlight %}
With the default CSS in place (and tweaked a little for the background color), the above produces this: