Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

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

Posted on • Originally published atdev.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 throughthe 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 atOh 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 modeExit 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 theget and theforeach loops to not use pattern matching and instead used theslug itself in both thefrom andto sections. This resulted in the following:

{{#get "posts" limit="all"}}    {{#foreach posts}}        {"from":"/{{slug}}/","to":"/blog/{{slug}}/","permanent":true},<br>    {{/foreach}}{{/get}}
Enter fullscreen modeExit 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 thetitle only before adding thelimit="all",<br>, and then fixed the output to use theslug 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)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Hi! I’m been working on developing apps to make my life easier and through it I’ve come across some tips that may make your life easier too. I share all general tips to my website SimplyKyra.
  • Location
    Seattle Area, Washington
  • Education
    University of Alberta: Computing Science with Bioinformatics major
  • Pronouns
    she/her
  • Work
    Own and manage SimplyKyra LLC
  • Joined

More fromKyra

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp