Bolt.tips

How to fetch CMS content like a pro

Have you not heard of Bolt CMS before? Let me fill you in on the big picture in under 30 seconds: Bolt CMS is the content management system built for all three user groups: you, the implementor, the content editors, and the site end users. At Bolt HQ, we are a bunch of pragmatic web developers who make a living out of building websites for clients. We know a thing or two about CMS features that content editors are looking for, so with the help of our open source community, we baked them into Bolt CMS. Bolt gives you the Lego blocks, it’s up to you to decide if you are building a megapolis, or a quiet little house on the side of the road. It’s completely free and open source.

It’s time to fetch CMS content like a pro. No SQL skills needed!

In the last Bolt tip, you saw how to create as many Content Types as are necessary to build your content. Now, it’s time to show you how easy it is to fetch specific content on any page that you like, in addition to the default listing and record pages that Bolt creates for you.

Example one: Fetching a list of posts

Let’s assume you are using the same posts Content Type as last time. There it is, for ease of reference:

    posts:
      name: Posts
      singular_name: post
      fields:
        title:
          type: text
        body:
          type: redactor

Already, you can use two template files in your theme: post.twig and posts.twig, to show a single post and a list of posts, respectively.

But, suppose you’d like to show a couple of news posts on your homepage. How do you do that in your Twig template? By using Bolt’s special setcontent tag.

{% setcontent posts = 'posts' %}

Example two: Limiting the number of results

By default, Bolt will limit the number of results to 20, so that your website remains fast to load. Let’s say, however, that in our homepage example we only want 4 posts. Easy!

{% setcontent posts = 'posts' limit 4 %}

Now, the posts variable will only contain 4 records.

Example three: Getting the most recent posts

We can expand our existing query to only grab the 4 most recent posts, by adding a single new keyword:

{% setcontent posts = 'posts' limit 4 latest %}

We still get 4 posts, but now we guarantee that they will be the 4 most recently created items, not just any 4 posts.

Example four: Getting the posts of an author

Now, let’s assume that we want to get the 4 most recent posts by a certain author. This can be useful for making suggestions for further reading at the bottom of a post.

In this case, we’ll also use

{% setcontent posts = 'posts' where { author: 1 } limit 4 latest %}

Or better yet, we can avoid the hard coded author ID, and instead use the same author as the post on the current page. We do this by using Bolt’s getuser function.

{% setcontent posts = 'posts' where { author: record.author.id } limit 4 latest %}

Example five: Get a random selection of posts

Okay, what if instead of showing the latest records of the author, we want to show any 4 random posts that they have made. That’s also easy :-)

{% setcontent posts = 'posts' where { author: record.author.id } limit 4 random %}

Just replacing latest with random and we’re good to go!

Example six: Ordering results

So, in the previous example we got ourselves 4 random records. Assume we want to order then by publication date (most recent first), here’s how to do it:

{% setcontent posts = 'posts' where { author: record.author.id } limit 4 random order '-publishedAt' %}

Example seven: Getting today’s posts

Yes, that’s also possible!

{% setcontent posts = 'posts' where { publishedAt: '>today && <tomorrow' } %}

Example eight: One or multiple results

Sometimes, you only want to get one record, or always get an array of records to iterate over. Luckily, Bolt handles edge cases, such as having just one result content, pretty well.

{% setcontent post = 'posts' order '-publishedAt' returnsingle %}

The example above will return just one post , the most recent one.

{% setcontent posts = 'posts' order '-publishedAt' returnmultiple %}

By using returnmultiple, you make sure that the posts variable will always return an array that can be used in a loop.

Example nine: Pagination

Sometimes, there’s a lot of records to show. In that case, it is better to use pagination. Thankfully, that’s easy too:

{% setcontent posts = 'posts' page 5 %}

Alternatively, you can use a query parameter. Let’s say there’s a ?page=5 :

{% setcontent posts = 'posts' page app.request.get('page', 1)

Notice the 1 parameter? This is an edge case, when there is no query parameter named page, we assume it’s the first page.

Example ten: Printing the query for SQL enthusiasts

Are you an SQL enthusiast who just cannot let go without seeing the magic under the hood? No worries, Bolt lets you see the SQL query that it executes to fetch the content:

{% setcontent posts = 'posts' printquery %}

The next steps?

If you’re looking to learn more, here are useful resources to help you get started:

Happy building! 🛠⚙️

Permalink - Written by Ivo on June 28, 2021.

« It's ridiculously easy to set up new page types in Bolt CMS