Inverted Block

An inverted block is started with {{^foo}} and end with {{/foo}}.

Inverted Block for True

An inverted block will not render inner content for an array.

lightncandy
Data:
array(
  "foo" => array(
    1,
    3,
    5
  )
)
Template:
{{^foo}}Ya!{{/foo}}
Result:
Source Code
require('./vendor/autoload.php');
use LightnCandy\LightnCandy;
$template = "{{^foo}}Ya!{{/foo}}";

$php = LightnCandy::compile($template);
$render = LightnCandy::prepare($php);
$data = array(
  "foo" => array(
    1,
    3,
    5
  )
);
echo $render($data);
handlebars.js
Data:
{
  "foo": [
    1,
    3,
    5
  ]
}
Template:
{{^foo}}Ya!{{/foo}}
Result:
Source Code
var Handlebars = require('handlebars');
var template = '{{^foo}}Ya!{{/foo}}';

var render = Handlebars.compile(template);
var data = {
  "foo": [
    1,
    3,
    5
  ]
};
console.log(render(data));
mustache
Data:
{
  "foo": [
    1,
    3,
    5
  ]
}
Template:
{{^foo}}Ya!{{/foo}}
Result:
Source Code
var Mustache = require('mustache');
var template = '{{^foo}}Ya!{{/foo}}';

var data = {
  "foo": [
    1,
    3,
    5
  ]
};
console.log(Mustache.render(template, data));

An inverted block will not render inner content for an object.

lightncandy
Data:
array(
  "foo" => array(
    "bar" => NULL,
    "moo" => NULL
  )
)
Template:
{{^foo}}Ya!{{/foo}}
Result:
Source Code
require('./vendor/autoload.php');
use LightnCandy\LightnCandy;
$template = "{{^foo}}Ya!{{/foo}}";

$php = LightnCandy::compile($template);
$render = LightnCandy::prepare($php);
$data = array(
  "foo" => array(
    "bar" => NULL,
    "moo" => NULL
  )
);
echo $render($data);
handlebars.js
Data:
{
  "foo": {
    "bar": null,
    "moo": null
  }
}
Template:
{{^foo}}Ya!{{/foo}}
Result:
Source Code
var Handlebars = require('handlebars');
var template = '{{^foo}}Ya!{{/foo}}';

var render = Handlebars.compile(template);
var data = {
  "foo": {
    "bar": null,
    "moo": null
  }
};
console.log(render(data));
mustache
Data:
{
  "foo": {
    "bar": null,
    "moo": null
  }
}
Template:
{{^foo}}Ya!{{/foo}}
Result:
Source Code
var Mustache = require('mustache');
var template = '{{^foo}}Ya!{{/foo}}';

var data = {
  "foo": {
    "bar": null,
    "moo": null
  }
};
console.log(Mustache.render(template, data));

An inverted block will not render inner content for a string.

lightncandy
Data:
array(
  "foo" => "test"
)
Template:
{{^foo}}Ya!{{/foo}}
Result:
Source Code
require('./vendor/autoload.php');
use LightnCandy\LightnCandy;
$template = "{{^foo}}Ya!{{/foo}}";

$php = LightnCandy::compile($template);
$render = LightnCandy::prepare($php);
$data = array(
  "foo" => "test"
);
echo $render($data);
handlebars.js
Data:
{
  "foo": "test"
}
Template:
{{^foo}}Ya!{{/foo}}
Result:
Source Code
var Handlebars = require('handlebars');
var template = '{{^foo}}Ya!{{/foo}}';

var render = Handlebars.compile(template);
var data = {
  "foo": "test"
};
console.log(render(data));
mustache
Data:
{
  "foo": "test"
}
Template:
{{^foo}}Ya!{{/foo}}
Result:
Source Code
var Mustache = require('mustache');
var template = '{{^foo}}Ya!{{/foo}}';

var data = {
  "foo": "test"
};
console.log(Mustache.render(template, data));

An inverted block will not render inner content for a number.

lightncandy
Data:
array(
  "foo" => 1
)
Template:
{{^foo}}Ya!{{/foo}}
Result:
Source Code
require('./vendor/autoload.php');
use LightnCandy\LightnCandy;
$template = "{{^foo}}Ya!{{/foo}}";

$php = LightnCandy::compile($template);
$render = LightnCandy::prepare($php);
$data = array(
  "foo" => 1
);
echo $render($data);
handlebars.js
Data:
{
  "foo": 1
}
Template:
{{^foo}}Ya!{{/foo}}
Result:
Source Code
var Handlebars = require('handlebars');
var template = '{{^foo}}Ya!{{/foo}}';

var render = Handlebars.compile(template);
var data = {
  "foo": 1
};
console.log(render(data));
mustache
Data:
{
  "foo": 1
}
Template:
{{^foo}}Ya!{{/foo}}
Result:
Source Code
var Mustache = require('mustache');
var template = '{{^foo}}Ya!{{/foo}}';

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

Inverted Block for False

When the value is false, the inner content will be rendered.

lightncandy
Data:
array(
  "foo" => false
)
Template:
{{^foo}}Ya!{{/foo}}
Result:
Ya!
Source Code
require('./vendor/autoload.php');
use LightnCandy\LightnCandy;
$template = "{{^foo}}Ya!{{/foo}}";

$php = LightnCandy::compile($template);
$render = LightnCandy::prepare($php);
$data = array(
  "foo" => false
);
echo $render($data);
handlebars.js
Data:
{
  "foo": false
}
Template:
{{^foo}}Ya!{{/foo}}
Result:
Ya!
Source Code
var Handlebars = require('handlebars');
var template = '{{^foo}}Ya!{{/foo}}';

var render = Handlebars.compile(template);
var data = {
  "foo": false
};
console.log(render(data));
mustache
Data:
{
  "foo": false
}
Template:
{{^foo}}Ya!{{/foo}}
Result:
Ya!
Source Code
var Mustache = require('mustache');
var template = '{{^foo}}Ya!{{/foo}}';

var data = {
  "foo": false
};
console.log(Mustache.render(template, data));

When the value is empty array, the inner content will be rendered.

lightncandy
Data:
array(
  "foo" => array(

  )
)
Template:
{{^foo}}Ya!{{/foo}}
Result:
Ya!
Source Code
require('./vendor/autoload.php');
use LightnCandy\LightnCandy;
$template = "{{^foo}}Ya!{{/foo}}";

$php = LightnCandy::compile($template);
$render = LightnCandy::prepare($php);
$data = array(
  "foo" => array(

  )
);
echo $render($data);
handlebars.js
Data:
{
  "foo": []
}
Template:
{{^foo}}Ya!{{/foo}}
Result:
Ya!
Source Code
var Handlebars = require('handlebars');
var template = '{{^foo}}Ya!{{/foo}}';

var render = Handlebars.compile(template);
var data = {
  "foo": []
};
console.log(render(data));
mustache
Data:
{
  "foo": []
}
Template:
{{^foo}}Ya!{{/foo}}
Result:
Ya!
Source Code
var Mustache = require('mustache');
var template = '{{^foo}}Ya!{{/foo}}';

var data = {
  "foo": []
};
console.log(Mustache.render(template, data));

When the value is null or undefined, the inner content will be rendered.

lightncandy
Data:
array(
  "foo" => NULL
)
Template:
{{^foo}}Ya!{{/foo}}
Result:
Ya!
Source Code
require('./vendor/autoload.php');
use LightnCandy\LightnCandy;
$template = "{{^foo}}Ya!{{/foo}}";

$php = LightnCandy::compile($template);
$render = LightnCandy::prepare($php);
$data = array(
  "foo" => NULL
);
echo $render($data);
handlebars.js
Data:
{
  "foo": null
}
Template:
{{^foo}}Ya!{{/foo}}
Result:
Ya!
Source Code
var Handlebars = require('handlebars');
var template = '{{^foo}}Ya!{{/foo}}';

var render = Handlebars.compile(template);
var data = {
  "foo": null
};
console.log(render(data));
mustache
Data:
{
  "foo": null
}
Template:
{{^foo}}Ya!{{/foo}}
Result:
Ya!
Source Code
var Mustache = require('mustache');
var template = '{{^foo}}Ya!{{/foo}}';

var data = {
  "foo": null
};
console.log(Mustache.render(template, data));

Mustache.js will think 0 is false.

lightncandy
Data:
array(
  "foo" => 0
)
Template:
{{^foo}}Ya!{{/foo}}
Result:
Source Code
require('./vendor/autoload.php');
use LightnCandy\LightnCandy;
$template = "{{^foo}}Ya!{{/foo}}";

$php = LightnCandy::compile($template);
$render = LightnCandy::prepare($php);
$data = array(
  "foo" => 0
);
echo $render($data);
handlebars.js
Data:
{
  "foo": 0
}
Template:
{{^foo}}Ya!{{/foo}}
Result:
Source Code
var Handlebars = require('handlebars');
var template = '{{^foo}}Ya!{{/foo}}';

var render = Handlebars.compile(template);
var data = {
  "foo": 0
};
console.log(render(data));
mustache
Data:
{
  "foo": 0
}
Template:
{{^foo}}Ya!{{/foo}}
Result:
Ya!
Source Code
var Mustache = require('mustache');
var template = '{{^foo}}Ya!{{/foo}}';

var data = {
  "foo": 0
};
console.log(Mustache.render(template, data));

Mustache.js will think empty string is false.

lightncandy
Data:
array(
  "foo" => ""
)
Template:
{{^foo}}Ya!{{/foo}}
Result:
Source Code
require('./vendor/autoload.php');
use LightnCandy\LightnCandy;
$template = "{{^foo}}Ya!{{/foo}}";

$php = LightnCandy::compile($template);
$render = LightnCandy::prepare($php);
$data = array(
  "foo" => ""
);
echo $render($data);
handlebars.js
Data:
{
  "foo": ""
}
Template:
{{^foo}}Ya!{{/foo}}
Result:
Source Code
var Handlebars = require('handlebars');
var template = '{{^foo}}Ya!{{/foo}}';

var render = Handlebars.compile(template);
var data = {
  "foo": ""
};
console.log(render(data));
mustache
Data:
{
  "foo": ""
}
Template:
{{^foo}}Ya!{{/foo}}
Result:
Ya!
Source Code
var Mustache = require('mustache');
var template = '{{^foo}}Ya!{{/foo}}';

var data = {
  "foo": ""
};
console.log(Mustache.render(template, data));

Context Switching

When rendering inner block, the context will not be switched.

lightncandy

Used option: FLAG_JSTRUE

Data:
array(
  "foo" => false,
  "bar" => "Good!"
)
Template:
{{^foo}}{{foo}},{{bar}}{{/foo}}
Result:
false,Good!
Source Code
require('./vendor/autoload.php');
use LightnCandy\LightnCandy;
$template = "{{^foo}}{{foo}},{{bar}}{{/foo}}\n";

$php = LightnCandy::compile($template, array(
  "flags" => LightnCandy::FLAG_JSTRUE
));
$render = LightnCandy::prepare($php);
$data = array(
  "foo" => false,
  "bar" => "Good!"
);
echo $render($data);
handlebars.js
Data:
{
  "foo": false,
  "bar": "Good!"
}
Template:
{{^foo}}{{foo}},{{bar}}{{/foo}}
Result:
false,Good!
Source Code
var Handlebars = require('handlebars');
var template = '{{^foo}}{{foo}},{{bar}}{{/foo}}\n';

var render = Handlebars.compile(template);
var data = {
  "foo": false,
  "bar": "Good!"
};
console.log(render(data));
mustache
Data:
{
  "foo": false,
  "bar": "Good!"
}
Template:
{{^foo}}{{foo}},{{bar}}{{/foo}}
Result:
false,Good!
Source Code
var Mustache = require('mustache');
var template = '{{^foo}}{{foo}},{{bar}}{{/foo}}\n';

var data = {
  "foo": false,
  "bar": "Good!"
};
console.log(Mustache.render(template, data));

lightncandy
Data:
array(
  "foo" => false,
  "bar" => false,
  "moo" => "OH!"
)
Template:
{{^foo}}{{^bar}}{{moo}}{{/bar}}{{/foo}}
Result:
OH!
Source Code
require('./vendor/autoload.php');
use LightnCandy\LightnCandy;
$template = "{{^foo}}{{^bar}}{{moo}}{{/bar}}{{/foo}}";

$php = LightnCandy::compile($template);
$render = LightnCandy::prepare($php);
$data = array(
  "foo" => false,
  "bar" => false,
  "moo" => "OH!"
);
echo $render($data);
handlebars.js
Data:
{
  "foo": false,
  "bar": false,
  "moo": "OH!"
}
Template:
{{^foo}}{{^bar}}{{moo}}{{/bar}}{{/foo}}
Result:
OH!
Source Code
var Handlebars = require('handlebars');
var template = '{{^foo}}{{^bar}}{{moo}}{{/bar}}{{/foo}}';

var render = Handlebars.compile(template);
var data = {
  "foo": false,
  "bar": false,
  "moo": "OH!"
};
console.log(render(data));
mustache
Data:
{
  "foo": false,
  "bar": false,
  "moo": "OH!"
}
Template:
{{^foo}}{{^bar}}{{moo}}{{/bar}}{{/foo}}
Result:
OH!
Source Code
var Mustache = require('mustache');
var template = '{{^foo}}{{^bar}}{{moo}}{{/bar}}{{/foo}}';

var data = {
  "foo": false,
  "bar": false,
  "moo": "OH!"
};
console.log(Mustache.render(template, data));

See Also...