Comparison in meteor blaze templates - meteor

{{#each month in showMonths}}
<td>
{{#each orderGetter}}{{ month.currMonth }} :: {{period}}{{/each}}
</td>
{{/each}}
showMonths is an array of dates. It loops and the idea is that it then matches dates retrieved by the orderGetter template helper. For the purpose of debugging the above simply generates the two values to be compared. This results in:
Mon Feb 01 2016 00:00:00 GMT+0000 :: Mon Feb 01 2016 00:00:00 GMT+0000
Using an equality template helper to compare the 2 dates, it always returns false. Even though as you can see they're identical. This also happens when I run the dates through moment in the equality helper.
The equality code:
{{#each month in showMonths}}
<td>
<input type="text" min="0" class="form-control" value="{{#each orderGetter}}{{#if matcher month.currMonth period}}foo{{/if}}{{/each}}">
</td>
{{/each}}
Template.registerHelper('matcher', function(a, b) {
return a ===b;
});
Kinda pulling my hair out.
Any ideas?
Thank you

Date.parse() on the matcher function seems to do it.

use == not === here
=== implies it is the SAME object, while == checks it is equal.

Related

Handlebars: Using #key in dot notation

This should be a simple thing to do in Handlebars, but I can't seem to do it.
I have two objects:
a: {
key1: "w",
key2: "x"
}
b: {
key1: "y",
key2: "z"
}
I'm trying something like this:
{{#each a}}
{{b.#key}}
{{/each}}
<!-- Should output "y z" but it doesn't! -->
What am I doing wrong here?
I am not really sure if you can use "#key" in the dot notation, because every-time you try doing the same, Handlebars compilation fails.
Moreover, b is not an object present in a. So, if you're iterating over a, you can never get keys of b.
{{#each b}}
{{#key}}
{{/each}}
This is the block of code that would iterate over "b" object, and would print all the keys present in b, meaning "y" and "z".
However, if you change your JSON to,
a: {
'kx':'x',
'ky':'y',
b:{
'by':'y',
'bz':'z'
}
}
then, you may get the keys of b, by saying,
{{#each a.b}}
{{#key}}
{{/each}}
Which would again, give you the desired result, i.e. by and bz.

Jade Template in Meteor - Conditional Comparison Syntax Error

Trying to compare two template variables is resulting in an error. This seems consistent with jade syntax. What's wrong here?
Code:
if name == current_project.name
| Current
else
a.nav-project-selection Set as Current
Error:
While building the application:
client/templates/account.jade: Jade syntax error: Expected identifier, number, string, boolean, or null
{{#if name == current_project.na...
^
SOLVED
Since Jade compiles down to Spacebars which doesn't support comparison, you have to do it via a helper function. The following worked for me:
client.js
UI.registerHelper('equals', function(a, b) {
return (a === b);
});
template
if equals name current_project.name
| Current
else
a.nav-project-selection Set as Current
An easier way is to use the Meteor-handlebar-helpers package.
meteor add raix:handlebar-helpers
Check the readme file to get an overview of the helpers. In your case:
if $eq name current_project.name
| Current

"Failed to compute" error because of unexpected operation inside #if

Here is the problem (Ractive.js v0.7.3): http://jsfiddle.net/Inversion/hLym2xcy/
r = new Ractive
el: 'cont'
template: '''
{{#task.options}}
{{#if pros}}
inside if
{{#each filter(.pros)}}<li class='{{set_class(.)}}'>+{{.v}}</li>{{/each}}
{{/if}}
{{/task.options}}
'''
data:
task:
options: [{pros:[{v:1}]}]
set_class: (o)-> o.v
filter: (arr)-> console.log(arr);arr || []
setTimeout(
-> r.set('task.options.0.pros', undefined)
2000
)
On r.set operation "inside if" is not printed out as expected, but #each tries to work with an undefined array which is not expected.
I cannot guarantee presence of an pros array and that's why I used {{#if pros}} to do not use it, but seems like it doesn't work.
console.log(arr) is added to see if filter is actually called, and it is.
Also when I try to return [] from the filter it still calls set_class which is unexpected because there is nothing to iterate.
Is there a way to overcome these unexpected problems?
Thanks!
Seems like it is a known problem and will be fixed in v0.8

Freemarker iteration from Spring-bound list

I have a List<String> bound by a Spring ModelAttribute. How do I iterate over it in Freemarker?
I've tried:
<#list ${modelattribute.listField} as thing >
<tr><td>${thing}</td></tr></#list>
</#list>
But Freemarker is not expecting the escape sequence
Encountered "{" at line 108, column 43 in things/search.html.
Was expecting one of: ...
How should I reference this value?
You should do something like this:
<#list modelattribute.listField as thing >
<tr><td>${thing}</td></tr></#list>
</#list>

How to represent Array of integers in handlebar.js

I have a json object "FoundAt" : [1, 3]
How do I represent the values in the array using handlebar.js?
If you are referring to iterating the array you can do so using {{each}} block, with reference to the above defined array use the following:
{{#each jsonObjectName.FoundAt}}
{{this}}
{{/each}}
OR
{{#each number in jsonObjectName.FoundAt}}
{{number}}
{{/each}}
Look for Simple Iterators in the official documentation

Resources