Ghost: If statements in Handlebars

Created on 17 Oct 2015  Â·  11Comments  Â·  Source: TryGhost/Ghost

I want to add an if statement to check if the navigation label equals a certain ...label... Is this possible? (The idea is if it equals a specific static post in the navigation, it would add a dropdown menu).

I assumed something like {{#if navigation.label === "value"}} would work, but that's more of javascript logic than handlebars.

Most helpful comment

_sigh_

All 11 comments

[Comment deleted: See subsequent comment for the code snippet.]

can i see into this one

What is your use case for this? Handlebars very intentionally comes without
if helpers.
On Oct 18, 2015 2:50 PM, "BlehDes" [email protected] wrote:

can i see into this one

—
Reply to this email directly or view it on GitHub
https://github.com/TryGhost/Ghost/issues/5966#issuecomment-149008582.

snippet

I attached a jpg with the code. It essentially allows me to shiv a sub-menu into the Ghost populated navigation. Since Ghost doesn't do it natively (or archiving, listing of blogs, etc except RSS), I had to find a way to get it to work.

It works live www.builtbygoat.com (albeit the CSS was a pain to finagle).

It isn't a pretty solution. I was hoping for a better alternative. I haven't really played with handlebars as much yet, just the few snippets Ghost uses.

I'm not sure where to register helpers in Ghost, but in Handlebars this can be solved with a eq helper like this:

Handlebars.registerHelper('eq', function(a, b) {
  return a === b;
});

And then in your template with Handlebars use subexpressions:

{{#if (eq a b)}}
  ...
{{else}}
  ...
{{/if}}

Good call topaxi.

We would have to add to ghost/core/server/helpers/ to create this kind of functionality. Aka, get it into the repo (or every time we'd upgrade, we'd have to redo it).

Lemme see how to go about fiddling with the helpers.

Yeah, looks like the only real thing would be to add a new js file in /helper/ (which would require changing other js files to load it) that would allow you to do:

{{#foreach navigation}}
{{#if (eq a b)}}
subnav
{{/if}}
{{/foreach}}

Hi @kaiserhase, this sort of logic is, as you have discovered, not possible with handlebars. The Ghost theme documentation is here: http://themes.ghost.org and the handlebars documentation is here: http://handlebarsjs.com/

In the short term your best bet is to use JavaScript, the way you have done. Ghost's navigation menu exists to serve the majority of use cases for blogs - which is a very simple flat menu. If that's not what you need, then the expectation is you would hard code your own menu that fits your needs.

If you have any further questions, please join the #themes channel of our slack community. GitHub is for tracking bugs & issues with the Ghost software.

_sigh_

I was considering replacing the if statements with case statements, I was
once in that kind of situation

On Sun, Oct 18, 2015 at 6:45 PM, Brett [email protected] wrote:

_sigh_

—
Reply to this email directly or view it on GitHub
https://github.com/TryGhost/Ghost/issues/5966#issuecomment-149025849.

Was this page helpful?
0 / 5 - 0 ratings