I’m newly trying Ghost blog, and I’ve seen an article about adding custom helpers. - https://dotnetfalcon.com/create-custom-helpers-to-add-functionality-to-your-ghost-blog/
BTW, in my installation (version 3.15.1), I can’t find the “helpers” directory in “core/server”…
Is the “helpers” directory moved to somewhere…?
Or am I misunderstanding the above article…?
Thank you for reading my question.
If you’re looking for some help, it’s important to provide as much context as possible so that people are able to assist you. Try to always mention:
- What’s your URL? This is the easiest way for others to debug your issue >>> slfvn.com
- What version of Ghost are you using? >>> 3.15.1
- What configuration?
- What browser? >>> Google Chrome
- What errors or information do you see in the console?
- What steps could someone else take to reproduce the issue you’re having?
Yep, it got moved to core/frontend/helper
1 Like
Thank you for your response…!
BTW, I still can’t get the meaning of this…
Navigate back to the core
folder to install all the dependencies your custom helper might use. Ghost uses yarn, so you’ll have to issue yarn add packagename
command, while in this folder.
I’ve made a test.js
file (as below) in the helpers
, but it seems Ghost is not finding the new helper file.
module.exports = function test(options) { return “test”; };
At the same time, I’ve added a more line in index.js
as below.
helpers[ ‘test’ ] = require( path.join( __dirname, ‘test.js’ ) );
But it seems the result is same, no recognition of the new test.js
helper by {{{{test}}}} or {{test}} in themes.
What should I do more for adding a custom helper…?
I guess there is a more step too add (register) custom helper to Ghost, but I don’t have any idea for this, please help me.
Hm, I think I found the way…
In the core/frontend/services/themes>handlebars/helper.js
, there are all registered helpers.
Just for another person who may need a custom helper, the summarize procedure is same as the below.
1.
Make your own helper in current/core/frontend/helpers
.
BTW, if you’re curious how is a helper’s code shape, I recommend to check current/core/frontend/helpers/raw.js
for it, because it is the shortest helper code in Ghost.
2
Open current/core/frontend/services/themes/handlebars/helpers.js
, and add your own helper.
If you see the file, you may not need any further explanation, because it has a very simple structure as `registerThemeHelper( ‘your_helper’, coreHelpers.your_helper );’
BTW, I think if Ghost has another updating which change the folder structure the above may be changed, so at that time, I think searching ‘registerThemeHelper’ will be the one of the solutions to find the helpers.js
.
Hey Jay. I was fiddling around with exactly that. But that will only work unless you are not in PRODUCTION mode.
Because in production - for whatever reason - the ghost core will LIVE-CHECK the validity of the theme by checking its used helpers. So if you did the proposed steps it will notice “hey you are on production and you added the helper abc.js whatever and I am gscan
and telling you this is invalid”.
TLDR i actually ended up automating this behaviour by doing what you said via node script in a Dockerfile PLUS I did knownHelpers.push('myHelperName')
within ${GHOST_PATH}/node_modules/gscan/lib/specs/canary.js