Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

override block in post using nunjucks #1632

Closed
btrem opened this issue Feb 6, 2021 · 4 comments
Closed

override block in post using nunjucks #1632

btrem opened this issue Feb 6, 2021 · 4 comments

Comments

@btrem
Copy link

btrem commented Feb 6, 2021

If I have a block defined in a layout using nunjucks, can I override it in a post? If this is my layout:

<!-- other markup -->
{% block content %}
    <main>
        {{ content | safe }}
    </main>
{% endblock %}

In mypost.njk, can I do this?

---
title: my post
---
{% block content %}
    <main class="specialClassForMyPost">
        <p>This is my post.</p>
    </main>
{% endblock %}

So in other words change the main element on a one off basis. That gives me duplicate <main> tags, which means either it can't be done or I'm doing it wrong.

@btrem btrem added the education label Feb 6, 2021
@Ryuno-Ki
Copy link
Contributor

Ryuno-Ki commented Feb 6, 2021

Sounds like a use case for {% extends %} to me 🤔

@pdehaan
Copy link
Contributor

pdehaan commented Feb 7, 2021

@btrem Do you have a public repo somewhere? I might be able to take a look.

#853 might also have some good pointers and links to examples in repos.

@btrem
Copy link
Author

btrem commented Feb 7, 2021

Sorry, of course I messed up my initial question. I edited it to make it clear what I'm hoping to achieve.

@Ryuno-Ki So it sounds like I can't override a block in a a post as I demonstrated in the edited initial post. Instead, I'd need to create a new layout, say, my-new-layout.njk:

{% extends 'base.njk' %}
{% block content %}
    <main class="specialClassForMyPost">
        {{ content | safe }}
    </main>
{% endblock %}

and then in my-post.njk (or my-post.md or whatever):

---
title: my post
layout: my-new-layout.njk
---

<main class="specialClassForMyPost">
    <p>This is my post.</p>
</main>

@btrem
Copy link
Author

btrem commented Feb 7, 2021

@pdehaan Thanks for the link to the github thread. It confirms what I thought -- and provides some interesting reading -- which is not a big deal. I can create a layout that extends my base layout.

@btrem btrem closed this as completed Feb 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants