Add CMS content to bridgetownrb

Written by:Matsimitsu(Robert Beekman)MatsimitsuRobert Beekman

It took me a while to figure out where to place the code that loads data from an external CMS and makes it available in bridgetownrb.

The idea is to place a builder in the builders folder. This builder is called each time the site is... built.

plugins/builders/posts_builder.rb

This file should contain a class with a build method. Bridgetown exposes a get method that allows you to fetch any data without having to worry about using a specific HTTP client. It also parses the JSON for you, if present.

In my case I'm getting data from a pocketbase instance and for each item, I'm adding a resource to bridgetown.

class Builders::PostsBuilder < SiteBuilder
  def build
    get('https://pocketbase/api/collections/blog_posts/records?sort=-date&perPage=1000&expand=files&filter=(public=true)') do |data|
      data[:items].each do |post|
        add_resource :posts, "#{post[:slug]}" do
          ___ post
          layout :post
          permalink '/blog/:slug.html'
        end
      end
    end
  end
end

The first argument is the collection (in this case posts), and the second argument the "id" of the resource. You can alter the layout and permalink by using the DSL.

You can now call this resource anywhere in your bridgetown code:

site.collections.posts.resources

Like

Webmentions

No mentions yet.