How to combine multiple Markdown sources in a Nunjkucks template in Eleventy

Viewed 580

I'm working with Nunjucks templates in Eleventy. The page layout consists of a main content area and an aside. I am able to use Markdown for the content but cannot find a way to use Markdown for the aside. It would seem that only one source can be Markdown; any other sources included in the template must be Nunjucks templates.

index.njk:

  <article>
    {{ content | safe }} 
  </article>
  <aside>
    {% include "aside.md" %}
  </aside>

aside.md:

# Aside.

Result:

<article>
<p>This is the content from the upstream Markdown.</p>
</article>
<aside>
# Aside.
</aside>

The aside is still raw Markdown. How can I include processed Markdown?

I'm new to all these technologies and have a feeling I am missing something basic.

1 Answers

Use includes or macros!

Includes have to be placed in a specific folder (_includes - you can override this folder name, or even have sub-folders):

    {% include 'partials/google-analytics.html' %}
    {% include 'partials/cookie-consent.html' %}
    {% include 'partials/post-sharing.html' %}

Or you can define a macro (they have to be imported, or be defined where you use them). The PRO for macros is that you can pass in parameters when you call them, they work as functions...

{% macro paginationLink(active, disabled, href, title) %}    
    <li class="page-item {% if active %} active {% elseif disabled %} disabled {% endif %}">
        {% if disabled %} 
            <span class="page-link">{{ title | safe }}</span>
        {% else %}
            <a class="page-link" href="{{ href | url }}">{{ title | safe }}</a>
        {% endif %}        
    </li>
{% endmacro %}

The macro ^ can then be easily called:

{{ paginationLink(false, isFirst, firstPageHref, '<i class="fas fa-angle-double-left"></i>') }}
Related