Editing templates
PieCrust makes all your pages and layouts go through a templating stage.
By default, the template engine is Twig, and all the examples found in this documentation are using the Twig syntax.
If you want to use a different default engine, change the
default_template_enginesetting in the site configuration.If you want just a specific layout file to use a different template engine, change its file extension from
htmlto the name of the engine you want (see the available template engines). Remember that pages will have to specify that layout with the extension.If you want a page itself to use a different template engine, set the
template_engineconfiguration setting for that page to the name of the engine you want. Available template engines are listed in the template engines list.
Writing a layout template
Templates are text files placed in the _content/templates folder. They usually
have the html file extension if they’re written using the default template
engine’s syntax — otherwise, they need to have a file extension that matches
the engine they should be processed with (see the available engine names).
In its most basic form, a layout template would look like this:
<html>
<head>
<title>{{ page.title }}</title>
</head>
<body>
{{ content|raw }}
</body>
</html>
This creates an HTML page whose title and contents are the title and contents of
the PieCrust page being rendered. This is possible because the page.title
and content variables are exposed to the template engine. A lot of other
variables are available (see the “Template variables” section below).
Page templating
Pages go through the templating stage before having their contents formatted. This means that template syntax (variables, loops, etc) are available not only in layout files, but also in pages. This is especially useful when a page needs to display a list of blog posts, for instance.
Template variables
Here’s how PieCrust exposes variables to the template engine.
In pages:
- The page’s configuration settings (from the page’s YAML header) are exposed directly.
- The page’s assets and pagination are exposed through the
assetandpaginationvariables. - The site’s configuration settings (from the
_content/config.ymlfile) are exposed directly. This means that thesitevariable exposes the settings found in thesitesection of the site’s configuration. Thecustomvariable would expose any setting found in thecustomsection. - The blog archives are available through the
blogvariable — unless you have multiple blogs, in which case they are exposed through the name of each blog (so if you named your blogsartblogandcookingblog, each blog’s archives would be exposed through variablesartblogandcookingblogrespectively).
In layouts:
- The page’s formatted contents are exposed in the
contentvariable. Other content segments are exposed directly with their name too. - The page’s configuration settings (from the page’s YAML header) are
exposed through the
pagevariable. - The site’s configuration settings (from the
_content/config.ymlfile) are exposed directly (see above). - The blog archives are available the same way as in pages (see above).
A few system variables are always exposed:
piecrust.versioncontains the current version of the engine.piecrust.brandingcontains a short text proudly telling the world how your site runs with PieCrust (you can see that text right now at the bottom of this page!).piecrust.debug_infoshows a list of available template variables on the current page, along with their values, if the debugging mode is turned on. This is called the debug window.
Sharing templates
You may not want to have all your templates in the _content/templates folder
— e.g. you may want to share some templates with other websites. In that
case, you can put some templates in other folders, including folders outside of
your website’s root folder.
You can specify your custom templates folders with the site/templates_dirs
configuration setting. Paths can be written relative to the website’s root:
site:
templates_dirs:
- ../shared/templates
- ../framework/templates