for(of)
Loop through a list of values, keys in an object, or integers.
{{# for( VARIABLE_NAME of EXPRESSION ) }}FN{{else}}INVERSE{{/ for}}
for
is used to to loop through:
- an array-like list of values
{{# for(task of this.tasks) }} {{task.name}} {{/for}}
- keys in an object
{{# for( this.task ) }} {{scope.key}} {{scope.value}} {{/for}}
- integers starting from 0
{{# for( this.tasks.length ) }} {{scope.index}} {{/for}}
If an array-like list of values is passed, for
loops through
each value returned by EXPRESSION
and points a
a local let-like variable named VARIABLE_NAME
at each value.
import {stache} from "can";
var view = stache(`
<ul>
{{# for(item of this.values) }}
<li>{{ item.name }}</li>
{{/ for }}
</ul>
`);
var data = {
values: [
{name: "first"},
{name: "second"}
]
};
var frag = view(data);
console.log(frag.firstElementChild.innerHTML)
//-> <li>first</li><li>second</li>
document.body.appendChild(frag);
If an object of key-values are passed, the values of the object will be looped through.
You can access the key with scope.key
:
import {stache} from "can";
var view = stache(`
<ul>
{{# for(name of this.values) }}
<li>{{scope.key}}: {{ name }}</li>
{{/ for }}
</ul>
`);
var data = {
values: {
first: "Hope",
middle: "van",
last: "Dyne"
}
};
var frag = view(data);
console.log(frag.firstElementChild.innerHTML)
//-> <li>first: Hope</li><li>middle: van</li><li>last: Dyne</li>
document.body.appendChild(frag);
If a positive integer value is passed, it will loop that number of times.
You can access the current index with scope.index
:
import {stache} from "can";
var view = stache(`
<ul>
{{# for(this.integerValue) }}
<li>{{scope.index}}</li>
{{/ for }}
</ul>
`);
var data = {
integerValue: 3
};
var frag = view(data);
console.log(frag.firstElementChild.innerHTML)
//-> <li>0</li><li>1</li><li>2</li>
document.body.appendChild(frag);
If the EXPRESSION
is falsy or an empty object or list, the INVERSE
section will be rendered:
import {stache} from "can";
var view = stache(`
<ul>
{{# for(name of this.values) }}
<li>{{ item.name }}</li>
{{ else }}
<li>No items</li>
{{/ for }}
</ul>
`);
var data = {
values: []
};
var frag = view(data);
console.log(frag.firstElementChild.innerHTML)
//-> <li>No items</li>
document.body.appendChild(frag);
Parameters
- VARIABLE_NAME
{VariableExpression}
:A local variable that will only be accessible to KeyLookups within the block. You can leave out the
VARIABLE_NAME
andof
to loop through items in the object like:{{# for( this.values ) }} <li>{{ scope.index }}</li> {{/ for }}
- EXPRESSION
{KeyLookup Expression|Call Expression}
:An expression that returns
- A list like data structure.
- An object-like (key-value) data structure.
- A positive integer Number
If the value of the
EXPRESSION
is an observable list (can-define/list/list) or map (can-define/map/map) and an item within it changes, only the minimum amount of DOM elements in the resulting HTML will change. If the list/map/or number value itself changes, a difference will be performed, resulting again in a minimal set of updates. - FN
{sectionRenderer(context, helpers)}
:A subsection that is rendered for each value in
EXPRESSION
. This subsection can access the value inEXPRESSION
asVARIABLE_NAME
. - INVERSE
{sectionRenderer(context, helpers)}
:A subsection that is rendered if
EXPRESSION
evaluats to an empty list.
Use
for
is used to render HTML for items in a list. It improves
upon {{#each(expression)}} in that it does not
change the scope. Notice that within the {{# for}}
block,
this
still refers to the data passed to the view:
import {stache} from "can";
var view = stache(`
<ul>
{{# for(item of this.values) }}
<li>{{this.dataName}}-{{ item.name }}</li>
{{/ for }}
</ul>
`);
var data = {
values: [
{name: "one"},
{name: "two"}
],
dataName: "number"
};
var frag = view(data);
document.body.appendChild(frag);
document.body.innerHTML
//-> <ul><li>number-one</li><li>number-two</li></ul>