Skip to content

Templating

Thane Thomson edited this page Jun 27, 2016 · 9 revisions

Statik uses Jinja2 as its templating engine, so if you're familiar with Django's templating engine it'll be pretty easy to get into using Statik's.

This guide will only cover the basics of Jinja2 templating. For more details, the Jinja2 Template Designer Documentation will help.

Variables

One of the most powerful features of any templating engine is its ability to embed variable values when rendering the templates. Statik template variables can come from two different places:

  1. View context - Every view can also define context variables (both static and dynamic). See the Views documentation on how to configure views.
  2. Project context - In your config.yml file within your project, you can define a context variable with both static and dynamic context. See the Project configuration documentation for more details.

The above order also gives the order of precedence for your variables (i.e. if a view context variable and project context variable both have the same name, view variables will override project variables).

Static Context Variables

When defining static context variables, their exact value will be inserted into your template when rendering. Say, for example, your config.yml file defines the following static context:

# config.yml
# ...
context:
  static:
    site-title: This is my page title!

And say, for example, your template looks like this:

<!DOCTYPE html>
<html>
<head><title>{{ site_title }}</title></head>
<body>
  <!-- page body goes here... -->
</body>
</html>

When your template is rendered, it will contain the following HTML content:

<!DOCTYPE html>
<html>
<head><title>This is my page title!</title></head>
<body>
  <!-- page body goes here... -->
</body>
</html>