Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jekyll: How to use for loop to generate table row within the same table inside markdown

I am trying to generate a table, based on the posts that I have. Now, the challenge here is that this is inside a markdown file, so for each row that I generate, liquid seems to generate a new table for each generated row. Is there a way to fit all rows inside one single table?

Here is my code:

|Title  |Link  |
|---|---|
{% for my_post in site.posts %}
  {% if my_post.title %}
|{{ my_post.title }}  |[Click Here]({{ my_post.url }})  |
  {% endif %}
{% endfor %}

The generated output looks like this enter image description here

As you can see, the outcome is actually a messed-up table header row + two separate tables. Can I really generate rows and fit them all inside one table? or am I better off switching to html code?

like image 205
Antony Avatar asked Feb 26 '16 03:02

Antony


1 Answers

The liquid markup introduces line breaks in markdown.

Edit : you can now manage use whitespace control in Liquid

In Liquid, you can include a hyphen in your tag syntax {{-, -}}, {%-, and -%} to strip whitespace from the left or right side of a rendered tag.

|Title  |Link  |
|---|---|
{% for my_post in site.posts -%}
{% if my_post.title -%}
|{{ my_post.title }}  |[Click Here]({{ my_post.url }})  |
{% endif %}
{%- endfor -%}

old answer

If you put your liquid tags on same line you'll have a valid table outputed.

| Title | Link |
|---|---|{% for my_post in site.posts %}{% if my_post.title %}
|{{ my_post.title }}  |[Click Here]({{ my_post.url }})  |{% endif %}{% endfor %}
like image 72
David Jacquel Avatar answered Oct 12 '22 10:10

David Jacquel