Dashes in jinja templates - jinja2

Found some of these in jinja files:
</extension>
{%- else -%}
<extension name="blabla">
And also
{% if freeswitch_dispatcher -%}
<extension name="hold_unhold">
See the dashes ? Any idea what it's for ?

Turns out that + and - are there for whitespace control purpose.
You can manually disable the lstrip_blocks behavior by putting a plus sign (+) at the start of a block
[...]
You can also strip whitespace in templates by hand. If you put an minus sign (-) to the start or end of an block (for example a for tag), a comment or variable expression you can remove the whitespaces after or before that block

It's for controlling whitespace within a block.
{%- by itself means current line should have no empty lines between current and previous line
-%} by itself means current line should have a single empty line above it
{%- and -%} means current line should be flush with previous line
You can experiment here:
http://jinja.quantprogramming.com
Additional links:
Documentation
Credit

Related

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/

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.

white space control in jekyll - why does {%- ... -%} a la liquid not work?

The liquid documentation states that
By including hyphens in your assign tag, you can strip the generated whitespace from the rendered template ... if you don't want any of your tags to output whitespace, as a general rule you can add hyphens to both sides of all your tags ({%- and -%}):
When I try in jekyll
{%- case key -%}
I get the error
Error: Liquid syntax error (line 139): Tag '{%- case key -%}' was not
properly terminated with regexp: /\%\}/
There are many posts about excessive whitespace in the jekyll generated html, for example Compressing Liquid generated code.
They all complain about dilute HTML output and discuss plug-ins as solution.
My simple questions are:
Why does {%- ... -%} not work in jekyll ?
Why behaves jekyll differently than the liquid documentation suggests
Jekyll < v3.5.0 use liquid v3.0.6.
White space control is only available in liquid v4 and this version will soon land in Jekyll.
As of 18 June 2017, Jekyll v3.5.0 has upgraded to Liquid v4. {%- ... -%} now works.

Automatically set starting line number from last one in Jekyll highlight code blocks

In my Jekyll3 blog, I use the highlight Liquid Tag to display syntax-highlighted code with fine line numbering. This is accomplished by doing:
{% highlight python linenos %}
# some code goes here
{% endhighlight %}
Now I often put several code blocks such as this in any given post. Let's suppose that the first code block has N lines, how can I have the second code block to start at line number (N+1)?
I know this can be done manually by providing the starting line value to the option startinline on the highlight tag, but I would want to accomplish this automatically.

How to support line number when using pygments with Jekyll

How can I number the code lines which are highlighted using pygments in Jekyll?
According to the Liquid Extensions wiki page of the Jekyll documentation, the highlight Liquid tag has an optional second parameter, which may have the value linenos to turn on line numbering:
{% highlight language linenos %}
your code here
{% endhighlight %}
Use it with caution. With linenos the line numbers are actually inserted in the code's text, so will be impossible to copy the code block without them. (This could be solved by letting the visitor to $('.lineno').toggle() the line numbers' visibility. Works in Firefox, not sure if is portable.)
Update: Better use linenos=table:
{% highlight language linenos=table %}
your code here
{% endhighlight %}
That will place the code in a table with two cells: first td all the line numbers, second td the code itself. This makes possible to select only the code, without the line numbers.