Render New in v1.0.0 #
A plugin to add shortcodes to render an Eleventy template string (or file) inside of another template.
Template Compatibility #
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 #
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);
};
module.exports
in your configuration file, so make sure you only copy the require
and the addPlugin
lines above!Usage #
renderTemplate
#
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 %}
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! The one exception here is that 11ty.js
string templates are not yet supported—use renderFile
below instead.
{% renderTemplate "vue" %}
<div>
THIS IS VUE <p v-html="hi"></p>
</div>
{% endrenderTemplate %}
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 #
The page
variable is available inside of these templates without any additional configuration. If you want to pass in additional data, you can do so like this:
---
myData:
myKey: myValue
---
{% renderTemplate "liquid", myData %}
{{ myKey }}
{% endrenderTemplate %}
Outputs myValue
.
renderFile
#
Use the renderFile
shortcode to render an include file.
{% 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" %}
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 #
The page
variable is available inside of these templates without any additional configuration. If you want to pass in additional data, you can do so like this:
---
myData:
myKey: myValue
---
{% renderFile "./_includes/blogpost.md", myData %}
Override the target file syntax #
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" %}
Will render blogpost.md using Nunjucks instead of Markdown!
Advanced #
Change the default shortcode names #
Pass some options into the addPlugin
call:
tagName: "renderTemplate"
: The default shortcode name to render a string.tagNameFile: "renderFile"
: The default shortcode name to render a file.
const { EleventyRenderPlugin } = require("@11ty/eleventy");
module.exports = function(eleventyConfig) {
let options = {
tagName: "superCoolString",
tagNameFile: "superCoolFile",
};
eleventyConfig.addPlugin(EleventyRenderPlugin, options);
};