简体   繁体   中英

Render markdown specified in Jekyll/liquid for loop

I'm trying to grab a file specified in the front matter of Jekyll page and and then render markdown in my template.

This just worked when there was only one key in the yaml, but I changed it to a loop and now I can't get it to render the markdown. It just spits out the values, and doesn't actually markdownify them.

My yaml looks like this:

additional_sidebar:
  - editorial-extra.md
  - editorial-extra-2.md

My Liquid looks like this:

{% for sidebar in page.additional_sidebar %}

  {% capture sidebar %}{% include {{ sidebar }} %}{% endcapture %}
  <section class="aside">
    {{ sidebar | markdownify }}
  </section>

{% endfor %}

The result is that it just spits out the values, instead of rending the markdown:

editorial-extra.md editorial-extra-2.md

For reference, this was my original, working code:

yaml:

additional_sidebar: editorial-extra.md

Liquid:

{% if page.additional_sidebar %}

  {% capture additional_sidebar %}{% include {{ page.additional_sidebar }} %}{% endcapture %}
  <section class="aside aside-scroll fixedsticky">
    {{ additional_sidebar | markdownify }}
  </section>

{% endif %}

You have a race condition in your liquid code. You use the sidebar var in the loop and in the capture. Just change the sidebar for sidebarmd in the capture.

{% for sidebar in page.additional_sidebar %}

  {% capture sidebarmd %}{% include {{ sidebar }} %}{% endcapture %}
  <section class="aside">
    {{ sidebarmd | markdownify }}
  </section>

{% endfor %}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM