Handlebars: Built-in block helper - #if

Provide simple if/else rendering logic.

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

Samples

True!

lightncandy

Used option: FLAG_ELSE

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

$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:
{{#if foo}}YES{{else}}no{{/if}}
Result:
YES
Source Code
var Handlebars = require('handlebars');
var template = '{{#if foo}}YES{{else}}no{{/if}}';

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:
{{#if foo}}YES{{else}}no{{/if}}
Result:
Command failed: php .exec_tmp_file 2>/dev/null
Source Code
require('./vendor/autoload.php');
use LightnCandy\LightnCandy;
$template = "{{#if foo}}YES{{else}}no{{/if}}";

$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:
{{#if foo}}YES{{else}}no{{/if}}
Result:
YES
Source Code
var Handlebars = require('handlebars');
var template = '{{#if foo}}YES{{else}}no{{/if}}';

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:
{{#if foo}}YES{{else}}no{{/if}}
Result:
Command failed: php .exec_tmp_file 2>/dev/null
Source Code
require('./vendor/autoload.php');
use LightnCandy\LightnCandy;
$template = "{{#if foo}}YES{{else}}no{{/if}}";

$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:
{{#if foo}}YES{{else}}no{{/if}}
Result:
YES
Source Code
var Handlebars = require('handlebars');
var template = '{{#if foo}}YES{{else}}no{{/if}}';

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:
{{#if foo}}YES{{else}}no{{/if}}
Result:
Command failed: php .exec_tmp_file 2>/dev/null
Source Code
require('./vendor/autoload.php');
use LightnCandy\LightnCandy;
$template = "{{#if foo}}YES{{else}}no{{/if}}";

$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:
{{#if foo}}YES{{else}}no{{/if}}
Result:
YES
Source Code
var Handlebars = require('handlebars');
var template = '{{#if foo}}YES{{else}}no{{/if}}';

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:
{{#if foo}}YES{{else}}no{{/if}}
Result:
Command failed: php .exec_tmp_file 2>/dev/null
Source Code
require('./vendor/autoload.php');
use LightnCandy\LightnCandy;
$template = "{{#if foo}}YES{{else}}no{{/if}}";

$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:
{{#if foo}}YES{{else}}no{{/if}}
Result:
YES
Source Code
var Handlebars = require('handlebars');
var template = '{{#if foo}}YES{{else}}no{{/if}}';

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:
{{#if foo}}YES{{else}}no{{/if}}
Result:
Command failed: php .exec_tmp_file 2>/dev/null
Source Code
require('./vendor/autoload.php');
use LightnCandy\LightnCandy;
$template = "{{#if foo}}YES{{else}}no{{/if}}";

$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:
{{#if foo}}YES{{else}}no{{/if}}
Result:
YES
Source Code
var Handlebars = require('handlebars');
var template = '{{#if foo}}YES{{else}}no{{/if}}';

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:
{{#if foo}}YES{{else}}no{{/if}}
Result:
Command failed: php .exec_tmp_file 2>/dev/null
Source Code
require('./vendor/autoload.php');
use LightnCandy\LightnCandy;
$template = "{{#if foo}}YES{{else}}no{{/if}}";

$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:
{{#if foo}}YES{{else}}no{{/if}}
Result:
no
Source Code
var Handlebars = require('handlebars');
var template = '{{#if foo}}YES{{else}}no{{/if}}';

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:
{{#if foo}}YES{{else}}no{{/if}}
Result:
Command failed: php .exec_tmp_file 2>/dev/null
Source Code
require('./vendor/autoload.php');
use LightnCandy\LightnCandy;
$template = "{{#if foo}}YES{{else}}no{{/if}}";

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

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:
{{#if foo}}YES{{else}}no{{/if}}
Result:
Command failed: php .exec_tmp_file 2>/dev/null
Source Code
require('./vendor/autoload.php');
use LightnCandy\LightnCandy;
$template = "{{#if foo}}YES{{else}}no{{/if}}";

$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:
{{#if foo}}YES{{else}}no{{/if}}
Result:
no
Source Code
var Handlebars = require('handlebars');
var template = '{{#if foo}}YES{{else}}no{{/if}}';

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:
{{#if foo}}YES{{else}}no{{/if}}
Result:
Command failed: php .exec_tmp_file 2>/dev/null
Source Code
require('./vendor/autoload.php');
use LightnCandy\LightnCandy;
$template = "{{#if foo}}YES{{else}}no{{/if}}";

$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:
{{#if foo}}YES{{else}}no{{/if}}
Result:
no
Source Code
var Handlebars = require('handlebars');
var template = '{{#if foo}}YES{{else}}no{{/if}}';

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

See Also...