11ty / eleventy

A simpler site generator. Transforms a directory of templates (of varying types) into HTML.
https://www.11ty.dev/
MIT License
17.18k stars 494 forks source link

How do I include a NJK into a Liquid template? #1316

Closed jalberto closed 3 months ago

jalberto commented 4 years ago

if I create a Liquid file like this:

{% include home/a.liquid %}
{% include home/b.njk %}
{% include home/c.liquid %}

`b.njk' will try to render as liquid, and this is an issue if it contains any njk specificities

jalberto commented 4 years ago

is it a bug, or works as expected?

cj81499 commented 4 years ago

I believe this is working as intended. It's generally best to pick one templating language and stick with it for the entire project.

gael-ian commented 4 years ago

It's generally best to pick one templating language and stick with it for the entire project.

Sure it's safer to stick with a single templating language. But what about markdown files?

It seems Eleventy does not apply any parsing or transformation to included files and blindly delegates the responsibility of inclusions to the template engine, just giving it some hints about where to find files. It means you can't have a template with complex HTML that includes pure content sections as easier to maintain markdown files.

binyamin commented 4 years ago

@gael-ian For cases like that, I have a filter to convert markdown to html.

const md =  require('markdown-it')({ })

eleventyConfig.setLibrary('md', md);

eleventyConfig.addFilter("markdownify", string => {
    return md.renderInline(string)
})
binyamin commented 4 years ago

@jalberto I think this is happening because the njk template is being included with a liquid tag. To my knowledge, includes only read the contents of a file. That said, you could create a custom njk filter (see https://github.com/11ty/eleventy/issues/1316#issuecomment-709389133).

gael-ian commented 4 years ago

To my knowledge, includes only read the contents of a file.

And maybe that's the point. What if 11ty overrides template engines include functions to not only give them the right context to be able to load files from the right directories but to also apply parsing and transforming ?

This way, no need for a markdownify filter (by the way, thanks for the tip) or any other. Any file could just be written is any of the supported formats and the output still be what developpers expect it to be.

I know it may be difficult to do and maybe it's not the way the 11ty team wants to go.

zachleat commented 3 months ago

Correct, because Nunjucks and Liquid are similar, this may appear like it’s working but it’s parsing both as Liquid (the file extension of an include has no bearing).

All that said, you can do this with the Render plugin, if you wish: https://www.11ty.dev/docs/plugins/render/