HOWTOs / Making a Singleton ContentType
Note: You are currently reading the documentation for Bolt 5.0. Looking for the documentation for Bolt 5.2 instead?
In a nutshell; a singleton is a ContentType that contains only 1 record.
You can use singletons for all kinds of things, from complex homepages, single page applications to a general settings page for your website.
When you set a ContentType to be a singleton, you can use it to update some aspects of the layout for that ContentType's record, making your experience more fluent.
Creating the ContentType¶
To make a ContentType behave as a singleton, you need only add singleton: true
to its configuration in contenttypes.yml
.
Example: A "settings" singleton¶
In this example we'll run you through creating a settings ContentType, here you can allow your site manager to edit things like social media URLs, non-page specific content, and anything you feel a user should be able to manage independent of the template designer.
Note: Adding sort: -id
will make
sure you can grab the last record created when fetching it in a template, even
if you've deleted and created the post multiple times and the ID has been
incremented.
Using a singleton record in your template¶
As a singleton is just a ContentType much like any other, you can fetch the
record with like normal using setcontent
.
For example, to fetch the record and display some of its content, the Twig template would contain something similar to:
Couldn't find what you were looking for? We are happy to help you in the forum, on Slack or on Github.