Loop on an Array or Object.
This is a handlebars.js extension, mustache do not support this.
array(
"foo" => array(
"Hello",
"World"
)
)
!
Hello!World!
require('./vendor/autoload.php');
use LightnCandy\LightnCandy;
$template = "{{#each foo}}{{.}}!{{/each}}";
$php = LightnCandy::compile($template);
$render = LightnCandy::prepare($php);
$data = array(
"foo" => array(
"Hello",
"World"
)
);
echo $render($data);
{
"foo": [
"Hello",
"World"
]
}
!
Hello!World!
var Handlebars = require('handlebars');
var template = '{{#each foo}}{{.}}!{{/each}}';
var render = Handlebars.compile(template);
var data = {
"foo": [
"Hello",
"World"
]
};
console.log(render(data));
{{@index}}
for current index in the loop.Used option: FLAG_SPVARS
array(
"foo" => array(
"Hello",
"World"
)
)
: !
0: Hello!
1: World!
require('./vendor/autoload.php');
use LightnCandy\LightnCandy;
$template = "{{#each foo}}\n{{@index}}: {{.}}!\n{{/each}}";
$php = LightnCandy::compile($template, array(
"flags" => LightnCandy::FLAG_SPVARS
));
$render = LightnCandy::prepare($php);
$data = array(
"foo" => array(
"Hello",
"World"
)
);
echo $render($data);
{
"foo": [
"Hello",
"World"
]
}
: !
0: Hello!
1: World!
var Handlebars = require('handlebars');
var template = '{{#each foo}}\n{{@index}}: {{.}}!\n{{/each}}';
var render = Handlebars.compile(template);
var data = {
"foo": [
"Hello",
"World"
]
};
console.log(render(data));
array(
"foo" => array(
"key" => "Hello",
"key2" => "World"
)
)
!
Hello!World!
require('./vendor/autoload.php');
use LightnCandy\LightnCandy;
$template = "{{#each foo}}{{.}}!{{/each}}";
$php = LightnCandy::compile($template);
$render = LightnCandy::prepare($php);
$data = array(
"foo" => array(
"key" => "Hello",
"key2" => "World"
)
);
echo $render($data);
{
"foo": {
"key": "Hello",
"key2": "World"
}
}
!
Hello!World!
var Handlebars = require('handlebars');
var template = '{{#each foo}}{{.}}!{{/each}}';
var render = Handlebars.compile(template);
var data = {
"foo": {
"key": "Hello",
"key2": "World"
}
};
console.log(render(data));
{{@key}}
for current key in the loop.Used option: FLAG_SPVARS
array(
"foo" => array(
"first" => "Hello",
"second" => "World"
)
)
: !
first:Hello!
second:World!
require('./vendor/autoload.php');
use LightnCandy\LightnCandy;
$template = "{{#each foo}}\n{{@key}}:{{.}}!\n{{/each}}";
$php = LightnCandy::compile($template, array(
"flags" => LightnCandy::FLAG_SPVARS
));
$render = LightnCandy::prepare($php);
$data = array(
"foo" => array(
"first" => "Hello",
"second" => "World"
)
);
echo $render($data);
{
"foo": {
"first": "Hello",
"second": "World"
}
}
: !
first:Hello!
second:World!
var Handlebars = require('handlebars');
var template = '{{#each foo}}\n{{@key}}:{{.}}!\n{{/each}}';
var render = Handlebars.compile(template);
var data = {
"foo": {
"first": "Hello",
"second": "World"
}
};
console.log(render(data));
..
to access parent context inside the #each
Used option: FLAG_SPVARS FLAG_PARENT
array(
"foo" => array(
"first" => "Hello",
"second" => "World"
),
"title" => "Values"
)
: !
Values:Hello!
Values:World!
require('./vendor/autoload.php');
use LightnCandy\LightnCandy;
$template = "{{#each foo}}\n{{../title}}:{{.}}!\n{{/each}}";
$php = LightnCandy::compile($template, array(
"flags" => LightnCandy::FLAG_SPVARS | LightnCandy::FLAG_PARENT
));
$render = LightnCandy::prepare($php);
$data = array(
"foo" => array(
"first" => "Hello",
"second" => "World"
),
"title" => "Values"
);
echo $render($data);
{
"foo": {
"first": "Hello",
"second": "World"
},
"title": "Values"
}
: !
Values:Hello!
Values:World!
var Handlebars = require('handlebars');
var template = '{{#each foo}}\n{{../title}}:{{.}}!\n{{/each}}';
var render = Handlebars.compile(template);
var data = {
"foo": {
"first": "Hello",
"second": "World"
},
"title": "Values"
};
console.log(render(data));
#each
do not work on stringUsed option: FLAG_SPVARS FLAG_PARENT
array(
"foo" => "Good"
)
!
require('./vendor/autoload.php');
use LightnCandy\LightnCandy;
$template = "{{#each foo}}\n{{.}}!\n{{/each}}";
$php = LightnCandy::compile($template, array(
"flags" => LightnCandy::FLAG_SPVARS | LightnCandy::FLAG_PARENT
));
$render = LightnCandy::prepare($php);
$data = array(
"foo" => "Good"
);
echo $render($data);
{
"foo": "Good"
}
!
var Handlebars = require('handlebars');
var template = '{{#each foo}}\n{{.}}!\n{{/each}}';
var render = Handlebars.compile(template);
var data = {
"foo": "Good"
};
console.log(render(data));