- Stable
3.0.0
- Canary
3.0.1-alpha.5
Menu
Eleventy
1.93s
Astro
22.90s
Virtual Templates Added in v3.0.0
In addition to template files in your input directory, Eleventy can also process virtual templates defined in your configuration file (or plugins). Related GitHub #1612.
The RSS plugin offers a virtual template to add feeds to your project.
API
Jump to section titled: APIeleventyConfig.addTemplate(virtualPath, content, data = {});
virtualPath
: used to determine the template language and data cascade for this template. This path is relative to your project’s input directory.content
: usually a string, but maybe JavaScript (if using an11ty.js
template). Can include front matter if the template language supports it.data
: a data object tied to the template. A little more ergonomic than front matter but functionally the same.
Example
Jump to section titled: ExampleMarkdown, HTML (via Liquid) Layout
Jump to section titled: Markdown, HTML (via Liquid) Layouteleventy.config.js
export default function(eleventyConfig) {
// Create content templates Files
eleventyConfig.addTemplate("virtual.md", `# Hello`, {
layout: "virtual.html"
});
// Works great with Layouts too
eleventyConfig.addTemplate("_includes/virtual.html", `<!-- Layout -->{{ content }}`);
};
module.exports = function(eleventyConfig) {
// Create content templates Files
eleventyConfig.addTemplate("virtual.md", `# Hello`, {
layout: "virtual.html"
});
// Works great with Layouts too
eleventyConfig.addTemplate("_includes/virtual.html", `<!-- Layout -->{{ content }}`);
};
JavaScript
Jump to section titled: JavaScriptAny of the JavaScript shapes on 11ty.js
templates are also supported here.
eleventy.config.js
export default function(eleventyConfig) {
// Create content templates Files
eleventyConfig.addTemplate("virtual.11ty.js", function(data) {
return `<h1>Hello</h1>`;
});
};
module.exports = function(eleventyConfig) {
// Create content templates Files
eleventyConfig.addTemplate("virtual.11ty.js", function(data) {
return `<h1>Hello</h1>`;
});
};