caseyanderson
caseyanderson

Reputation: 530

cannot remove line break generated by for loop in jekyll

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?

Upvotes: 3

Views: 1746

Answers (3)

knut
knut

Reputation: 27855

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.

Upvotes: 4

Kolappan N
Kolappan N

Reputation: 4011

I had the same problem and changing the source file to html solved the issue.

  1. Change the source file from markdown to HTML.
  2. Add the frontmatter --- to the top of HTML so it is processed by Jekyll.
  3. Change the corresponding markdown items such as ###### to <h6></h6>.

The linebreak should not appear now.

Upvotes: -1

Ron
Ron

Reputation: 1161

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.

Upvotes: 1

Related Questions