Render New in v1.0.0 Jump to heading
A plugin to add shortcodes to render an Eleventy template string (or file) inside of another template.
Template Compatibility Jump to heading
This plugin adds a renderTemplate
and renderFile
asynchronous shortcode to:
- Nunjucks
- Liquid
- JavaScript (11ty.js)
Everything you’ve added to project’s configuration file will also be available in these renders too: shortcodes, filters, etc. That means you can nest 😱 them, too!
Installation Jump to heading
This plugin is bundled with Eleventy core so it doesn’t require additional installation. But you do have to add it to your configuration file (probably .eleventy.js
) with addPlugin
:
const { EleventyRenderPlugin } = require("@11ty/eleventy");
module.exports = function(eleventyConfig) {
eleventyConfig.addPlugin(EleventyRenderPlugin);
};
Expand to view all of the Plugin Options
const { EleventyRenderPlugin } = require("@11ty/eleventy");
module.exports = function(eleventyConfig) {
eleventyConfig.addPlugin(EleventyRenderPlugin, {
tagName: "renderTemplate", // Change the renderTemplate shortcode name
tagNameFile: "renderFile", // Change the renderFile shortcode name
});
};
module.exports
in your configuration file, so make sure you only copy the require
and the addPlugin
lines above!Usage Jump to heading
renderTemplate
Jump to heading
Use the renderTemplate
paired shortcode to render a template string.
{% renderTemplate "md" %}
# I am a title
* I am a list
* I am a list
{% endrenderTemplate %}
{% renderTemplate "md" %}
# I am a title
* I am a list
* I am a list
{% endrenderTemplate %}
module.exports = async function() {
return await this.renderTemplate(`# I am a title
* I am a list
* I am a list`, "md");
};
The content inside of the shortcode will be rendered using Markdown ("md"
). Front matter is not yet supported.
The first argument to renderTemplate
can be any valid templateEngineOverride
value. You can even use "liquid,md"
to preprocess markdown with liquid. You can use custom template types here too, including the Vue plugin!
{% renderTemplate "vue" %}
<div>
THIS IS VUE <p v-html="hi"></p>
</div>
{% endrenderTemplate %}
{% renderTemplate "vue" %}
<div>
THIS IS VUE <p v-html="hi"></p>
</div>
{% endrenderTemplate %}
module.exports = async function() {
return await this.renderTemplate(`<div>
THIS IS VUE <p v-html="hi"></p>
</div>`, "vue");
};
{% renderTemplate "11ty.js" %}
JavaScript string templates are not yet supported—use renderFile
below instead.To add Vue support, don’t forget to install @11ty/eleventy-plugin-vue
(v0.6.0 or newer) and add the Vue plugin in your config file. There is an example on the Eleventy Vue Plugin documentation showing how to call the render plugin inside of Vue components.
Pass in data Jump to heading
Both the eleventy
and page
variables are available inside of these templates by default. If you want to pass in additional data, you can do so like this:
---
myData:
myKey: myValue
---
{% renderTemplate "liquid", myData %}
{{ myKey }}
{% endrenderTemplate %}
---
myData:
myKey: myValue
---
{% renderTemplate "liquid", myData %}
{{ myKey }}
{% endrenderTemplate %}
module.exports.data = {
myData: {
myKey: "myValue"
}
};
module.exports.render = async function(data) {
return await this.renderTemplate(`{{ myKey }}`, "liquid", data.myData);
};
Outputs myValue
.
renderFile
Jump to heading
Use the renderFile
shortcode to render an include file.
{% renderFile "./_includes/blogpost.md" %}
{% renderFile "./_includes/blogpost.md" %}
module.exports = async function() {
return await this.renderFile("./includes/blogpost.md");
};
The first argument to renderFile
is a project root relative path to any template file. Front matter inside of the target files is not yet supported. The template syntax used is inferred by the file extension.
Note that you can use files supported by any custom file extensions you’ve added too, including a Vue Single File Component from the Eleventy Vue plugin!
{% renderFile "./_includes/header.vue" %}
{% renderFile "./_includes/header.vue" %}
module.exports = async function() {
return await this.renderFile("./includes/header.vue");
};
To add Vue support, don’t forget to install @11ty/eleventy-plugin-vue
(v0.6.0 or newer) and add the Vue plugin in your config file.
Pass in data Jump to heading
Both the eleventy
and page
variables are available inside of these templates by default. If you want to pass in additional data, you can do so like this:
---
myData:
myKey: myValue
---
{% renderFile "./_includes/blogpost.md", myData %}
---
myData:
myKey: myValue
---
{% renderFile "./_includes/blogpost.md", myData %}
module.exports.data = {
myData: {
myKey: "myValue"
}
};
module.exports.render = async function(data) {
return await this.renderFile("./includes/blogpost.md", data.myData);
};
Override the target file syntax Jump to heading
The syntax is normally inferred using the file extension, but it can be overridden using a third argument. It can be any valid templateEngineOverride
value. You can even use "liquid,md"
to preprocess markdown with liquid.
---
myData:
key: value
---
{% renderFile "./_includes/blogpost.md", myData, "njk" %}
---
myData:
key: value
---
{% renderFile "./_includes/blogpost.md", myData, "njk" %}
module.exports.data = {
myData: {
myKey: "myValue"
}
};
module.exports.render = async function(data) {
return await this.renderFile("./includes/blogpost.md", data.myData, "njk");
};
Will render blogpost.md using Nunjucks instead of Markdown!