Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: "any" conditional helper #39

Open
de-vo-id opened this issue Aug 20, 2024 · 1 comment
Open

Feature request: "any" conditional helper #39

de-vo-id opened this issue Aug 20, 2024 · 1 comment

Comments

@de-vo-id
Copy link

de-vo-id commented Aug 20, 2024

Description

Hello! I found your plugin earlier, and have been enjoying it a lot. However, I've ran into a bit of a roadblock with my recipe template. I wanted to specify a sort of "or" or "any" conditional statement in order to only create a nutrition category if those values are available from the recipe website. Since a helper like that isn't built into Handlebars and a custom one isn't provided (and using a bunch of nested if statements isn't my jam) I very quickly came up with my own and shoved it into the main.js plugin file. It's been working very well for me, but it would be awesome to see it included in the plugin by default.

Plugin code

I'm quite a noob with JS and TS so these might be a bad way of doing it or prone to breaking everything. Likely can be refined.

The helper function (for the compiled main.js):

handlebars.registerHelper("any", function(...args) {
return args.some(arg => Boolean(arg));
});

The helper function (for main.ts):

handlebars.registerHelper("any", function (...args: any[]): boolean {
    return args.some(arg => Boolean(arg));
});

Handlebars template usage

How I've used it:

{{#if (any nutrition.calories nutrition.carbohydrateContent nutrition.proteinContent nutrition.fatContent nutrition.fiberContent nutrition.sugarContent nutrition.sodiumContent)}}
{{!-- Create category for nutrition details and specify further logic for the individual nutrition values--}}
{{/if}}

There's a chance that all of this is completely useless and I'm an idiot for missing something that's already built-in. If not, then I'd love for you to consider adding it!

@seethroughdev
Copy link
Owner

Hi @de-vo-id , I'm so sorry i missed this earlier! Thank you for taking the time to right it up.

I can't think of reason not to add this. it seems very straight forward.

@Lanjelin , you have more Handlebars experience, am I missing something here, or I go ahead and put it in the next release?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants