Hey Guys,
I’m trying to create custom Handlebar helpers in my Ghost CMS v5.109.6 installation. I’ve written a helper, but I’m having trouble getting it to work. I’d love some guidance on how to implement it correctly. I’m struggling with getting it to work at all.
I’ve created a helper called user_local.js
, placed it in the current/core/frontend/helpers/
directory and restarted Ghost. The code is as follows:
// # User Language / Local Helper
// Usage: `{{user_local}}`
//
// Outputs users language as string. Will strip region from local setting.
module.exports = function user_local() {
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Locale
const locale = new Intl.Locale(navigator.language);
return locale.language;
};
In a nutshell, I want to get the users current language setting to determine which blog posts to render. To check if this helper is working, I’ve added the following code to the top of my default.hbs
template file:
{{log 'USER_LOCALE =' user_locale}}
But the development logs of my Ghost install keeps saying “USER_LOCALE = undefined
”. I tried this with a “Hello World” helper too, that just returns a string - but with the same result. The theme engine doesn’t seem to recognize my custom helpers at all.
What I actually want to do is a match
block based on the users language settings. E.g. like this:
{{!-- Match for user language / local --}}
{{#match user_locale "de"}}
{{#get "posts" filter="tag:[hash-de]" order="published_at desc" page=pagination.page}}
{{#foreach posts visibility="all"}}
{{> loop/classic-article}}
{{/foreach}}
{{/get}}
{{else}}
{{!-- Use the site local as default --}}
{{#get "posts" filter="tag:[hash-{{@site.locale}}]" order="published_at desc" page=pagination.page}}
{{#foreach posts visibility="all"}}
{{> loop/classic-article}}
{{/foreach}}
{{/get}}
{{/match}}
Can someone please help me understand what I’m doing wrong? Is there a specific configuration or setup I need to perform to get custom helpers working in Ghost CMS?
Any guidance or advice would be greatly appreciated!