State
There are three types of state in Aurora: page, post and site.
Page State
Page state stores values that are only available on that page.
For example, consider the following template:
---
title: Hello, World!
layout: default
---
Welcome to the website!
Any value in the front matter is stored in the page state. This state can be accessed using:
{{ page.title }}
Tip
You can access the name of the template from which a page was generated with:
{{ page.generated_from }}
This is useful if you want to make a public edit page to a GitHub repository, like the one in the footer of this documentation.
Post State
Post state stores information about a blog post.
You can access post state on any template that is used by a post.
For example, consider the following template called pages/_layouts/post.html for rendering a blog post:
---
layout: default
---
<h1>{{ post.title }}</h1>
<p>{{ post.content }}</p>
Here, we access the title and content of the post using the post state.
This template (pages/_layouts/post.html) inherits from the default layout, and could be used on any blog post with:
---
layout: post
title: Hello, World!
---
...
Site State
Page state stores values that are global to the website.
You can access site state on any page.
By default, site state contains:
- A list of your posts (
site.posts
) - The root URL of your site (
site.root_url
) - The build date of your site (
site.build_date
) - A list of all pages in your site (
site.pages
)
For example, consider the following template:
---
title: Blog Home
layout: default
---
<ul>
{% for post in site.posts[:5] %}
<li>
<a href="{{ post.url }}">{{ post.title }}</a>
</li>
{% endfor %}
</ul>
Here, we iterate over the first five posts in the site state and display them on the page.
The above code could be used on a home page to display the most recent posts.
You can add custom values to your site state by adding to the SITE_STATE dictionary in your config.py file:
SITE_STATE = {
'site_version': os.getenv('SITE_VERSION', '1.0.0')
}