cannot remove line break generated by for loop in jekyll - html

I am unable to remove the new line character that is generated by jekyll's for loop from the following:
<br/><ul class="post-list">
{% for post in site.tags[page.tag] %}
<li>
<h2>{{ post.date | date: "%b %d %Y" | append: post.excerpt }}</h2>
</li>
{% endfor %}
</ul>
I have looked all over SO and, though I see that this is a issue that people get stuck on, it is not clear to me whether it is possible to remove this newline without writing something that would remove the newline characters from the jekyll generated HTML after the fact. I would prefer not to have to do that. Does anyone know how to get post.date and post.excerpt to generate without a new line in between?

Im not sure if I understood your question correct. I guess, with new line character you mean some obsolete empty lines in the generated HTML.
At least with newer versions of liquid, the solution is to replace {% by {%-.
<br/><ul class="post-list">
{%- for post in site.tags[page.tag] -%}
<li>
<h2>{{ post.date | date: "%b %d %Y" | append: post.excerpt }}</h2>
</li>
{%- endfor -%}
</ul>
More details can be found here.

I think if you move the {% endfor %} up to the end of the </li> it will remove the new lines.
Like:
<br/><ul class="post-list">
{% for post in site.tags[page.tag] %}
<li>
<h2>{{ post.date | date: "%b %d %Y" | append: post.excerpt }}</h2>
</li>{% endfor %}
</ul>
EDIT:
After playing with this I see what you mean. The post.excerpt itself seems to contain a line break. If you take out the post.excerpt and just use post.date as an example you should see that you will get new lines if the {% endfor %} is below the </li> and moving it up fixes that. I tried a few things to remove the line break from the actual post.excerpt but nothing worked. I use my own descriptions in the front matter instead of post.excerpt and that works.
Also, your code I think is not correct - by appending the post.excerpt to the post date inside of an h2 you are ending up with a <p> inside of an h2.
I would do something like this:
<ul class="post-list">
{% for post in site.tags.[page.tag] %}
<li><h2>{{post.title}}</h2>{{post.excerpt}}</li>{% endfor %}
</ul>
I used title instead of date, use what ever works for you but appending the excerpt to the date inside of an h2 doesn't seem right.
As for the post.excerpt seeming to contain a line break, I think that may be a bug. Though in my code I don't see the line break causing any problems.

I had the same problem and changing the source file to html solved the issue.
Change the source file from markdown to HTML.
Add the frontmatter --- to the top of HTML so it is processed by Jekyll.
Change the corresponding markdown items such as ###### to <h6></h6>.
The linebreak should not appear now.

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 }}

For loop duplicating code in Jekyll / Liquid

My input is pretty simple. I'm pulling from a data file to fill <p> with content then running the method markdownify, but the output duplicates the <p> element in (to me) unexpected ways.
Please advise! And thank you.
Sample Input:
{% for item in site.data.filename.item %}
<p style="display:{{ item.display }}" id="{{ item.nav }}">{{ item.content | markdownify }}</p>
{% endfor %}
And the Output:
<p id="test" style="display:block"></p>
<p>testing output</p>
<p></p>
Is there something going on with my liquid syntax? Many thanks in advance
This is typically what happens when you nest paragraphs. You should remove HTML tags from your content.
{{ item.content | strip_HTML}}
https://docs.shopify.com/themes/liquid/filters/string-filters#strip_html
If you want to keep the html formatting, you can use a div to wrap the content, like this:
{% for item in site.data.filename.item %}
<div style="display:{{ item.display }}" id="{{ item.nav }}">{{ item.content | markdownify }}</div>
{% endfor %}
Did you try removing markdownify?
Internally jekyll framework will apply markdownify so we do not need to write markdownify
I tried your code with markdownify removed and got single lines of <p> duplicates.
strip_HTML option also works.
Either remove markdownify or put strip_HTML
Instead of display:{{ item.display }} try
display:{{ display }}
Should do the job! :)

(Django) line breaking in template goes to a space

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.

Jekyll: code is getting highligheted unwantingly

I've got the following page:
---
layout: default
status: publish
published: true
title: Categories
author:
display_name: lucas
---
{% for category in site.categories %}
<li><a name="{{ category | first }}">{{ category | first }}</a>
<ul>
{% for posts in category %}
{% for post in posts %}
<li>{{ post.title }}</li>
{% endfor %}
{% endfor %}
</ul>
</li>
{% endfor %}
Instead of listing the hrefs per categery, it somehow gets code highlighting:
Can anyone help me out preventing the code getting highlighted? Thanks!
As mentioned by #David Jacquel, .md files are automatically parsed as markdown, in which four space indentations are treated as preformatted code.
I followed his suggestion on my website: change all files to .html. This disabled Markdown parsing in the file. However, if you are unwilling to write in pure HTML and would still like to use Markdown, there is a workaround.
For each element that you would like Markdown to ignore, give it the attribute markdown="0".
For each element that you would like Markdown to parse, give it the attribute markdown="1".
In your case, the following is a possible implementation.
...
<div markdown="0">
{% for category in site.categories %}
<li><a name="{{ category | first }}">{{ category | first }}</a>
<ul>
{% for posts in category %}
{% for post in posts %}
<li>{{ post.title }}</li>
{% endfor %}
{% endfor %}
</ul>
</li>
{% endfor %}
</div>
Is it a .md file ? If yes, try to change for an .html extension.
In markdown four space indentation is used to print code tag.

Add a project page to site.posts on a Jekyll blog

I have a Jekyll blog where the main page is a bunch of links to posts, but where I also want to include a link to a project's gh-pages page and I want the list to stay sorted by date. Right now, I'm just manually inserting it at the top of the page.
<ul class="posts">
<li>
<span class="post-date">Jul 8, 2015</span>
<a class="post-link" href="/QuisCustodiet/">The Most Influential Works, According to TvTropes</a>
</li>
{% for post in site.posts %}
<li>
<span class="post-date">{{ post.date | date: "%b %-d, %Y" }}</span>
<a class="post-link" href="{{ post.url | prepend: site.baseurl }}">{{ post.title }}</a>
</li>
{% endfor %}
</ul>
This looks okay, but it will break in a week or two when I make another post. Is there a way to create another "post" and insert it into the list of site.posts in a way that it stays sorted? Is there some other much better way of doing this that I don't know about?
If I understand your problem right, you want to have some "special project posts" in your list of posts that are only links to another specific project site but have a date and therefore can be sorted together with the other posts.
Here is what I came up with:
You create an empty Post in _posts/ with front matter like this:
---
layout: post
title: "Project Example"
customlink: http://www.example.org
date: 2015-07-12 12:50:25
---
The customlink attribute is for the link to your project page. In your html, where you list all your posts, you make an if-statement to check for the attribute and handle it properly. Like this for example:
<ul>
{% for post in site.posts %}
<li>
{% if post.customlink %}
{{ post.title }}
{% else %}
{{ post.title }}
{% endif %}
</li>
{% endfor %}
</ul>
Your project post is treated as your other posts and sorted with them by date, but its link will direct the user to your project page.
Jekyll will still create the html for this post and serve it like all the other posts under a specific path. To prevent this you could add an empty permalink attribute in the posts front matter like permalink: "" and your post would lie under the root of your blog. Jekyll would still create a ".html" file for this post in the _site folder.
You need to create the posts for every external project manually but I do not know a better solution.