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

Template Render Error with Markdown file using Nunjucks engine #791

Closed
babycourageous opened this issue Nov 25, 2019 · 6 comments
Closed
Labels
education: template language Learning about a template language.

Comments

@babycourageous
Copy link

babycourageous commented Nov 25, 2019

Template Render Error with Markdown file as Nunjucks engine if a fenced code block contains Nunjucks code that conflicts with code already in layout

Describe the bug
When writing a fenced code block in a Markdown file that contains Nunjucks code - specifically wanting to display a simple template example like:

{% extends "layout.njk" %}
{% block content %}
<h1>Example Heading</h1>
{% endblock %}

11ty throws an error - in this case it is processing the code block as Nunjucks and looks for that layout.njk.

TemplateContentRenderError was thrown
> (./src/posts/[FILENAME].md)
  Error: template not found: layout.njk

Originally I was just gonna define markdownTemplateEngine as false but I'm using shortcodes (Universal) as well.

I could just define the markdownTemplateEngine as 11ty.js (or some other random language) and the error vanishes since it's not trying to convert that raw Nunjucks code. I was curious if there was another solution - maybe one where the renderer knows not to worry about the stuff in the code block? - That way I can continue using Nunjucks as the template engine for markdown rather than the above workaround.

Hope that makes sense!

Expected behavior
11ty ignores markdown template engine code inside markdown files if it is inside a code block

Environment:

  • OSX
  • Eleventy Version 0.9.0
  • MarkdownIt as markdown renderer as well as 11ty-syntax-highlight plugin to highlight code blocks.
@babycourageous babycourageous changed the title Template Render Error with Markdown file as Nunjucks engine Template Render Error with Markdown file using Nunjucks engine Nov 25, 2019
@octoxalis
Copy link

Use Nunjucks {% raw %} tag for your example.

Have a look there for more insight (NB: this site is still beta!).

@babycourageous
Copy link
Author

Thanks @octoxalis!

Indeed if I wrap the code block in {% raw %} tags the code block renders properly instead of trying to process it as a Nunjucks block. I guess now it's a matter of intercepting Markdown files and wrapping all code blocks in raw tags. Hmmm.

@zachleat
Copy link
Member

A few ideas here:

  • You can override the template language on a template-by-template basis like so: https://www.11ty.io/docs/languages/#overriding-the-template-language
  • Maybe you can use Nunjucks for your layout and raw markdown (without another preprocessing language) for your content? Not sure where your shortcodes live.
  • Just an idea worth investigating: Maybe you can create a shortcode that returns markdown with the raw text content inside? Not sure if that will work out of the box, hmm

@zachleat zachleat added education: template language Learning about a template language. and removed needs-triage labels Nov 27, 2019
@babycourageous
Copy link
Author

Thanks @zachleat!

Option #2 was where I started. I omitted any Markdown template language (setting markdownTemplateEngine to false) but then I reckon I can't use any shortcodes in a Markdown post?

Wrapping code blocks in nunjucks raw tags works. But it seems like there might be a more straightforward way than having to wrap Markdown code blocks, methinks?

Do you think there's a way to say, "Howdy, template engine, when you render Markdown files do us a solid and don't process code contained inside triple backslash code blocks, that's not code to be rendered, that's code to be displayed ya silly." hahah

@babycourageous
Copy link
Author

@zachleat, just a little update. I dug around the 11ty docs site code and saw that wrapping code blocks with {% raw %} tags was how you prevent njk code inside code blocks from running. I decided to go ahead and follow your example there. If I forget to wrap them I usually get an error as a reminder, heheh. Seeing that's how y'all are handling that sorta thing I feel like that's the way to go!

Feel free to close if you like!

@zachleat
Copy link
Member

zachleat commented Jul 1, 2022

This is an automated message to let you know that a helpful response was posted to your issue and for the health of the repository issue tracker the issue will be closed. This is to help alleviate issues hanging open waiting for a response from the original poster.

If the response works to solve your problem—great! But if you’re still having problems, do not let the issue’s closing deter you if you have additional questions! Post another comment and we will reopen the issue. Thanks!

@zachleat zachleat closed this as completed Jul 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
education: template language Learning about a template language.
Projects
None yet
Development

No branches or pull requests

3 participants