1. Go to this page and download the library: Download opine/lightncandy library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
opine / lightncandy example snippets
// THREE STEPS TO USE LIGHTNCANDY
// Step 1. "Welcome {{name}} , You win \${{value}} dollars!!\n";
$phpStr = LightnCandy::compile($template); // compiled PHP code in $phpStr
// Step 2A. (Usage 1) use LightnCandy::prepare to get rendering function
// DEPRECATED , it may hpStr);
// Step 2B. (Usage 2) Store your render function in a file
// You decide your compiled template file path and name, save it.
// You can load your render function by
// Loading partial from file system only when valid directory is provided by basedir option
// '.' means getpwd()
LightnCandy::compile($template, Array(
'basedir' => '.'
));
// Multiple basedir and fileext are supported
LightnCandy::compile($template, Array(
'flags' => LightnCandy::FLAG_STANDALONE,
'basedir' => Array(
'/usr/local/share/handlebars/templates',
'/usr/local/share/my_project/templates',
'/usr/local/share/my_project/partials',
),
'fileext' => Array(
'.tmpl',
'.mustache',
'.handlebars',
)
));
LightnCandy::compile($template, Array(
// FLAG_NAMEDARG is rray(
// 1. You may pass your function name
// When the function is not exist, you get compile time error
// In this case, the helper name is same with function name
// Template: {{my_helper_functoin ....}}
'my_helper_function',
// 2. You may also provide a static call from a class
// In this case, the helper name is same with provided full name
// **DEPRECATED** It is not valid in handlebars.js
// Template: {{myClass::myStaticMethod ....}}
'myClass::myStaticMethod',
// 3. You may also provide an alias name for helper function
// This help you to mapping different function to a preferred helper name
// Template: {{helper_name ....}}
'helper_name' => 'my_other_helper',
// 4. Alias also works well for static call of a class
// This help you to mapping different function to a preferred helper name
// Template: {{helper_name2 ....}}
'helper_name2' => 'myClass::func',
// 5. Anonymous function should be provided with alias
// The function will be
function myhelper ($args, $named) {
if (count($args)) {
// handle no name arguments....
}
// access foo=bar from $named['foo'] ...
}
// escaping is handled by lightncandy and decided by template
// if the helper is in {{ }} , you get 'The U&ME Helper is ececuted!'
// if the helper is in {{{ }}} , you get 'The U&ME Helper is executed!'
return 'The U&ME Helper is executed!';
// Same as above because the escape_flag is DEFAULT
// 0, false, null, undefined, or '' means DEFAULT
return Array('The U&ME Helper is executed!');
return Array('The U&ME Helper is executed!', false);
return Array('The U&ME Helper is executed!', 0);
// escaping is handled by the helper, lightncandy will do nothing
// No matter in {{ }} or {{{ }}} , you get 'Exact&Same output \' \" Ya!'
return Array('Exact&Same output \' " Ya!', 'raw');
// force lightncandy escaping the helper result
// No matter in {{ }} or {{{ }}} , you get 'Not&Same output ' " Ya!'
return Array('Not&Same output \' " Ya!', 'enc');
// force lightncandy escaping the helper result in handlebars.js way
// No matter in {{ }} or {{{ }}} , you get 'Not&Same output ' " Ya!'
return Array('Not&Same output \' " Ya!', 'encq');
LightnCandy::compile($template, Array(
'blockhelpers' => Array( // The usage of blockhelpers option is similar with helpers option.
'my_helper_function', // You can use function name, class name with static method,
... // and choose preferred helper name by providing key name.
)
));
// Only render inner block when input > 5
// {{#helper_iffivemore total_people}}More then 5 people, discount!{{/helper_iffivemore}}
function helper_iffivemore($cx, $args, $named) {
return $args[0] > 5 ? $cx : null;
}
// You can use named arguments, too
// {{#helper_if value=people logic="more" tovalue=5}}Yes the logic is true{{/helper_if}}
function helper_if($cx, $args, $named) {
switch ($args['logic']) {
case 'more':
return $named['value'] > $named['tovalue'] ? $cx : null;
case 'less':
return $named['value'] < $named['tovalue'] ? $cx : null;
case 'eq':
return $named['value'] == $named['tovalue'] ? $cx : null;
}
}
// Provide default values for name and salary
// {{#helper_defaultpeople}}Hello, {{name}} ....Your salary will be {{salary}}{{/helper_defaultpeople}}
function helper_defaultpeople($cx, $args, $named) {
if (!isset($cx['name'])) {
$cx['name'] = 'Sir';
}
$cx['salary'] = isset($cx['salary']) ? '$' . $cx['salary'] : 'unknown';
return $cx;
}
// Provide specific context to innerblock
// {{#helper_sample}}Preview Name:{{name}} , Salary:{{salary}}.{{/helper_sample}}
function helper_sample($cx, $args) {
return Array('name' => 'Sample Name', 'salary' => 'Sample Salary');
}
// Provide specific context to innerblock, then loop on it.
// {{#helper_categories}}{{#each .}}<li><a href="?id={{id}}">{{name}}</a></li>{{/each}}{{/helper_categories}}
function helper_categories($cx, $args) {
return getMyCategories(); // Array('category1', 'category2', ...)
}
// LightnCandy sample, #mywith works same with #with
$php = LightnCandy::compile($template, Array(
'flags' => LightnCandy::FLAG_HANDLEBARSJS,
'hbhelpers' => Array(
'mywith' => function ($context, $options) {
return $options['fn']($context);
}
)
));
// LightnCandy sample, #myeach works same with #each
$php = LightnCandy::compile($template, Array(
'flags' => LightnCandy::FLAG_HANDLEBARSJS,
'hbhelpers' => Array(
'myeach' => function ($context, $options) {
$ret = '';
foreach ($context as $cx) {
$ret .= $options['fn']($cx);
}
return $ret;
}
)
));
// LightnCandy sample, #myif works same with #if
$php = LightnCandy::compile($template, Array(
'flags' => LightnCandy::FLAG_HANDLEBARSJS,
'hbhelpers' => Array(
'myif' => function ($conditional, $options) {
if ($conditional) {
return $options['fn']();
} else {
return $options['inverse']();
}
}
)
));
$php = LightnCandy::compile($template, Array(
'flags' => LightnCandy::FLAG_HANDLEBARSJS,
'hbhelpers' => Array(
'sample' => function ($arg1, $arg2, $options) {
// All hashed arguments are in $options['hash']
}
)
));
$template = "Hello! {{name}} is {{gender}}.
Test1: {{@root.name}}
Test2: {{@root.gender}}
Test3: {{../test3}}
Test4: {{../../test4}}
Test5: {{../../.}}
Test6: {{../../[test'6]}}
{{#each .}}
each Value: {{.}}
{{/each}}
{{#.}}
section Value: {{.}}
{{/.}}
{{#if .}}IF OK!{{/if}}
{{#unless .}}Unless not OK!{{/unless}}
";
// compile to debug version
$php = LightnCandy::compile($template, Array(
'flags' => LightnCandy::FLAG_RENDER_DEBUG | LightnCandy::FLAG_HANDLEBARSJS
));
// Get the render function
$renderer = LightnCandy::prepare($php);
// error_log() when missing data:
// LCRun3: [gender] is not exist
// LCRun3: ../[test] is not exist
$renderer(Array('name' => 'John'), LCRun3::DEBUG_ERROR_LOG);
// Output visual debug template with ANSI color:
echo $renderer(Array('name' => 'John'), LCRun3::DEBUG_TAGS_ANSI);
// Output debug template with HTML comments:
echo $renderer(Array('name' => 'John'), LCRun3::DEBUG_TAGS_HTML);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.