Switch current context
This is a handlebars.js extension, mustache do not support this.
array(
"foo" => array(
"bar" => "Yes!"
)
)
Yes!
require('./vendor/autoload.php');
use LightnCandy\LightnCandy;
$template = "{{#with foo}}{{bar}}{{/with}}";
$php = LightnCandy::compile($template);
$render = LightnCandy::prepare($php);
$data = array(
"foo" => array(
"bar" => "Yes!"
)
);
echo $render($data);
{
"foo": {
"bar": "Yes!"
}
}
Yes!
var Handlebars = require('handlebars');
var template = '{{#with foo}}{{bar}}{{/with}}';
var render = Handlebars.compile(template);
var data = {
"foo": {
"bar": "Yes!"
}
};
console.log(render(data));
array(
"foo" => array(
"bar" => array(
"hey" => "Hello",
"ha" => "world"
)
)
)
,
Hello, world
require('./vendor/autoload.php');
use LightnCandy\LightnCandy;
$template = "{{#with foo.bar}}{{hey}}, {{ha}}{{/with}}";
$php = LightnCandy::compile($template);
$render = LightnCandy::prepare($php);
$data = array(
"foo" => array(
"bar" => array(
"hey" => "Hello",
"ha" => "world"
)
)
);
echo $render($data);
{
"foo": {
"bar": {
"hey": "Hello",
"ha": "world"
}
}
}
,
Hello, world
var Handlebars = require('handlebars');
var template = '{{#with foo.bar}}{{hey}}, {{ha}}{{/with}}';
var render = Handlebars.compile(template);
var data = {
"foo": {
"bar": {
"hey": "Hello",
"ha": "world"
}
}
};
console.log(render(data));
{{..}}
to access original context.Used option: FLAG_PARENT
array(
"foo" => array(
"bar" => array(
"moo" => "Hello"
)
),
"bar" => "world"
)
,
Hello, world
require('./vendor/autoload.php');
use LightnCandy\LightnCandy;
$template = "{{#with foo.bar}}{{moo}}, {{../bar}}{{/with}}";
$php = LightnCandy::compile($template, array(
"flags" => LightnCandy::FLAG_PARENT
));
$render = LightnCandy::prepare($php);
$data = array(
"foo" => array(
"bar" => array(
"moo" => "Hello"
)
),
"bar" => "world"
);
echo $render($data);
{
"foo": {
"bar": {
"moo": "Hello"
}
},
"bar": "world"
}
,
Hello, world
var Handlebars = require('handlebars');
var template = '{{#with foo.bar}}{{moo}}, {{../bar}}{{/with}}';
var render = Handlebars.compile(template);
var data = {
"foo": {
"bar": {
"moo": "Hello"
}
},
"bar": "world"
};
console.log(render(data));
Used option: FLAG_PARENT
NULL
Current context:
Current context:0
require('./vendor/autoload.php');
use LightnCandy\LightnCandy;
$template = "{{#with 0}}Current context:{{.}}{{/with}}";
$php = LightnCandy::compile($template, array(
"flags" => LightnCandy::FLAG_PARENT
));
$render = LightnCandy::prepare($php);
$data = NULL;
echo $render($data);
Current context:
Current context:0
var Handlebars = require('handlebars');
var template = '{{#with 0}}Current context:{{.}}{{/with}}';
var render = Handlebars.compile(template);
var data = undefined;
console.log(render(data));
Used option: FLAG_PARENT FLAG_JSOBJECT
NULL
Current context:
Current context:1
require('./vendor/autoload.php');
use LightnCandy\LightnCandy;
$template = "{{#with 1}}Current context:{{.}}{{/with}}";
$php = LightnCandy::compile($template, array(
"flags" => LightnCandy::FLAG_PARENT | LightnCandy::FLAG_JSOBJECT
));
$render = LightnCandy::prepare($php);
$data = NULL;
echo $render($data);
Current context:
Current context:1
var Handlebars = require('handlebars');
var template = '{{#with 1}}Current context:{{.}}{{/with}}';
var render = Handlebars.compile(template);
var data = undefined;
console.log(render(data));
Used option: FLAG_PARENT
array(
)
Current context:
require('./vendor/autoload.php');
use LightnCandy\LightnCandy;
$template = "{{#with .}}Current context:{{.}}{{/with}}";
$php = LightnCandy::compile($template, array(
"flags" => LightnCandy::FLAG_PARENT
));
$render = LightnCandy::prepare($php);
$data = array(
);
echo $render($data);
[]
Current context:
var Handlebars = require('handlebars');
var template = '{{#with .}}Current context:{{.}}{{/with}}';
var render = Handlebars.compile(template);
var data = [];
console.log(render(data));
false
you will skip the included sectionUsed option: FLAG_PARENT
array(
)
Current context:
require('./vendor/autoload.php');
use LightnCandy\LightnCandy;
$template = "{{#with .}}Current context:{{.}}{{/with}}";
$php = LightnCandy::compile($template, array(
"flags" => LightnCandy::FLAG_PARENT
));
$render = LightnCandy::prepare($php);
$data = array(
);
echo $render($data);
[]
Current context:
var Handlebars = require('handlebars');
var template = '{{#with .}}Current context:{{.}}{{/with}}';
var render = Handlebars.compile(template);
var data = [];
console.log(render(data));