Iteracja podstawowej pętli " for " za pomocą kierownicy.js
Jestem nowy w kierownicach.js i po prostu zaczął go używać. Większość przykładów opiera się na iteracji nad obiektem. Chciałem wiedzieć jak używać kierownicy w basic for loop.
Przykład.
for(i=0 ; i<100 ; i++) {
create li's with i as the value
}
Jak można to osiągnąć? 70
Author: Jan Johansen, 2012-08-12
4 answers
W kierownicy nie ma nic do tego, ale możesz łatwo dodać własnych pomocników.
Jeśli po prostu chcesz coś zrobić n
razy to:
Handlebars.registerHelper('times', function(n, block) {
var accum = '';
for(var i = 0; i < n; ++i)
accum += block.fn(i);
return accum;
});
I
{{#times 10}}
<span>{{this}}</span>
{{/times}}
Jeśli chcesz mieć całą for(;;)
pętlę, to coś takiego:
Handlebars.registerHelper('for', function(from, to, incr, block) {
var accum = '';
for(var i = from; i < to; i += incr)
accum += block.fn(i);
return accum;
});
I
{{#for 0 10 2}}
<span>{{this}}</span>
{{/for}}
153
Author: mu is too short,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-08-12 19:06:12
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-08-12 19:06:12
NAJLEPSZA odpowiedź tutaj jest dobra, jeśli chcesz użyć last / first / index, chociaż możesz użyć następującego
Handlebars.registerHelper('times', function(n, block) {
var accum = '';
for(var i = 0; i < n; ++i) {
block.data.index = i;
block.data.first = i === 0;
block.data.last = i === (n - 1);
accum += block.fn(this);
}
return accum;
});
I
{{#times 10}}
<span> {{@first}} {{@index}} {{@last}}</span>
{{/times}}
12
Author: Mike Mellor,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-03-21 10:09:17
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-03-21 10:09:17
Jeśli lubisz CoffeeScript
Handlebars.registerHelper "times", (n, block) ->
(block.fn(i) for i in [0...n]).join("")
I
{{#times 10}}
<span>{{this}}</span>
{{/times}}
8
Author: Manuel,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-01-25 08:14:13
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-01-25 08:14:13
Ten fragment zajmie się blokiem else w przypadku, gdy n
ma wartość dynamiczną, i dostarczy @index
opcjonalną zmienną kontekstową, zachowa również zewnętrzny kontekst wykonania.
/*
* Repeat given markup with given times
* provides @index for the repeated iteraction
*/
Handlebars.registerHelper("repeat", function (times, opts) {
var out = "";
var i;
var data = {};
if ( times ) {
for ( i = 0; i < times; i += 1 ) {
data.index = i;
out += opts.fn(this, {
data: data
});
}
} else {
out = opts.inverse(this);
}
return out;
});
5
Author: dmi3y,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-09-03 21:45:30
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-09-03 21:45:30