I am just starting with handlebars
and I am trying to do a simple double for loop in order to have all the day's time with 15 minute intervals. A very weird thing is happening where if the child and parent have the same values, the view
object is being returned instead. This is my code:
var handlebarsExpress = require('express-handlebars').create({
helpers: {
for: function(from, to, incr, block) {
var accum = '';
for(var i = from; i <= to; i += incr) {
accum += block.fn(i);
}
return accum;
}
}
});
app.engine('handlebars', handlebarsExpress.engine);
app.set('view engine', 'handlebars');
...
...
In the view file (sth.handlebars
) i have this:
<div class="row columns large-12">
{{#for 0 23 1}}
{{#for 0 45 15}}
{{log ../this}}
<span>{{../this}}:{{this}}</span><br>
{{/for}}
{{/for}}
</div>
The html output is something like this:
[object Object]:0
0:15
0:30
0:45
1:0
...
...
13:45
14:0
14:15
14:30
14:45
15:0
[object Object]:15
15:30
15:45
16:0
16:15
The log
helper reports the following in the terminal:
{ settings:
{ 'x-powered-by': true,
etag: 'weak',
'etag fn': [Function: wetag],
env: 'development',
'query parser': 'extended',
'query parser fn': [Function: parseExtendedQueryString],
'subdomain offset': 2,
'trust proxy': false,
'trust proxy fn': [Function: trustNone],
view: [Function: View],
views: '/home/elsa/Projects/rental-borg/project/views',
'jsonp callback name': 'callback',
'view engine': 'handlebars',
port: 8765 },
flash: { info: undefined, error: undefined },
_locals: { flash: { info: undefined, error: undefined } },
cache: false }
0
0
0
1
1
1
1
2
2
2
2
3
3
3
3
4
...
...
13
13
13
13
14
14
14
14
15
{ settings:
{ 'x-powered-by': true,
etag: 'weak',
'etag fn': [Function: wetag],
env: 'development',
'query parser': 'extended',
'query parser fn': [Function: parseExtendedQueryString],
'subdomain offset': 2,
'trust proxy': false,
'trust proxy fn': [Function: trustNone],
view: [Function: View],
views: '/home/elsa/Projects/rental-borg/project/views',
'jsonp callback name': 'callback',
'view engine': 'handlebars',
port: 8765 },
flash: { info: undefined, error: undefined },
_locals: { flash: { info: undefined, error: undefined } },
cache: false }
15
15
16
16
16
16
...
23
23
23
23
It is apparent that when this
and ../this
are equal, ../this
actually returns something like ../../this
which is the view object itself, I suppose. This happens twice in my specific case, on 00:00 and on 15:15.
Is this a bug?
EDIT: I solved the problem with the new Block Parameters feature of Handlebars 3, but the initial question remains. This is what I did to solve the problem:
.handlebars
file:
{{#for 0 23 1 as |hour|}}
{{#for 0 45 15 as |minute|}}
<span>{{hour}}:{{minute}}</span>
{{/for}}
{{/for}}
and the helper:
for: function(from, to, incr, block) {
var args = [], options = arguments[arguments.length - 1];
for (var i = 0; i < arguments.length - 1; i++) {
args.push(arguments[i]);
}
var accum = '';
for(var i = from; i <= to; i += incr) {
accum += options.fn(i, {data: options.data, blockParams: [i]});
}
return accum;
},
See Question&Answers more detail:os