DEV Community

Cover image for Using Handlebars Code to Generate Ghost YAML
Kyra
Kyra

Posted on • Originally published at dev.to

Using Handlebars Code to Generate Ghost YAML

A bit ago I realized I needed to generate redirect text for each of my current blog posts on my Casper themed Ghost website. This seemed to be an overwhelming task but after talking it through both my husband and I came up with two potential ideas. I shared both how to find the redirect section through the Ghost interface along with his earlier idea that used the data file, jq, and visual filtering in both the prior posts in this series. Now let's see how I realized we could code this using handlebars in case it also helps you. That said, if you are looking for my original full post you can check it out at Oh No, I Need to Create Redirect Text for All My Posts!.

Quick warning: As I used the previous method in this series to generate my redirect text I didn't need to fully implement this method yet I still wanted to see if it was valid so I quickly coded it up on localhost to confirm it generated what looked like the right output but I didn't work on it any further beyond that. That said if I was to redo this... this method would be what I used this time around.

The Plan

My localhost is about a year behind in content from my current website so if I wanted to use the output of this I would've needed to, temporarily, have this run on my main website. My original plan, after confirming this worked locally, was to create a hidden non-linked page, connect it to the handlebar code, push to website, confirm the resulting text looked good, copy the results to my redirect file, upload, confirm a couple redirects worked, and then undo my code and page changes.

Warning

As I haven't tested this in my redirect file and your website is probably set up differently please confirm it's right before using it yourself.

The Code

I first wrote this up after using the redirect text generated in the first method that used pattern matching. Thus my first bit of code looked like this:

{{#get "posts" limit="all"}}
    {{#foreach posts}}
         {"from":"^/({{slug}})/$","to":"/blog/$1","permanent":true},<br>
    {{/foreach}}
{{/get}}
Enter fullscreen mode Exit fullscreen mode

After looking at my earlier redirect text from when I first moved my website from Wordpress to Ghost I updated the text within the get and the foreach loops to not use pattern matching and instead used the slug itself in both the from and to sections. This resulted in the following:

{{#get "posts" limit="all"}}
    {{#foreach posts}}
        {"from":"/{{slug}}/","to":"/blog/{{slug}}/","permanent":true},<br>
    {{/foreach}}
{{/get}}
Enter fullscreen mode Exit fullscreen mode

With either code segment you get, hopefully, working redirect text that you can quickly copy, paste into the file, look over, and upload.

In all the journey was a bit longer to get this than shown as I started with a simple get and foreach loop showing the title only before adding the limit="all", <br>, and then fixed the output to use the slug along with the entire redirect text I wanted.

Images showing the process in a collage.

And with that hopefully it worked! Just don't forget to remove the code from your website when done. And I hope you're having a great day.

Top comments (0)