Handlebars: Built-in block helper - #unless

Provide simple unless/else rendering logic which reversed from #if

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

Samples

True!

lightncandy

Used option: FLAG_ELSE

Data:
array(
  "foo" => true
)
Template:
{{#unless foo}}YES{{else}}no{{/unless}}
Result:
no
Source Code
require('./vendor/autoload.php');
use LightnCandy\LightnCandy;
$template = "{{#unless foo}}YES{{else}}no{{/unless}}";

$php = LightnCandy::compile($template, array(
  "flags" => LightnCandy::FLAG_ELSE
));
$render = LightnCandy::prepare($php);
$data = array(
  "foo" => true
);
echo $render($data);
handlebars.js
Data:
{
  "foo": true
}
Template:
{{#unless foo}}YES{{else}}no{{/unless}}
Result:
no
Source Code
var Handlebars = require('handlebars');
var template = '{{#unless foo}}YES{{else}}no{{/unless}}';

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

None empty string means true

lightncandy

Used option: FLAG_ELSE

Data:
array(
  "foo" => "String"
)
Template:
{{#unless foo}}YES{{else}}no{{/unless}}
Result:
no
Source Code
require('./vendor/autoload.php');
use LightnCandy\LightnCandy;
$template = "{{#unless foo}}YES{{else}}no{{/unless}}";

$php = LightnCandy::compile($template, array(
  "flags" => LightnCandy::FLAG_ELSE
));
$render = LightnCandy::prepare($php);
$data = array(
  "foo" => "String"
);
echo $render($data);
handlebars.js
Data:
{
  "foo": "String"
}
Template:
{{#unless foo}}YES{{else}}no{{/unless}}
Result:
no
Source Code
var Handlebars = require('handlebars');
var template = '{{#unless foo}}YES{{else}}no{{/unless}}';

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

None empty array means true

lightncandy

Used option: FLAG_ELSE

Data:
array(
  "foo" => array(
    1
  )
)
Template:
{{#unless foo}}YES{{else}}no{{/unless}}
Result:
no
Source Code
require('./vendor/autoload.php');
use LightnCandy\LightnCandy;
$template = "{{#unless foo}}YES{{else}}no{{/unless}}";

$php = LightnCandy::compile($template, array(
  "flags" => LightnCandy::FLAG_ELSE
));
$render = LightnCandy::prepare($php);
$data = array(
  "foo" => array(
    1
  )
);
echo $render($data);
handlebars.js
Data:
{
  "foo": [
    1
  ]
}
Template:
{{#unless foo}}YES{{else}}no{{/unless}}
Result:
no
Source Code
var Handlebars = require('handlebars');
var template = '{{#unless foo}}YES{{else}}no{{/unless}}';

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

Numbers other than 0 means true

lightncandy

Used option: FLAG_ELSE

Data:
array(
  "foo" => 1
)
Template:
{{#unless foo}}YES{{else}}no{{/unless}}
Result:
no
Source Code
require('./vendor/autoload.php');
use LightnCandy\LightnCandy;
$template = "{{#unless foo}}YES{{else}}no{{/unless}}";

$php = LightnCandy::compile($template, array(
  "flags" => LightnCandy::FLAG_ELSE
));
$render = LightnCandy::prepare($php);
$data = array(
  "foo" => 1
);
echo $render($data);
handlebars.js
Data:
{
  "foo": 1
}
Template:
{{#unless foo}}YES{{else}}no{{/unless}}
Result:
no
Source Code
var Handlebars = require('handlebars');
var template = '{{#unless foo}}YES{{else}}no{{/unless}}';

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

Numbers other than 0 means true

lightncandy

Used option: FLAG_ELSE

Data:
array(
  "foo" => -1
)
Template:
{{#unless foo}}YES{{else}}no{{/unless}}
Result:
no
Source Code
require('./vendor/autoload.php');
use LightnCandy\LightnCandy;
$template = "{{#unless foo}}YES{{else}}no{{/unless}}";

$php = LightnCandy::compile($template, array(
  "flags" => LightnCandy::FLAG_ELSE
));
$render = LightnCandy::prepare($php);
$data = array(
  "foo" => -1
);
echo $render($data);
handlebars.js
Data:
{
  "foo": -1
}
Template:
{{#unless foo}}YES{{else}}no{{/unless}}
Result:
no
Source Code
var Handlebars = require('handlebars');
var template = '{{#unless foo}}YES{{else}}no{{/unless}}';

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

Object or associative array means true

lightncandy

Used option: FLAG_ELSE

Data:
array(
  "foo" => array(
    "bar" => NULL
  )
)
Template:
{{#unless foo}}YES{{else}}no{{/unless}}
Result:
no
Source Code
require('./vendor/autoload.php');
use LightnCandy\LightnCandy;
$template = "{{#unless foo}}YES{{else}}no{{/unless}}";

$php = LightnCandy::compile($template, array(
  "flags" => LightnCandy::FLAG_ELSE
));
$render = LightnCandy::prepare($php);
$data = array(
  "foo" => array(
    "bar" => NULL
  )
);
echo $render($data);
handlebars.js
Data:
{
  "foo": {
    "bar": null
  }
}
Template:
{{#unless foo}}YES{{else}}no{{/unless}}
Result:
no
Source Code
var Handlebars = require('handlebars');
var template = '{{#unless foo}}YES{{else}}no{{/unless}}';

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

False!

lightncandy

Used option: FLAG_ELSE

Data:
array(
  "foo" => false
)
Template:
{{#unless foo}}YES{{else}}no{{/unless}}
Result:
YES
Source Code
require('./vendor/autoload.php');
use LightnCandy\LightnCandy;
$template = "{{#unless foo}}YES{{else}}no{{/unless}}";

$php = LightnCandy::compile($template, array(
  "flags" => LightnCandy::FLAG_ELSE
));
$render = LightnCandy::prepare($php);
$data = array(
  "foo" => false
);
echo $render($data);
handlebars.js
Data:
{
  "foo": false
}
Template:
{{#unless foo}}YES{{else}}no{{/unless}}
Result:
YES
Source Code
var Handlebars = require('handlebars');
var template = '{{#unless foo}}YES{{else}}no{{/unless}}';

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

Empty string means false

lightncandy

Used option: FLAG_ELSE

Data:
array(
  "foo" => ""
)
Template:
{{#unless foo}}YES{{else}}no{{/unless}}
Result:
YES
Source Code
require('./vendor/autoload.php');
use LightnCandy\LightnCandy;
$template = "{{#unless foo}}YES{{else}}no{{/unless}}";

$php = LightnCandy::compile($template, array(
  "flags" => LightnCandy::FLAG_ELSE
));
$render = LightnCandy::prepare($php);
$data = array(
  "foo" => ""
);
echo $render($data);
handlebars.js
Data:
{
  "foo": ""
}
Template:
{{#unless foo}}YES{{else}}no{{/unless}}
Result:
YES
Source Code
var Handlebars = require('handlebars');
var template = '{{#unless foo}}YES{{else}}no{{/unless}}';

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

Empty array means false

lightncandy

Used option: FLAG_ELSE

Data:
array(
  "foo" => array(

  )
)
Template:
{{#unless foo}}YES{{else}}no{{/unless}}
Result:
YES
Source Code
require('./vendor/autoload.php');
use LightnCandy\LightnCandy;
$template = "{{#unless foo}}YES{{else}}no{{/unless}}";

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

  )
);
echo $render($data);
handlebars.js
Data:
{
  "foo": []
}
Template:
{{#unless foo}}YES{{else}}no{{/unless}}
Result:
YES
Source Code
var Handlebars = require('handlebars');
var template = '{{#unless foo}}YES{{else}}no{{/unless}}';

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

0 means false

lightncandy

Used option: FLAG_ELSE

Data:
array(
  "foo" => 0
)
Template:
{{#unless foo}}YES{{else}}no{{/unless}}
Result:
YES
Source Code
require('./vendor/autoload.php');
use LightnCandy\LightnCandy;
$template = "{{#unless foo}}YES{{else}}no{{/unless}}";

$php = LightnCandy::compile($template, array(
  "flags" => LightnCandy::FLAG_ELSE
));
$render = LightnCandy::prepare($php);
$data = array(
  "foo" => 0
);
echo $render($data);
handlebars.js
Data:
{
  "foo": 0
}
Template:
{{#unless foo}}YES{{else}}no{{/unless}}
Result:
YES
Source Code
var Handlebars = require('handlebars');
var template = '{{#unless foo}}YES{{else}}no{{/unless}}';

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

See Also...