Skip to navigation Skip to main content
Back the Build Awesome Kickstarter!

The same static site generator you know and love, now with endless Pro 'possum-bilities. But just like other parts of the Awesomeverse, Build Awesome will continue to be free and open source.

Support the Kickstarter!

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

eleventyConfig.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 an 11ty.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

Markdown, HTML (via Liquid) Layout

eleventy.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

Any 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>`;
	});
};