PHP code example of contentasaurus / lightncandy
1. Go to this page and download the library: Download contentasaurus/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/ */
contentasaurus / lightncandy example snippets
LightnCandy::compile($template, Array(
'flags' => LightnCandy::FLAG_ERROR_LOG | LightnCandy::FLAG_STANDALONEPHP
));
// LightnCandy sample, #mywith works same with #with
$php = LightnCandy::compile($template, Array(
'flags' => LightnCandy::FLAG_HANDLEBARSJS,
'helpers' => 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,
'helpers' => 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,
'helpers' => Array(
'myif' => function ($conditional, $options) {
if ($conditional) {
return $options['fn']();
} else {
return $options['inverse']();
}
}
)
));
$php = LightnCandy::compile($template, Array(
'flags' => LightnCandy::FLAG_HANDLEBARSJS,
'helpers' => Array(
'getRoot' => function ($options) {
print_r($options['_this']); // dump current context
return $options['data']['root']; // same as {{@root}}
}
)
));
$php = LightnCandy::compile($template, Array(
'flags' => LightnCandy::FLAG_HANDLEBARSJS,
'helpers' => Array(
'list' => function ($context, $options) {
$out = '';
$data = $options['data'];
foreach ($context as $idx => $cx) {
$data['index'] = $idx;
$out .= $options['fn']($cx, Array('data' => $data));
}
return $out;
}
)
));
LightnCandy::compile('I wanna use <% foo %> as delimiters!', Array(
'delimiters' => array('<%', '%>')
));
$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:
// LightnCandy\Runtime: [gender] is not exist
// LightnCandy\Runtime: ../[test] is not exist
$renderer(Array('name' => 'John'), array('debug' => LightnCandy\Runtime::DEBUG_ERROR_LOG));
// Output visual debug template with ANSI color:
echo $renderer(Array('name' => 'John'), array('debug' => LightnCandy\Runtime::DEBUG_TAGS_ANSI));
// Output debug template with HTML comments:
echo $renderer(Array('name' => 'John'), array('debug' => LightnCandy\Runtime::DEBUG_TAGS_HTML));
$php = LightnCandy::compile($template, Array(
'flags' => LightnCandy::FLAG_HANDLEBARSJS,
'prepartial' => function ($context, $template, $name) {
return "<!-- partial start: $name -->$template<!-- partial end: $name -->";
}
));
$php = LightnCandy::compile($template, Array(
'flags' => LightnCandy::FLAG_HANDLEBARSJS,
'renderex' => '// Compiled at ' . date('Y-m-d h:i:s')
));
function ($in) {
$cx = array(...);
// compiled at 1999-12-31 00:00:00
return .....
}
// Customized runtime library to debug {{{foo}}}
class MyRunTime extends LightnCandy\Runtime {
public static function raw($cx, $v) {
return '[[DEBUG:raw()=>' . var_export($v, true) . ']]';
}
}
// Use MyRunTime as runtime library
$php = LightnCandy::compile($template, Array(
'flags' => LightnCandy::FLAG_HANDLEBARSJS,
'runtime' => 'MyRunTime'
));