Handlebars: Whitespace Control

By default the line change for standalone tags will be removed. If you wanna remove more line changes or spaces, you can use ~ in left or right side of a tag.

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

Samples

Line change of standalone tags will not be removed

lightncandy
Data:
array(
  "foo" => 1
)
Template:
Line 1
{{foo}}
Line 3
  {{foo}}
Line 5
     {{foo}}
Line 7
Result:
Command failed: php .exec_tmp_file 2>/dev/null
Source Code
require('./vendor/autoload.php');
use LightnCandy\LightnCandy;
$template = "Line 1\n{{foo}}\nLine 3\n  {{foo}}\nLine 5\n     {{foo}}\nLine 7\n";

$php = LightnCandy::compile($template);
$render = LightnCandy::prepare($php);
$data = array(
  "foo" => 1
);
echo $render($data);
handlebars.js
Data:
{
  "foo": 1
}
Template:
Line 1
{{foo}}
Line 3
  {{foo}}
Line 5
     {{foo}}
Line 7
Result:
Line 1
1
Line 3
  1
Line 5
     1
Line 7
Source Code
var Handlebars = require('handlebars');
var template = 'Line 1\n{{foo}}\nLine 3\n  {{foo}}\nLine 5\n     {{foo}}\nLine 7\n';

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

Line change of standalone block tags and {{else}} will be removed

lightncandy

Used option: FLAG_THIS FLAG_ELSE

Data:
array(
  "foo" => 1
)
Template:
Line 1
{{#with foo}}
Line 3
{{else}}
Line 5
{{/with}}
Line 7
Result:
Command failed: php .exec_tmp_file 2>/dev/null
Source Code
require('./vendor/autoload.php');
use LightnCandy\LightnCandy;
$template = "Line 1\n{{#with foo}}\nLine 3\n{{else}}\nLine 5\n{{/with}}\nLine 7\n";

$php = LightnCandy::compile($template, array(
  "flags" => LightnCandy::FLAG_THIS | LightnCandy::FLAG_ELSE
));
$render = LightnCandy::prepare($php);
$data = array(
  "foo" => 1
);
echo $render($data);
handlebars.js
Data:
{
  "foo": 1
}
Template:
Line 1
{{#with foo}}
Line 3
{{else}}
Line 5
{{/with}}
Line 7
Result:
Line 1
Line 3
Line 7
Source Code
var Handlebars = require('handlebars');
var template = 'Line 1\n{{#with foo}}\nLine 3\n{{else}}\nLine 5\n{{/with}}\nLine 7\n';

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

Line changes and spaces will be removed when you use whitespace control charactor

lightncandy

Used option: FLAG_THIS FLAG_ELSE

Data:
array(
  "foo" => 1
)
Template:
Line 1
{{foo~}}
Line 3
  {{~foo~}}
Line 5
    {{~foo}}
Line 7
Result:
Command failed: php .exec_tmp_file 2>/dev/null
Source Code
require('./vendor/autoload.php');
use LightnCandy\LightnCandy;
$template = "Line 1\n{{foo~}}\nLine 3\n  {{~foo~}}\nLine 5\n    {{~foo}}\nLine 7\n";

$php = LightnCandy::compile($template, array(
  "flags" => LightnCandy::FLAG_THIS | LightnCandy::FLAG_ELSE
));
$render = LightnCandy::prepare($php);
$data = array(
  "foo" => 1
);
echo $render($data);
handlebars.js
Data:
{
  "foo": 1
}
Template:
Line 1
{{foo~}}
Line 3
  {{~foo~}}
Line 5
    {{~foo}}
Line 7
Result:
Line 1
1Line 31Line 51
Line 7
Source Code
var Handlebars = require('handlebars');
var template = 'Line 1\n{{foo~}}\nLine 3\n  {{~foo~}}\nLine 5\n    {{~foo}}\nLine 7\n';

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