-
-
Notifications
You must be signed in to change notification settings - Fork 498
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
How to use nunjucks macro inside markdown files ? #613
Comments
Have you enabled NJK as a template language acceptable for use in markdown files? within your elventyConfig ? |
I don't think you can import your macros in the base template. I have to import my macros on every page to work around this. |
@edwardhorsford How are you importing macros from a |
I import all macros under a Sample from one of my md files: ---
title: This is the title
date: 2019-09-07
tags:
- foo
- bar
- baz
---
{% import "includes/macros.njk" as macro with context %}
Some content here And then use a macro like this: |
Thanks @edwardhorsford!
|
@edwardhorsford thanks for sharing this solution. I’m trying to implement something along those lines, but i get lost a bit. edit i think i found it: #212 (comment) |
So i manage to cheat my way to add the call to the macros when the collection is built:
I wish i could use an absolute link to the macros, but so far this should be working :) |
Would it need an absolute path if you put the macros.njk file into the _includes/ directory? |
oh my! So absolute actually work, i can symply use Thanks a lot @pdehaan! this is gonna remove a lot of run around! |
@julientaq Great idea! This has been one of the few annoyances I’ve had with Eleventy. This will make is so much easier for clients to add new content without having to remember to import macros. Here’s my slightly refactored code fwiw: // Automatically import macros on every page
// (otherwise we need to manually include on each page that uses them)
// https://github.com/11ty/eleventy/issues/613#issuecomment-968189433
config.addCollection('everything', (collectionApi) => {
// Note: Update the path to point to your macro file
const macroImport = `{% import "macros/index.njk" as macro with context %}`;
// Note: Update the pattern below to include all files that need macros imported
// Note: Collections don’t include layouts or includes, which still require importing macros manually
let collection = collectionApi.getFilteredByGlob('src/**/*.njk');
collection.forEach((item) => {
item.template.frontMatter.content = `${macroImport}\n${item.template.frontMatter.content}`
})
return collection;
}); FYI I use a single file to define all of my macros ( {% macro foo(params) -%}
{% include "./foo.njk" %}
{%- endmacro %}
{% macro bar(params) -%}
{% include "./bar.njk" %}
{%- endmacro %} |
- ref: 11ty/eleventy#613 -- using shortcodes in place of component macros
I'm trying the above approach and whilst it seems to work for individual pages, it's crashing my build if applied to a paginated template that generates lots of pages - my tag pages. Has anyone else experienced similar? The error is If I truncate the paginated data to 100 items it renders ok. Edit: for now, I'm going to exclude all paginated pages - this seems to work for blog posts, which is the bulk of my files anyway. Would be nice if there was a more 'official' way of doing this / one that could apply to paginated pages too. |
For what it’s worth, code based on the excellent example shown by @tedw above doesn’t work in the current alpha of Eleventy 3.0 (3.0.0-alpha.4 as of this writing); it errors out with, among other messages, the following (here, the macro name is
The same macro works fine in Eleventy 2.x. cc: @zachleat |
Ran into the same issue with item.template.frontMatter.content = `${macroImport}\n${item.template.frontMatter.content}` As of
|
Working example posted here: #3345 (comment) |
Hi !
Question
I would like to create a macro to embed images inside a
<figure>
with a<figcaption>
tag.I'm trying to use nunjucks macro inside my markdown files but it doesn't work.
Am i doing it wrong or is it not possible ?
Context
I have created a
src/_includes/macro.njk
file with a test macroThe macro is imported in the base template
src/_includes/layouts/base.njk
Then I use macro inside a markdown file
src/_content/pages/styleguide.md
but nothing happen### Figures {{ macro.figure("test") }}
The text was updated successfully, but these errors were encountered: