eleventy icon indicating copy to clipboard operation
eleventy copied to clipboard

Using Nunjucks blocks from within Markdown

Open cmcknight opened this issue 5 years ago • 3 comments

I'd like to be able to assign content in a Markdown source file to a Nunjucks block.

page.njk:

{% block main %}{% endblock main %}

page.md:

{% block main %} This is some sample text. {% endblock main %}

The thought is that the template could be set up to accommodate named sections of content that could be written in Markdown.

page.njk:

`

{% block main %}{% endblock main %}
{% block section1 %}

... `

page.md:

` {% block main %} This is some sample text. {% endblock main %}

{% block section1 %} This is some more sample text. {% endblock section1 %}

... `

cmcknight avatar Oct 21 '20 17:10 cmcknight

This is currently doable, but you need to use Nunjucks {% extends %} instead of Eleventy layouts.

dz4k avatar Dec 30 '20 07:12 dz4k

Is this still doable? I just had it working and suddenly it stopped.

ngustavo avatar Apr 04 '23 12:04 ngustavo

(Using 11ty v.1.0.2) Just to share my case, hope this helps.

I've a a page that uses a specific layout in my FrontMatter: layout: layouts/article-container-layout.html that is based upon another "base" layout (a more general one). In the page I use the block syntax to have the opportunity to substitute content. But it does not function. The block are never substituted.

I change my definitions in my FrontMatter as per @dz4k suggestion: extends: layouts/article-container-layout.html

The engine processed the blocks, but unluckily lost all the layout. I wish I could combine both... as Nunjucks is capable to do.

Is there a way to obtain this? Thanks in advance.

mbutomax avatar Jun 14 '23 18:06 mbutomax

Sorry for the super late reply here but Markdown is preprocessed by Liquid by default (not Nunjucks).

You’ll likely need to use https://www.11ty.dev/docs/config/#default-template-engine-for-markdown-files to change this setting to Nunjucks instead. Then you’ll be able to use blocks as expected (in the same file)

zachleat avatar Oct 23 '25 22:10 zachleat