lumeland / lume

🔥 Static site generator for Deno 🦕
https://lume.land
MIT License
1.79k stars 77 forks source link

nunjucks template-inheritance/block not working as intended #314

Closed angelside closed 1 year ago

angelside commented 1 year ago

Version

1.12.1

Platform

Arch Linux

What steps will reproduce the bug?

~ git clone https://github.com/angelside/lume-block-test.git
~ deno task serve

What do you see instead?

Caused by Template render error: (/home/angelside/code/static/lume-block-test/index.njk)
  Error: no super block available for "breadcrumb"

Additional information

Block and super structure not working with default frontmatter layout definition.

But, it's working if replacing frontmatter with classic nunjucks extends (Which is not the desired method, I like to use single data.yml file for layout).

---
layout: layout.njk
---
{% extends "_includes/layout.njk" %}

Docs: https://mozilla.github.io/nunjucks/templating.html#template-inheritance Minimal example: https://github.com/angelside/lume-block-test


layout.njk

<div class="breadcrumb">
    {% block breadcrumb %}
        block in base
    {% endblock %}
<div>

index.njk

{% block breadcrumb %}
    {{ super() }}
    block in index
{% endblock %}
oscarotero commented 1 year ago

I have to dig into the nunjucks internals code and see if it's possible to include the blocks defined in one template into another.

oscarotero commented 1 year ago

I have been trying to implement this but looks like it's not possible because it breaks other things. A solution for your use case is creating a preprocessor to add the extends directive. For example:

site.preprocess([".njk"], (page) => {
  // Add the extends directive with the layout
  page.data.content = `{% extends "${page.data.layout}" %}\n${page.data.content}`;

  // Delete the layout from the frontmatter
  delete page.data.layout;
});