How to make an inline `if` condition in handlebars partials? - handlebars.js

I have a partial like
{{> form/my-form value=form.value.someValue}}
I want to send someValue to my-form only if a condition is true. Something like
{{> form/my-form value={{#if (condition)}}form.value.someValue{{/if}}}}
But this syntax seems to be wrong.
How to achieve it?

Related

Is there a way to insert a javascript variable into Bigcommerce Handlebars

Using assignVar and getVar (Bigcommerce Handlebars helpers) I'm trying to create and insert a javascript variable like so:
<script>var exampleVar = "string";</script>
{{ assignVar "exampleVar" exampleVar}}
The result when using {{ getVar"exampleVar" }} is that it throws a "not a string error".
I tried {{ assignVar "exampleVar" "exampleVar"}} which outputs "exampleVar" (minus the quotes). I would like it to output "string" (minus the quotes).
Any thoughts?
You can't. Handlebars is already compiled in the server before it reaches the front end side. If the logic is done in javascript, then you should display the result via javascript too.

Can't figure out the structure of the if/then statement for SendGrid Template using custom field

I have used a lot of the email marketing platforms, but I don't think I am understanding the developer guidelines for using Handlebar.js with SendGrid to use If/Then statements.
I know my way around the code, but the guide isn't clear how to structure the statement for use with their custom fields. It looks like there needs to be some sort of path. Or maybe not. The examples they use don't really help.
https://sendgrid.com/docs/for-developers/sending-email/using-handlebars/#basic-if-else-else-if
I've basically rewritten the code over and over and sent myself some tests. I can get the field to show up without the if-then statement, but other than that its a no go. I have also written tech support, but have not heard back yet. No chat feature :/
{{#if city_code=avl}}
AVL
{{else}}
not avl
{{/if}}
The email only contains the {{else}} part of the code. So in the above example "not avl".
I hope you already found an answer to your question though Sendgrid does not allow to extend Handlebars with extra plug-ins contrary to suggested above.
What you need is the following construct,
{{#equals city_code avl}}
AVL
{{else}}
Not AVL
{{/equals}}
https://sendgrid.com/docs/for-developers/sending-email/using-handlebars/#conditional-statements
{{if anything}} - only check whether anything is truthy or falsy, you cannot compare it to something.
The if statement verify only if the variable is true.
In order to achieve your example, you should create a new helper:
Handlebars.registerHelper("eq", function(a, b, options){
if (a == b) {
return options.fn(this);
}else{
try{
return options.inverse(this);
}catch(e){}
}
});
And your handlebar template will be:
{{#eq city_code avl}}
AVL
{{else}}
not avl
{{/eq}}

handlebar template looping over json data

So I have these two data structures that I need to loop over and both of these will output the expected values
{{#each viewData.activity}}
{{label}}<br/>
{{/each}}
<hr>
{{#each viewData.maxActivity}}
{{label}}<br/>
{{/each}}
But I need to perform the following loop but it's not working as I have it below:
{{#each viewData.activity}}
{{label}}<br/>
{{../viewData.maxActivity.label}}<br/>
{{/each}}
The ../ should be working but it isn't. What am I missing?
I found in another SO post (https://stackoverflow.com/a/26341035/483140) and have the following working but doesn't seem efficient:
{{#each .}}
{{#activity}}{{activityLabel}}{{/activity}}<br/>
{{#maxActivity}}{{activityLabel}}{{/maxActivity}}<br/>
{{/each}}
Okay, so I tried this in my code and it's working great.
{{#each .}}
{{#activity}}{{activityLabel}}{{/activity}}<br/>
{{#maxActivity}}{{activityLabel}}{{/maxActivity}}<br/>
{{/each}}
In a small code sample, this looks odd but in a larger form, it actually makes more sense, visually which might have been my hang up to begin with.

How do I include a variable in an "ifequal" handlebar?

I'm using Foundation for Sites which uses Panini and Handlebars for JS templating. I need an {{#ifequal}} statement that includes a js variable so that I can compare a URL parameter to a JSON
What I have right now:
{{#ifequal "Canada" this.destination}}
//do the thing
{{/ifequal}}
What I need is something like this:
{{#ifequal <script>document.write(destId)</script> this.destination}}
//do the thing
{{/ifequal}}
The js var "destId" is assigned earlier in the page when it's pulled out of the URL parameters, but of course I can't include the script inside the handlebars. If there's a way to pass a URL parameter directly into a handlebar that would also work.
as noted before on this question (link is here):
Handlebars partials take a second parameter which becomes the context for the partial:
{{> person this}}
In versions v2.0.0 alpha and later, you can also
pass a hash of named parameters:
{{> person headline='Headline'}}
You can see the tests for these
scenarios:
https://github.com/wycats/handlebars.js/blob/ce74c36118ffed1779889d97e6a2a1028ae61510/spec/qunit_spec.js#L456-L462
https://github.com/wycats/handlebars.js/blob/e290ec24f131f89ddf2c6aeb707a4884d41c3c6d/spec/partials.js#L26-L32

How to access JavaScript object other members data whilst iterating over array in handlebars each statement

I have a JavaScript object like this
Object {all: Array[5], cattype: "sometype"}
My question is, how to write cattype in the code below
I do this
{{#each all}}
<li><a href="#{{cattype}}/{{ id }}" >{{{title}}}</a></li>
{{/each}}
This code is working, just cattype is not written. Outside {{#each}} it's working of course.
Thank you very much for your attantion ! :)
I found solution
{{../cattype}}
solved my problem

Resources