Learn

Templates

Shaping different pages within your site.

A template is the way one type of page is organized, and how page content is displayed within it.

Templates sit between layouts and page content. Where a layout is the outer-shell of your website that doesn’t change page-to-page, templates are the inner-shell that tells Statamic how to render the content for a page.

Statamic replaces the {{ layout_content }} tag in your layout with the chosen template for the page being viewed (and likewise, within that template, Statamic replaces each tag with the appropriate value from the page’s content).

An Example of Templates

You’ll usually create a template of each type of page that you want to display. A list of templates used by a standard blog might be:

  • home page template
  • blog post detail page template
  • category list page template
  • general static content page template

With this list of templates, we have all of the ways content will be displayed on our site. Each page is told which template to use to correctly display the page’s content.

Placing Page Variables & Content

You’ll use Statamic’s templating system to display page content exactly as you’d like. To get your page’s main content onto your template (everything below the second set of --- in each your content files), you’ll use the {{ content }} tag.

An example of this in action might look something like this:

<div class="columns">
    <article>
        {{ content }}
    </article>

    <aside class="sidebar">
        <!-- sidebar content would go here -->
    </aside>
</div>

Naming Templates

By default, Statamic will use the default template. This will be the code contained in default.html within your currently chosen theme’s templates folder. You can name your layouts whatever you’d like, but if you choose a name other than default for your default template, you’ll need to set that on every page (or within every folder as the default for each page within that folder).

Choosing a Template

Per Page

To tell one page of your site to use a specific template, you just need to set _template in your page’s front-matter. For example, if you want to use the blog-detail template for a blog post, your entry’s front-matter might look like this:

---
title: My Trip to Spain
_template: blog-detail
---
It rained all over the plains the whole time I was there.

This will use code from blog-detail.html within your current theme’s templates folder. If that file doesn’t exist, Statamic will display an error.

Per Folder

If you want to change the template for an entire folder of your site, you can set _default_folder_template on the page file of that folder. Your folder’s page file might look something like this:

---
title: My Blog
_default_folder_template: blog-detail
---
Welcome to my blog!

Note that you can then override the template per page by setting _template on the pages you want to change.

This article was last updated on March 30th, 2016. Find an error? Please let us know!