(Django) line breaking in template goes to a space - html

For simple template
{% with var1="super" var2="man" %}
<p>
{{ var1 }}
{{ var2 }}
</p>
{% endwith %}
gives super man but I want superman.
{% spaceless %} does not work for this case (between two strings, not two tags.)
What is the solution? Making {{ var1 }} and {{ var2 }} in one line is actually too long in my code.

The solution is simple, just remove the enter character:
{% with var1="super" var2="man" %}
<p>
{{ var1 }}{{ var2 }}
</p>
{% endwith %}
But if you don't want to make the code as you said "long" ( I don't know the reason :) ), you can combine the variables two by two and merge them and so on.
Needless to say, as long as you have HTML file, it will interpret the enter character as a space in <p></p>, so your problem isn't really a django/python problem, because the problem is between the tags, not the tags themselves.

Look at what the "problem" is differently.
{% with v1=var1 %}
{% with v2=var2 %}
{{ v1 }}{{ v2 }}
{% endwith %}
{% endwith %}
Though technically less efficient here, in a lot of cases readability is more important than saving a few bits or cycles.

Related

Parsing links in include parameter text

My Jekyll template has a simple alert include:
<div{% if include.style %} class="uk-alert-{{ include.style }}"{% endif %} data-uk-alert>
<p>{{ include.text | markdownify }}</p>
</div>
I am trying to do something like this:
{% include alert.html style="warning" text="This article is for Administrators and other Roles. Learn more about [permissions]({% link _docs/permissions.md %}) and [roles]({% link _docs/roles.md %})." %}
The issue is the output is not parsed by markdown and I get the raw text:
This article is for Administrators and other Roles. Learn more about [permissions](/support/docs/permissions/) and [roles](/support/docs/roles/).
Tried adding this:
<div{% if include.style %} class="uk-alert-{{ include.style }}"{% endif %} data-uk-alert>
<p>{{ include.text | markdownify }}</p>
</div>
This sort of works but I get extra <p></p> before and after the markdown text and it adds padding.
So far this is what I have working with a capture:
{% capture alert_text %}This article is for Administrators and other Roles. Learn more about permissions and roles.{% endcapture %}
{% include alert.html style="warning" text=alert_text %}
Can I just just have the link generated inline direct or somehow eliminate the extra paragraph tags?
A single line of markdown is considered a paragraph, which is why it's being wrapped in those tags. I'd keep the capture to render out the liquid link tags, and just remove the p tags in the include:
<div{% if include.style %} class="uk-alert-{{ include.style }}"{% endif %} data-uk-alert>
{{ include.text | markdownify }}
</div>
If you're seeing the p tags added before or after, it's because an opening p tag implies the close of the previous one. If you're still having issues with whitespace on each end of your text, you could strip it before markdownify to be sure:
{{ include.text | strip | markdownify }}

Jekyll: Previous/Next navigation when using custom navigation

The official Jekyll tutorial has an entire section on using a YAML file to define a custom sequence of pages: https://jekyllrb.com/tutorials/navigation/
But it doesn't mention anywhere how one might create previous/next navigation buttons on the pages within that sequence, which is particularly ironic considering that the tutorial itself has them.
I've come up with some Liquid to determine the index of the current page:
{% for item in site.data.encore.docs %}
{% if item.url != page.url %}
{{ item.title }}: {{ forloop.index }}
{% else %}
<strong> This page index: {{ forloop.index }}</strong>
{% assign this_page_index = forloop.index %}
{% break %}
{% endif %}
{% endfor %}
but getting the index of the previous page via {% decrement this_page_index %} always returns -1 for some reason, and something like {% assign previous = this_page_index - 1 %} isn't valid Liquid. Same goes for trying to get the next page with similar methods.
What's the ideal way to accomplish this? I've searched every way I can think of and not found anything.
You can find the code for Jekyll's own navigation on their tutorials page by sifting through their GitHub repo until you get to their section_nav_tutorials.html, but it appears the way to do it is very close to what you have.
Liquid doesn't respect you doing math directly, you have to use a filter. For you, you'd use {% assign previous = this_page_index | minus: 1 %}.

jekyll blog title to include pagination variables

I am trying to get Page 2+ of my blog to have a different title for search engines to index.
I have read several other stackoverflow answers stating that you cannot use liquid tags in the front matter yaml. One suggested to use JS to update the title, however this will not work for me as I want the search engine to index the parsed title.
I thought there may be another way. I can perhaps create a HTML page for each of my pages. I would like to do that without having to manually add each one of my posts into each of the pages (resulting in an ongoing time consuming task each time I post a new article).
I was thinking I could make one page for 1-10, another page for 11-20, etc... Something like this:
---
title: Blog Page 2
---
{% for post in paginator.posts %}
{% if post.index > 10 %}{% if post.index <= 20 %}
<div class="post-preview">
<a href="{{ post.url | prepend: site.baseurl }}">
<h2 class="post-title"> {{ post.title }}</h2>
{{ post.excerpt }}
</a>
</div>
{% endif %}{% endif %}
{% endfor %}
It seems there is no post.index variable available. Is there anything similar I can use?
Or are there any other ways to achieve "Blog Page X" in my title?
Supposing that your head tags are in your _includes/head.html file. In the title tag just add :
{% if paginator %} - page {{ paginator.page }}{% endif %}
Your title tag now looks like this :
<title>
{% if page.title %}{{ page.title }}{% else %}{{ site.title }}{% endif %}
{%if paginator %} - page {{ paginator.page }}{% endif %}
</title>

Jekyll code in jekyll

I'm creating a bird's eye view tutorial for Jekyll, to be hosted on Github pages (on my blog that runs on Jekyll). So, I want to put some code there. If I put the following:
{% for post in site.posts %}
{% if post.categories contains '<categoryname>' %}
<h2>
{{ post.title }}
</h2>
{% endif %}
{% endfor %}
(all lines after tabspaces), it doesn't render as code, rather, it executes. How do I stop it from executing and render it as code?
The {%...%} syntax used by Jekyll is part of the Liquid templating engine. To escape these tags, and so show them literally, you should use the raw tag.
You will probably want to combine this with the markdown syntax for code blocks. With Redcarpet you can use the triple backtick syntax. It doesn’t matter if you put the backticks inside the raw tags or the other way round:
{%raw%}
```
{% for post in site.posts %}
{% if post.categories contains '<categoryname>' %}
<h2>
{{ post.title }}
</h2>
{% endif %}
{% endfor %}
```
{%endraw%}
Enclose your code in backticks:
(tested with redcarpet markdown engine)
```
{% for post in site.posts %}
{% if post.categories contains '<categoryname>' %}
<h2>
{{ post.title }}
</h2>
{% endif %}
{% endfor %}
```
There are at least three options exist, taht you can use to format code in Jekyll:
highlight - Jekyll has built in
{% highlight java %}
ValidationResult validationResult = NetLicensing.LicenseeService.validate(context, licenseeNumber);
{% endhighlight %}
example: https://raw.githubusercontent.com/Labs64/netlicensing.io/gh-pages/_drafts/2010-09-16-post-template.md (see Syntax highlighting section)
backtick - GitHub style
```java
ValidationResult validationResult = NetLicensing.LicenseeService.validate(context, licenseeNumber);
```
HTML pre/code - HTML can be included in markdown as well
<pre><code>
ValidationResult validationResult = NetLicensing.LicenseeService.validate(context, licenseeNumber);
<code/></pre>

Jekyll break for loop [duplicate]

This question already has an answer here:
Is there a "break" tag to escape a loop in Liquid?
(1 answer)
Closed 9 years ago.
I'm processing site.posts to compare post.categories against page.categories to create a related posts sidebar but if there are multiple common categories I get multiple links.
I want to break the inner loop but can't find anything to suggest this is possible.
Roughly (because on train and phone) the code I have is
{% for post in site.posts %}
{% for postcat in post.categories %}
{% for cat in page.categories %}
{% if cat == postcat %}
<p> {{ post.title }} </p>
{% endif %}
{% endfor %}
{% endfor %}
{% endfor %}
Not sure this is even doable
This is more of a Liquid Templating engine question, than a Jekyll one. It appears that Liquid has support for a {% break %} tag which is what you are looking for.
I would suggest making sure your Liquid gem is updated and then try using {% break %} in your code as suggested here.
Possible Duplicate