Black Lives Matter
Eleventy
Eleventy Documentation
Toggle Menu
Eleventy 5.81s
Astro 12.52s

Render Added in v1.0.0

Contents

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:

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:

Filename .eleventy.js
const { EleventyRenderPlugin } = require("@11ty/eleventy");

module.exports = function(eleventyConfig) {
eleventyConfig.addPlugin(EleventyRenderPlugin);
};
Expand to view all of the Plugin Options
Filename .eleventy.js
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
});
};
INFO:
You’re only allowed one 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.

View this example in: Liquid Nunjucks 11ty.js Handlebars
Syntax Liquid
{% renderTemplate "md" %}
# I am a title

* I am a list
* I am a list
{% endrenderTemplate %}
Syntax Nunjucks
{% renderTemplate "md" %}
# I am a title

* I am a list
* I am a list
{% endrenderTemplate %}
Syntax JavaScript
module.exports = async function() {
return await this.renderTemplate(`# I am a title

* I am a list
* I am a list
`
, "md");
};

The renderTemplate shortcode requires an async-friendly template language and is not available in Handlebars.

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!

View this example in: Liquid Nunjucks 11ty.js Handlebars
{% 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 %}
Syntax JavaScript
module.exports = async function() {
return await this.renderTemplate(`<div>
THIS IS VUE <p v-html="hi"></p>
</div>
`
, "vue");
};

The renderTemplate shortcode requires an async-friendly template language and is not available in Handlebars.

INFO:
The one exception here is that {% 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:

View this example in: Liquid Nunjucks 11ty.js Handlebars
Syntax Liquid
---
myData:
myKey: myValue
---
{% renderTemplate "liquid", myData %}
{{ myKey }}
{% endrenderTemplate %}
Syntax Nunjucks
---
myData:
myKey: myValue
---
{% renderTemplate "liquid", myData %}
{{ myKey }}
{% endrenderTemplate %}
Syntax JavaScript
module.exports.data = {
myData: {
myKey: "myValue"
}
};
module.exports.render = async function(data) {
return await this.renderTemplate(`{{ myKey }}`, "liquid", data.myData);
};

The renderTemplate shortcode requires an async-friendly template language and is not available in Handlebars.

Outputs myValue.

renderFile Jump to heading

Use the renderFile shortcode to render an include file.

View this example in: Liquid Nunjucks 11ty.js Handlebars
Syntax Liquid
{% renderFile "./_includes/blogpost.md" %}
Syntax Nunjucks
{% renderFile "./_includes/blogpost.md" %}
Syntax JavaScript
module.exports = async function() {
return await this.renderFile("./includes/blogpost.md");
};

The renderFile shortcode requires an async-friendly template language and is not available in Handlebars.

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!

View this example in: Liquid Nunjucks 11ty.js Handlebars
Syntax Liquid
{% renderFile "./_includes/header.vue" %}
Syntax Nunjucks
{% renderFile "./_includes/header.vue" %}
Syntax JavaScript
module.exports = async function() {
return await this.renderFile("./includes/header.vue");
};

The renderFile shortcode requires an async-friendly template language and is not available in Handlebars.

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:

View this example in: Liquid Nunjucks 11ty.js Handlebars
Syntax Liquid
---
myData:
myKey: myValue
---
{% renderFile "./_includes/blogpost.md", myData %}
Syntax Nunjucks
---
myData:
myKey: myValue
---
{% renderFile "./_includes/blogpost.md", myData %}
Syntax JavaScript
module.exports.data = {
myData: {
myKey: "myValue"
}
};
module.exports.render = async function(data) {
return await this.renderFile("./includes/blogpost.md", data.myData);
};

The renderFile shortcode requires an async-friendly template language and is not available in Handlebars.

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.

View this example in: Liquid Nunjucks 11ty.js Handlebars
Syntax Liquid
---
myData:
key: value
---
{% renderFile "./_includes/blogpost.md", myData, "njk" %}
Syntax Nunjucks
---
myData:
key: value
---
{% renderFile "./_includes/blogpost.md", myData, "njk" %}
Syntax JavaScript
module.exports.data = {
myData: {
myKey: "myValue"
}
};
module.exports.render = async function(data) {
return await this.renderFile("./includes/blogpost.md", data.myData, "njk");
};

The renderFile shortcode requires an async-friendly template language and is not available in Handlebars.

Will render blogpost.md using Nunjucks instead of Markdown!

From the Community


Other pages in Plugins: