How to add ghost handlbar helpers to js node script

Not sure if I have this in the right category, please let me know if I don’t, I’ll update.

I have created a custom theme and a node js script that creates posts and inserts them into ghost via the ghost API. Both of these are presently working as expected. I can add the posts, and I can update elements of the theme and rebuild of files being saved via npm run dev.

Now I would like to use some of the ghost handlebar helpers in my theme.

This fails in my script with the following error (for the asset helper/partial):


stderr: Missing helper: "asset"

I think this is because I am missing some sort of require() statement in my script where I would include ghost itself. The only ghost related require I have is for the api:

// // The admin API client is the easiest way to use the API
const GhostAdminAPI = require(“@tryghost/admin-api”);

Is there an npm module I need to install and then require? Some other magic incantation?

Thanks in advance for your help!

You shouldn’t need the Ghost API to develop your theme, as your script and theme will run in different contexts.

How are you loading your theme? It should be related to an installation of Ghost, but your post suggests that it’s not.

Yes, I understand it’s different contexts. I’ve set the theme in for itself to my theme, it works fine.

The context in asking about is the script that is going to create many thousands of initial pages. The script also works fine, but now I have to refund the theme (in both contexts) as part of the development effort.

So the question is about how to expand ghost partials in the script context.

More specifically, at this point, I am trying to include certain theme specific images on the heated pages (such as icons and logos).

These are located in (ottomh, not in front of laptop right now) theme/content/images.

I suspect I can use {{asset}} in the SRC property of an HTML tag (or similar).

But my script doesn’t seem to have access to that right now.

I don’t fully follow, your script should have the data for the initial pages, which can be inserted via the API, and then you can let Ghost render the page by requesting the associated post URL.

But if you’re trying to render files in the theme as part of the script (maybe it’s just me, but this seems like the wrong path to go down), here’s the code to the Ghost asset helper:

I don’t strictly have to use the ghost helpers I suppose.

But where exactly is the url that {{assets}} would return for an image in the theme, e.g. mytheme/images/myimage.png?

I can’t find any documentation for this. I could hard code it in my script or put it in a .env file, that’s fine for now.

I think it might be simpler to put it in an env file if it won’t change often

That’s easy enough to do. But what’s the actual url? I’m not using this theme as a template, so I don’t have an actual post I can look at the url. That’s the big question :stuck_out_tongue_winking_eye:

Ahh, your best bet to check what the final output is to install Ghost, or use an existing installation (e.g. demo.ghost.io)

I guess I’ll have to do the experiment and then post the answer here. Seems like something basic that should be documented

OK, I figured this out. I’ll provide the solution for the next person that has a similar issue.

This being my first theme (but not my first rodeo :) ), I was overthinking it.

I realized that my content is going through handlebars twice.

The first time is when my post creation script is run. The second is at display time.

The first time uses a handlebars file, let’s call it barry.hbs, that is used at the time my script runs. The output of the script is HTML that is inserted as post content in a post that the script creates using the ghost api.

The second time is when ghost displays the post. The template for that is “post.hbs”, and the {{asset}} helper works fine there.

in post.hbs, I can add:

and it works.

At display time, this returns:

which is as-expected.

From this, I learn that in the script file, where I want to put an image (within the post content), I can use this html tag (or similar as needed):

This works as expected.

Thanks @vikaspotluri123 for your assistance!