Handlebars: Built-in block helper - #with

Switch current context

This is a handlebars.js extension, mustache do not support this.

Samples


lightncandy
Data:
array(
  "foo" => array(
    "bar" => "Yes!"
  )
)
Template:
{{#with foo}}{{bar}}{{/with}}
Result:
Command failed: php .exec_tmp_file 2>/dev/null
Source Code
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);
handlebars.js
Data:
{
  "foo": {
    "bar": "Yes!"
  }
}
Template:
{{#with foo}}{{bar}}{{/with}}
Result:
Yes!
Source Code
var Handlebars = require('handlebars');
var template = '{{#with foo}}{{bar}}{{/with}}';

var render = Handlebars.compile(template);
var data = {
  "foo": {
    "bar": "Yes!"
  }
};
console.log(render(data));

lightncandy
Data:
array(
  "foo" => array(
    "bar" => array(
      "hey" => "Hello",
      "ha" => "world"
    )
  )
)
Template:
{{#with foo.bar}}{{hey}}, {{ha}}{{/with}}
Result:
Command failed: php .exec_tmp_file 2>/dev/null
Source Code
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);
handlebars.js
Data:
{
  "foo": {
    "bar": {
      "hey": "Hello",
      "ha": "world"
    }
  }
}
Template:
{{#with foo.bar}}{{hey}}, {{ha}}{{/with}}
Result:
Hello, world
Source Code
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));

Use {{..}} to access original context.

lightncandy

Used option: FLAG_PARENT

Data:
array(
  "foo" => array(
    "bar" => array(
      "moo" => "Hello"
    )
  ),
  "bar" => "world"
)
Template:
{{#with foo.bar}}{{moo}}, {{../bar}}{{/with}}
Result:
Command failed: php .exec_tmp_file 2>/dev/null
Source Code
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);
handlebars.js
Data:
{
  "foo": {
    "bar": {
      "moo": "Hello"
    }
  },
  "bar": "world"
}
Template:
{{#with foo.bar}}{{moo}}, {{../bar}}{{/with}}
Result:
Hello, world
Source Code
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));

With 0 you get 0

lightncandy

Used option: FLAG_PARENT

Data:
NULL
Template:
{{#with 0}}Current context:{{.}}{{/with}}
Result:
Command failed: php .exec_tmp_file 2>/dev/null
Source Code
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);
handlebars.js
Data:
Template:
{{#with 0}}Current context:{{.}}{{/with}}
Result:
Current context:0
Source Code
var Handlebars = require('handlebars');
var template = '{{#with 0}}Current context:{{.}}{{/with}}';

var render = Handlebars.compile(template);
var data = undefined;
console.log(render(data));

With 1 you get 1

lightncandy

Used option: FLAG_PARENT FLAG_JSOBJECT

Data:
NULL
Template:
{{#with 1}}Current context:{{.}}{{/with}}
Result:
Command failed: php .exec_tmp_file 2>/dev/null
Source Code
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);
handlebars.js
Data:
Template:
{{#with 1}}Current context:{{.}}{{/with}}
Result:
Current context:1
Source Code
var Handlebars = require('handlebars');
var template = '{{#with 1}}Current context:{{.}}{{/with}}';

var render = Handlebars.compile(template);
var data = undefined;
console.log(render(data));

With empty array you will skip the included section

lightncandy

Used option: FLAG_PARENT

Data:
array(

)
Template:
{{#with .}}Current context:{{.}}{{/with}}
Result:
Command failed: php .exec_tmp_file 2>/dev/null
Source Code
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);
handlebars.js
Data:
[]
Template:
{{#with .}}Current context:{{.}}{{/with}}
Result:
Source Code
var Handlebars = require('handlebars');
var template = '{{#with .}}Current context:{{.}}{{/with}}';

var render = Handlebars.compile(template);
var data = [];
console.log(render(data));

With false you will skip the included section

lightncandy

Used option: FLAG_PARENT

Data:
array(

)
Template:
{{#with .}}Current context:{{.}}{{/with}}
Result:
Command failed: php .exec_tmp_file 2>/dev/null
Source Code
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);
handlebars.js
Data:
[]
Template:
{{#with .}}Current context:{{.}}{{/with}}
Result:
Source Code
var Handlebars = require('handlebars');
var template = '{{#with .}}Current context:{{.}}{{/with}}';

var render = Handlebars.compile(template);
var data = [];
console.log(render(data));

See Also...