Debian Codegraph @

vendor/zordius/lightncandy/README.md

raw
1 LightnCandy
2 ===========
3
4 ⚡🍭 An extremely fast PHP implementation of handlebars ( http://handlebarsjs.com/ ) and mustache ( http://mustache.github.io/ ).
5
6 Travis CI status: [![Unit testing](https://travis-ci.org/zordius/lightncandy.svg?branch=master)](https://travis-ci.org/zordius/lightncandy) [![Regression testing](https://travis-ci.org/zordius/HandlebarsTest.svg?branch=master)](https://travis-ci.org/zordius/HandlebarsTest)
7
8 Scrutinizer CI status: [![Code Coverage](https://img.shields.io/scrutinizer/coverage/g/zordius/lightncandy.svg)](https://scrutinizer-ci.com/g/zordius/lightncandy/)
9
10 Package on packagist: [![Latest Stable Version](https://poser.pugx.org/zordius/lightncandy/v/stable.svg)](https://packagist.org/packages/zordius/lightncandy) [![License](https://poser.pugx.org/zordius/lightncandy/license.svg)](https://github.com/zordius/lightncandy/blob/master/LICENSE.md) [![Total Downloads](https://poser.pugx.org/zordius/lightncandy/downloads)](https://packagist.org/packages/zordius/lightncandy)
11
12 Features
13 --------
14
15 * Logicless template: mustache ( http://mustache.github.com/ ) or handlebars ( http://handlebarsjs.com/ ) .
16 * Compile template to **pure PHP** code. Examples:
17 * <a href="https://github.com/zordius/HandlebarsTest/blob/master/fixture/001-simple-vars.tmpl">Template A</a> generated <a href="https://github.com/zordius/HandlebarsTest/blob/master/fixture/001-simple-vars.php">PHP A</a>
18 * <a href="https://github.com/zordius/HandlebarsTest/blob/master/fixture/016-hb-eachthis.tmpl">Template B</a> generated <a href="https://github.com/zordius/HandlebarsTest/blob/master/fixture/016-hb-eachthis.php">PHP B</a>
19 * **FAST!**
20 * Runs 2~7 times faster than <a href="https://github.com/bobthecow/mustache.php">mustache.php</a> (Justin Hileman/bobthecow implementation).
21 * Runs 2~7 times faster than <a href="https://github.com/dingram/mustache-php">mustache-php</a> (Dave Ingram implementation).
22 * Runs 10~50 times faster than <a href="https://github.com/XaminProject/handlebars.php">handlebars.php</a>.
23 * Detail performance test reports can be found <a href="https://github.com/zordius/HandlebarsTest">here</a>, go http://zordius.github.io/HandlebarsTest/ to see charts.
24 * **SMALL!** all PHP files in 189K
25 * **ROBUST!**
26 * 100% supports <a href="https://github.com/mustache/spec">mustache spec v1.1.3</a>. For the optional lambda module, supports 4 of 10 specs.
27 * Supports almost all <a href="https://github.com/jbboehr/handlebars-spec">handlebars.js spec</a>
28 * Output <a href="https://github.com/zordius/HandlebarsTest/blob/master/FEATURES.md">SAME</a> with <a href="https://github.com/wycats/handlebars.js">handlebars.js</a>
29 * **FLEXIBLE!**
30 * Lot of <a href="#compile-options">options</a> to change features and behaviors.
31 * Context generation
32 * Analyze used features from your template (execute `LightnCandy::getContext()` to get it) .
33 * Debug
34 * <a href="#template-debugging">Generate debug version template</a>
35 * Find out missing data when rendering template.
36 * Generate visually debug template.
37 * Standalone Template
38 * The compiled PHP code can run without any PHP library. You do not need to include LightnCandy when execute rendering function.
39
40 Installation
41 ------------
42
43 Use Composer ( https://getcomposer.org/ ) to install LightnCandy:
44
45 ```
46 composer require zordius/lightncandy:dev-master
47 ```
48
49 **UPGRADE NOTICE**
50
51 * Please check <a href="HISTORY.md">HISTORY.md</a> for versions history.
52 * Please check <a href="UPGRADE.md">UPGRADE.md</a> for upgrade notice.
53
54 Documents
55 ---------
56
57 * <a href="https://zordius.github.io/HandlebarsCookbook/9000-quickstart.html">Quick Start</a>
58
59 Compile Options
60 ---------------
61
62 You can apply more options by running `LightnCandy::compile($template, $options)`:
63
64 ```php
65 LightnCandy::compile($template, array(
66 'flags' => LightnCandy::FLAG_ERROR_LOG | LightnCandy::FLAG_STANDALONEPHP
67 ));
68 ```
69
70 Default is to compile the template as PHP, which can be run as fast as possible (flags = <a href="https://zordius.github.io/HandlebarsCookbook/LC-FLAG_BESTPERFORMANCE.html">FLAG_BESTPERFORMANCE</a>).
71
72 **Error Handling**
73 * <a href="https://zordius.github.io/HandlebarsCookbook/LC-FLAG_ERROR_LOG.html">FLAG_ERROR_LOG</a>
74 * <a href="https://zordius.github.io/HandlebarsCookbook/LC-FLAG_ERROR_EXCEPTION.html">FLAG_ERROR_EXCEPTION</a>
75 * <a href="https://zordius.github.io/HandlebarsCookbook/LC-FLAG_ERROR_SKIPPARTIAL.html">FLAG_ERROR_SKIPPARTIAL</a>
76 * <a href="https://zordius.github.io/HandlebarsCookbook/LC-FLAG_RENDER_DEBUG.html">FLAG_RENDER_DEBUG</a>
77
78 **JavaScript Compatibility**
79 * <a href="https://zordius.github.io/HandlebarsCookbook/LC-FLAG_JSTRUE.html">FLAG_JSTRUE</a>
80 * <a href="https://zordius.github.io/HandlebarsCookbook/LC-FLAG_JSOBJECT.html">FLAG_JSOBJECT</a>
81 * <a href="https://zordius.github.io/HandlebarsCookbook/LC-FLAG_JSLENGTH.html">FLAG_JSLENGTH</a>
82 * <a href="https://zordius.github.io/HandlebarsCookbook/LC-FLAG_JS.html">FLAG_JS</a>
83
84 **Mustache Compatibility**
85 * `FLAG_MUSTACHELOOKUP` : align recursive lookup up behaviors with mustache specification. And, the rendering performance will be worse.
86 * `FLAG_MUSTACHELAMBDA` : support simple lambda logic as mustache specification. And, the rendering performance will be worse.
87 * `FLAG_NOHBHELPERS` : Do not compile handlebars.js builtin helpers. With this option, `{{#with}}`, `{{#if}}`, `{{#unless}}`, `{{#each}}` means normal section, and `{{#with foo}}`, `{{#if foo}}`, `{{#unless foo}}`, `{{#each foo}}` will cause compile error.
88 * `FLAG_MUSTACHESECTION` : align section context behaviors with mustache.js.
89 * `FLAG_MUSTACHE` : support all mustache specification but performance drop, same with `FLAG_ERROR_SKIPPARTIAL` + `FLAG_MUSTACHELOOKUP` + `FLAG_MUSTACHELAMBDA` + `FLAG_NOHBHELPERS` + `FLAG_RUNTIMEPARTIAL` + `FLAG_JSTRUE` + `FLAG_JSOBJECT`.
90
91 **Handlebars Compatibility**
92 * <a href="https://zordius.github.io/HandlebarsCookbook/LC-FLAG_THIS.html">FLAG_THIS</a>
93 * <a href="https://zordius.github.io/HandlebarsCookbook/LC-FLAG_PARENT.html">FLAG_PARENT</a>
94 * <a href="https://zordius.github.io/HandlebarsCookbook/LC-FLAG_HBESCAPE.html">FLAG_HBESCAPE</a>
95 * <a href="https://zordius.github.io/HandlebarsCookbook/LC-FLAG_ADVARNAME.html">FLAG_ADVARNAME</a>
96 * <a href="https://zordius.github.io/HandlebarsCookbook/LC-FLAG_NAMEDARG.html">FLAG_NAMEDARG</a>
97 * <a href="https://zordius.github.io/HandlebarsCookbook/LC-FLAG_SLASH.html">FLAG_SLASH</a>
98 * <a href="https://zordius.github.io/HandlebarsCookbook/LC-FLAG_ELSE.html">FLAG_ELSE</a>
99 * `FLAG_RAWBLOCK`: support `{{{{raw_block}}}}any_char_or_{{foo}}_as_raw_string{{{{/raw_block}}}}`.
100 * `FLAG_HANDLEBARSLAMBDA` : support lambda logic as handlebars.js specification. And, the rendering performance will be worse.
101 * `FLAG_SPVARS` : support special variables include @root, @index, @key, @first, @last. Otherwise, compile these variable names with default parsing logic.
102 * `FLAG_HANDLEBARS` : support most handlebars extensions and also keep performance good, same with `FLAG_THIS` + `FLAG_PARENT` + `FLAG_HBESCAPE` + `FLAG_ADVARNAME` + `FLAG_NAMEDARG` + `FLAG_SPVARS` + `FLAG_SLASH` + `FLAG_ELSE` + `FLAG_RAWBLOCK`.
103 * `FLAG_HANDLEBARSJS` : support most handlebars.js + javascript behaviors and also keep performance good, same with `FLAG_JS` + `FLAG_HANDLEBARS`.
104 * `FLAG_HANDLEBARSJS_FULL` : enable all supported handlebars.js behaviors but performance drop, same with `FLAG_HANDLEBARSJS` + `FLAG_INSTANCE` + `FLAG_RUNTIMEPARTIAL` + `FLAG_MUSTACHELOOKUP` + `FLAG_HANDLEBARSLAMBDA`.
105
106 **Handlebars Options**
107 * <a href="https://zordius.github.io/HandlebarsCookbook/LC-FLAG_NOESCAPE.html">FLAG_NOESCAPE</a>
108 * <a href="https://zordius.github.io/HandlebarsCookbook/LC-FLAG_PARTIALNEWCONTEXT.html">FLAG_PARTIALNEWCONTEXT</a>
109 * `FLAG_IGNORESTANDALONE` : prevent standalone detection on `{{#foo}}`, `{{/foo}}` or `{{^}}`, the behavior is same with handlebars.js ignoreStandalone compile time option.
110 * `FLAG_STRINGPARAMS` : pass variable name as string to helpers, the behavior is same with handlebars.js stringParams compile time option.
111 * `FLAG_KNOWNHELPERSONLY`: Only pass current context to lambda, the behavior is same with handlebars.js knownHelpersOnly compile time option.
112 * `FLAG_PREVENTINDENT` : align partial indent behavior with mustache specification. This is same with handlebars.js preventIndent copmile time option.
113
114 **PHP**
115 * <a href="https://zordius.github.io/HandlebarsCookbook/LC-FLAG_STANDALONEPHP.html">FLAG_STANDALONEPHP</a>
116 * <a href="https://zordius.github.io/HandlebarsCookbook/LC-FLAG_EXTHELPER.html">FLAG_EXTHELPER</a>
117 * <a href="https://zordius.github.io/HandlebarsCookbook/LC-FLAG_RUNTIMEPARTIAL.html">FLAG_RUNTIMEPARTIAL</a>
118 * <a href="https://zordius.github.io/HandlebarsCookbook/LC-FLAG_PROPERTY.html">FLAG_PROPERTY</a>
119 * <a href="https://zordius.github.io/HandlebarsCookbook/LC-FLAG_METHOD.html">FLAG_METHOD</a>
120 * <a href="https://zordius.github.io/HandlebarsCookbook/LC-FLAG_INSTANCE.html">FLAG_INSTANCE</a>
121 * <a href="https://zordius.github.io/HandlebarsCookbook/LC-FLAG_ECHO.html">FLAG_ECHO</a>
122 * <a href="https://zordius.github.io/HandlebarsCookbook/LC-FLAG_BESTPERFORMANCE.html">FLAG_BESTPERFORMANCE</a>
123
124 Partial Support
125 ---------------
126
127 * <a href="https://zordius.github.io/HandlebarsCookbook/0011-partial.html">Example of compile time partial</a>
128 * <a href="https://zordius.github.io/HandlebarsCookbook/0024-partialcontext.html">Example of partial context changing</a>
129 * <a href="https://zordius.github.io/HandlebarsCookbook/0028-dynamicpartial.html">use dynamic partial</a>
130 * <a href="https://zordius.github.io/HandlebarsCookbook/9902-lcop-partialresolver.html">The partialresolver option</a>
131
132 Custom Helper
133 -------------
134
135 * <a href="https://zordius.github.io/HandlebarsCookbook/9001-customhelper.html">Custom Helpers in LighnCandy</a>
136 * <a href="https://zordius.github.io/HandlebarsCookbook/9002-helperoptions.html">The $options Object</a>
137 * <a href="https://zordius.github.io/HandlebarsCookbook/9003-helperescaping.html">Use SafeString</a>
138 * <a href="https://zordius.github.io/HandlebarsCookbook/9901-lcop-helperresolver.html">The helperresolver option</a>
139
140 Custom Helper Examples
141 ----------------------
142
143 **#mywith (context change)**
144 * LightnCandy
145 ```php
146 // LightnCandy sample, #mywith works same with #with
147 $php = LightnCandy::compile($template, array(
148 'flags' => LightnCandy::FLAG_HANDLEBARSJS,
149 'helpers' => array(
150 'mywith' => function ($context, $options) {
151 return $options['fn']($context);
152 }
153 )
154 ));
155 ```
156
157 * Handlebars.js
158 ```javascript
159 // Handlebars.js sample, #mywith works same with #with
160 Handlebars.registerHelper('mywith', function(context, options) {
161 return options.fn(context);
162 });
163 ```
164
165 **#myeach (context change)**
166 * LightnCandy
167 ```php
168 // LightnCandy sample, #myeach works same with #each
169 $php = LightnCandy::compile($template, array(
170 'flags' => LightnCandy::FLAG_HANDLEBARSJS,
171 'helpers' => array(
172 'myeach' => function ($context, $options) {
173 $ret = '';
174 foreach ($context as $cx) {
175 $ret .= $options['fn']($cx);
176 }
177 return $ret;
178 }
179 )
180 ));
181 ```
182
183 * Handlebars.js
184 ```javascript
185 // Handlebars.js sample, #myeach works same with #each
186 Handlebars.registerHelper('myeach', function(context, options) {
187 var ret = '', i, j = context.length;
188 for (i = 0; i < j; i++) {
189 ret = ret + options.fn(context[i]);
190 }
191 return ret;
192 });
193 ```
194
195 **#myif (no context change)**
196 * LightnCandy
197 ```php
198 // LightnCandy sample, #myif works same with #if
199 $php = LightnCandy::compile($template, array(
200 'flags' => LightnCandy::FLAG_HANDLEBARSJS,
201 'helpers' => array(
202 'myif' => function ($conditional, $options) {
203 if ($conditional) {
204 return $options['fn']();
205 } else {
206 return $options['inverse']();
207 }
208 }
209 )
210 ));
211 ```
212
213 * Handlebars.js
214 ```javascript
215 // Handlebars.js sample, #myif works same with #if
216 Handlebars.registerHelper('myif', function(conditional, options) {
217 if (conditional) {
218 return options.fn(this);
219 } else {
220 return options.inverse(this);
221 }
222 });
223 ```
224
225 You can use `isset($options['fn'])` to detect your custom helper is a block or not; you can also use `isset($options['inverse'])` to detect the existence of `{{else}}`.
226
227 **Data variables and context**
228
229 You can get special data variables from `$options['data']`. Using `$options['_this']` to receive current context.
230
231 ```php
232 $php = LightnCandy::compile($template, array(
233 'flags' => LightnCandy::FLAG_HANDLEBARSJS,
234 'helpers' => array(
235 'getRoot' => function ($options) {
236 print_r($options['_this']); // dump current context
237 return $options['data']['root']; // same as {{@root}}
238 }
239 )
240 ));
241 ```
242
243 * Handlebars.js
244 ```javascript
245 Handlebars.registerHelper('getRoot', function(options) {
246 console.log(this); // dump current context
247 return options.data.root; // same as {{@root}}
248 });
249 ```
250
251 **Private variables**
252
253 You can inject private variables into inner block when you execute child block with second parameter. The example code showed similar behavior with `{{#each}}` which sets index for child block and can be accessed with `{{@index}}`.
254
255 * LightnCandy
256 ```php
257 $php = LightnCandy::compile($template, array(
258 'flags' => LightnCandy::FLAG_HANDLEBARSJS,
259 'helpers' => array(
260 'list' => function ($context, $options) {
261 $out = '';
262 $data = $options['data'];
263
264 foreach ($context as $idx => $cx) {
265 $data['index'] = $idx;
266 $out .= $options['fn']($cx, array('data' => $data));
267 }
268
269 return $out;
270 }
271 )
272 ));
273 ```
274
275 * Handlebars.js
276 ```javascript
277 Handlebars.registerHelper('list', function(context, options) {
278 var out = '';
279 var data = options.data ? Handlebars.createFrame(options.data) : undefined;
280
281 for (var i=0; i<context.length; i++) {
282 if (data) {
283 data.index = i;
284 }
285 out += options.fn(context[i], {data: data});
286 }
287 return out;
288 });
289 ```
290
291 Change Delimiters
292 -----------------
293
294 You may change delimiters from `{{` and `}}` to other strings. In the template, you can use `{{=<% %>=}}` to change delimiters to `<%` and `%>` , but the change will not affect included partials.
295
296 If you want to change default delimiters for a template and all included partials, you may `compile()` it with `delimiters` option:
297
298 ```php
299 LightnCandy::compile('I wanna use <% foo %> as delimiters!', array(
300 'delimiters' => array('<%', '%>')
301 ));
302 ```
303
304 Template Debugging
305 ------------------
306
307 When template error happened, LightnCandy::compile() will return false. You may compile with `FLAG_ERROR_LOG` to see more error message, or compile with `FLAG_ERROR_EXCEPTION` to catch the exception.
308
309 You may generate debug version of templates with `FLAG_RENDER_DEBUG` when compile() . The debug template contained more debug information and slower (TBD: performance result) , you may pass extra LightnCandy\Runtime options into render function to know more rendering error (missing data). For example:
310
311 ```php
312 $template = "Hello! {{name}} is {{gender}}.
313 Test1: {{@root.name}}
314 Test2: {{@root.gender}}
315 Test3: {{../test3}}
316 Test4: {{../../test4}}
317 Test5: {{../../.}}
318 Test6: {{../../[test'6]}}
319 {{#each .}}
320 each Value: {{.}}
321 {{/each}}
322 {{#.}}
323 section Value: {{.}}
324 {{/.}}
325 {{#if .}}IF OK!{{/if}}
326 {{#unless .}}Unless not OK!{{/unless}}
327 ";
328
329 // compile to debug version
330 $phpStr = LightnCandy::compile($template, array(
331 'flags' => LightnCandy::FLAG_RENDER_DEBUG | LightnCandy::FLAG_HANDLEBARSJS
332 ));
333
334 // Save the compiled PHP code into a php file
335 file_put_contents('render.php', '<?php ' . $phpStr . '?>');
336
337 // Get the render function from the php file
338 $renderer = include('render.php');
339
340 // error_log() when missing data:
341 // LightnCandy\Runtime: [gender] is not exist
342 // LightnCandy\Runtime: ../[test] is not exist
343 $renderer(array('name' => 'John'), array('debug' => LightnCandy\Runtime::DEBUG_ERROR_LOG));
344
345 // Output visual debug template with ANSI color:
346 echo $renderer(array('name' => 'John'), array('debug' => LightnCandy\Runtime::DEBUG_TAGS_ANSI));
347
348 // Output debug template with HTML comments:
349 echo $renderer(array('name' => 'John'), array('debug' => LightnCandy\Runtime::DEBUG_TAGS_HTML));
350 ```
351
352 The ANSI output will be:
353
354 <a href="tests/example_debug.php"><img src="https://github.com/zordius/lightncandy/raw/master/example_debug.png"/></a>
355
356 Here are the list of LightnCandy\Runtime debug options for render function:
357
358 * `DEBUG_ERROR_LOG` : error_log() when missing required data
359 * `DEBUG_ERROR_EXCEPTION` : throw exception when missing required data
360 * `DEBUG_TAGS` : turn the return value of render function into normalized mustache tags
361 * `DEBUG_TAGS_ANSI` : turn the return value of render function into normalized mustache tags with ANSI color
362 * `DEBUG_TAGS_HTML` : turn the return value of render function into normalized mustache tags with HTML comments
363
364 Preprocess Partials
365 -------------------
366
367 If you want to do extra process before the partial be compiled, you may use `prepartial` when `compile()`. For example, this sample adds HTML comments to identify the partial by the name:
368
369 ```php
370 $php = LightnCandy::compile($template, array(
371 'flags' => LightnCandy::FLAG_HANDLEBARSJS,
372 'prepartial' => function ($context, $template, $name) {
373 return "<!-- partial start: $name -->$template<!-- partial end: $name -->";
374 }
375 ));
376 ```
377
378 You may also extend <a href="https://zordius.github.io/lightncandy/class-LightnCandy.Partial.html">LightnCandy\Partial</a> by override the <a href="https://zordius.github.io/lightncandy/class-LightnCandy.Partial.html#_prePartial">prePartial()</a> static method to turn your preprocess into a built-in feature.
379
380 Customize Render Function
381 -------------------------
382
383 If you want to do extra tasks inside render function or add more comment, you may use `renderex` when `compile()` . For example, this sample embed the compile time comment into the template:
384
385 ```php
386 $php = LightnCandy::compile($template, array(
387 'flags' => LightnCandy::FLAG_HANDLEBARSJS,
388 'renderex' => '// Compiled at ' . date('Y-m-d h:i:s')
389 ));
390 ```
391
392 Your render function will be:
393
394 ```php
395 function ($in) {
396 $cx = array(...);
397 // compiled at 1999-12-31 00:00:00
398 return .....
399 }
400 ```
401
402 Please make sure the passed in `renderex` is valid PHP, LightnCandy will not check it.
403
404 Customize Rendering Runtime Class
405 ---------------------------------
406
407 If you want to extend `LightnCandy\Runtime` class and replace the default runtime library, you may use `runtime` when `compile()` . For example, this sample will generate render function based on your extended `MyRunTime`:
408
409 ```php
410 // Customized runtime library to debug {{{foo}}}
411 class MyRunTime extends LightnCandy\Runtime {
412 public static function raw($cx, $v) {
413 return '[[DEBUG:raw()=>' . var_export($v, true) . ']]';
414 }
415 }
416
417 // Use MyRunTime as runtime library
418 $php = LightnCandy::compile($template, array(
419 'flags' => LightnCandy::FLAG_HANDLEBARSJS,
420 'runtime' => 'MyRunTime'
421 ));
422 ```
423
424 Please make sure `MyRunTime` exists when compile() or rendering based on your `FLAG_STANDALONEPHP` .
425
426 Unsupported Feature
427 -------------------
428
429 * `{{foo/bar}}` style variable name, it is deprecated in official handlebars.js document, please use this style: `{{foo.bar}}`.
430
431 Suggested Handlebars Template Practices
432 ---------------------------------------
433
434 * Prevent to use `{{#with}}` . I think `{{path.to.val}}` is more readable then `{{#with path.to}}{{val}}{{/with}}`; when using `{{#with}}` you will confusing on scope changing. `{{#with}}` only save you very little time when you access many variables under same path, but cost you a lot time when you need to understand then maintain a template.
435 * use `{{{val}}}` when you do not require HTML escaped output on the value. It is better performance, too.
436 * Prevent to use custom helper if you want to reuse your template in different language. Or, you may need to implement different versions of helper in different languages.
437 * For best performance, you should only use 'compile on demand' pattern when you are in development stage. Before you go to production, you can `LightnCandy::compile()` on all your templates, save all generated PHP codes, and deploy these generated files (You may need to maintain a build process for this) . **DO NOT COMPILE ON PRODUCTION** , it also a best practice for security. Adding cache for 'compile on demand' is not the best solution. If you want to build some library or framework based on LightnCandy, think about this scenario.
438 * Recompile your templates when you upgrade LightnCandy every time.
439 * Persistant ESCAPING practice of `{` or `}` for both handlebars and lightncandy:
440 * If you want to display atomic `}}` , you can just use it without any trick. EX: `{{foo}} }}`
441 * If you want to display `}` just after any handlebars token, you can use this: `{{#with "}"}}{{.}}{{/with}}` . EX: `{{foo}}{{#with "}"}}{{.}}{{/with}}`
442 * If you want to display atomic `{` , you can just use it without any trick. EX: `{ and {{foo}}`.
443 * If you want to display `{{` , you can use `{{#with "{{"}}{{.}}{{/with}}`. EX: `{{#with "{{"}}{{.}}{{/with}}{{foo}}`
444
445 Detail Feature list
446 -------------------
447
448 Go http://handlebarsjs.com/ to see more feature description about handlebars.js. All features align with it.
449
450 * Exact same CR/LF behavior with handlebars.js
451 * Exact same CR/LF bahavior with mustache spec
452 * Exact same 'true' or 'false' output with handlebars.js (require `FLAG_JSTRUE`)
453 * Exact same '[object Object]' output or join(',' array) output with handlebars.js (require `FLAG_JSOBJECT`)
454 * Can place heading/tailing space, tab, CR/LF inside `{{ var }}` or `{{{ var }}}`
455 * Indent behavior of the partial same with mustache spec
456 * Recursive variable lookup to parent context behavior same with mustache spec (require `FLAG_MUSTACHELOOKUP`)
457 * `{{{value}}}` or `{{&value}}` : raw variable
458 * true as 'true' (require `FLAG_JSTRUE`)
459 * false as 'false' (require `FLAG_TRUE`)
460 * `{{value}}` : HTML escaped variable
461 * true as 'true' (require `FLAG_JSTRUE`)
462 * false as 'false' (require `FLAG_JSTRUE`)
463 * `{{{path.to.value}}}` : dot notation, raw
464 * `{{path.to.value}}` : dot notation, HTML escaped
465 * `{{.}}` : current context, HTML escaped
466 * `{{{.}}}` : current context, raw
467 * `{{this}}` : current context, HTML escaped (require `FLAG_THIS`)
468 * `{{{this}}}` : current context, raw (require `FLAG_THIS`)
469 * `{{#value}}` : section
470 * false, undefined and null will skip the section
471 * true will run the section with original scope
472 * All others will run the section with new scope (includes 0, 1, -1, '', '1', '0', '-1', 'false', Array, ...)
473 * `{{/value}}` : end section
474 * `{{^value}}` : inverted section
475 * false, undefined and null will run the section with original scope
476 * All others will skip the section (includes 0, 1, -1, '', '1', '0', '-1', 'false', Array, ...)
477 * `{{! comment}}` : comment
478 * `{{!-- comment or {{ or }} --}}` : extended comment that can contain }} or {{ .
479 * `{{=<% %>=}}` : set delimiter to custom string , the custom string can not contain `=` . Check http://mustache.github.io/mustache.5.html for more example.
480 * `{{#each var}}` : each loop
481 * `{{#each}}` : each loop on {{.}}
482 * `{{/each}}` : end loop
483 * `{{#each bar as |foo|}}` : echo loop on bar and set the value as foo. (require `FLAG_ADVARNAME`)
484 * `{{#each bar as |foo moo|}}` : echo loop on bar, set the value as foo, set the index as moo. (require `FLAG_ADVARNAME`)
485 * `{{#if var}}` : run if logic with original scope (null, false, empty Array and '' will skip this block)
486 * `{{#if foo includeZero=true}}` : result as true when foo === 0 (require `FLAG_NAMEDARG`)
487 * `{{/if}}` : end if
488 * `{{else}}` or `{{^}}` : run else logic, should between `{{#if var}}` and `{{/if}}` ; or between `{{#unless var}}` and `{{/unless}}`; or between `{{#foo}}` and `{{/foo}}`; or between `{{#each var}}` and `{{/each}}`; or between `{{#with var}}` and `{{/with}}`. (require `FLAG_ELSE`)
489 * `{{#if foo}} ... {{else if bar}} ... {{/if}}` : chained if else blocks
490 * `{{#unless var}}` : run unless logic with original scope (null, false, empty Array and '' will render this block)
491 * `{{#unless foo}} ... {{else if bar}} ... {{/unless}}` : chained unless else blocks
492 * `{{#unless foo}} ... {{else unless bar}} ... {{/unless}}` : chained unless else blocks
493 * `{{#foo}} ... {{else bar}} ... {{/foo}}` : custom helper chained else blocks
494 * `{{#with var}}` : change context scope. If the var is false or an empty array, skip included section.
495 * `{{#with bar as |foo|}}` : change context to bar and set the value as foo. (require `FLAG_ADVARNAME`)
496 * `{{lookup foo bar}}` : lookup foo by value of bar as key.
497 * `{{../var}}` : parent template scope. (require `FLAG_PARENT`)
498 * `{{>file}}` : partial; include another template inside a template.
499 * `{{>file foo}}` : partial with new context (require `FLAG_RUNTIMEPARTIAL`)
500 * `{{>file foo bar=another}}` : partial with new context which mixed with followed key value (require `FLAG_RUNTIMEPARTIAL`)
501 * `{{>(helper) foo}}` : include dynamic partial by name provided from a helper (require `FLAG_RUNTIMEPARTIAL`)
502 * `{{@index}}` : references to current index in a `{{#each}}` loop on an array. (require `FLAG_SPVARS`)
503 * `{{@key}}` : references to current key in a `{{#each}}` loop on an object. (require `FLAG_SPVARS`)
504 * `{{@root}}` : references to root context. (require `FLAG_SPVARS`)
505 * `{{@first}}` : true when looping at first item. (require `FLAG_SPVARS`)
506 * `{{@last}}` : true when looping at last item. (require `FLAG_SPVARS`)
507 * `{{@root.path.to.value}}` : references to root context then follow the path. (require `FLAG_SPVARS`)
508 * `{{@../index}}` : access to parent loop index. (require `FLAG_SPVARS` and `FLAG_PARENT`)
509 * `{{@../key}}` : access to parent loop key. (require `FLAG_SPVARS` and `FLAG_PARENT`)
510 * `{{foo.[ba.r].[#spec].0.ok}}` : references to $CurrentConext['foo']['ba.r']['#spec'][0]['ok'] . (require `FLAG_ADVARNAME`)
511 * `{{~any_valid_tag}}` : Space control, remove all previous spacing (includes CR/LF, tab, space; stop on any none spacing character)
512 * `{{any_valid_tag~}}` : Space control, remove all next spacing (includes CR/LF, tab, space; stop on any none spacing character)
513 * `{{{helper var}}}` : Execute custom helper then render the result
514 * `{{helper var}}` : Execute custom helper then render the HTML escaped result
515 * `{{helper "str"}}` or `{{helper 'str'}}` : Execute custom helper with string arguments (require `FLAG_ADVARNAME`)
516 * `{{helper 123 null true false undefined}}` : Pass number, true, false, null or undefined into helper
517 * `{{helper name1=var name2=var2}}` : Execute custom helper with named arguments (require `FLAG_NAMEDARG`)
518 * `{{#helper ...}}...{{/helper}}` : Execute block custom helper
519 * `{{helper (helper2 foo) bar}}` : Execute custom helpers as subexpression (require `FLAG_ADVARNAME`)
520 * `{{{{raw_block}}}} {{will_not_parsed}} {{{{/raw_block}}}}` : Raw block (require `FLAG_RAWBLOCK`)
521 * `{{#> foo}}block{{/foo}}` : Partial block, provide `foo` partial default content (require `FLAG_RUNTIMEPARTIAL`)
522 * `{{#> @partial-block}}` : access partial block content inside a partial
523 * `{{#*inline "partial_name"}}...{{/inline}}` : Inline partial, provide a partial and overwrite the original one.
524 * `{{log foo}}` : output value to stderr for debug.
525
526 Developer Notes
527 ---------------
528
529 Please read <a href=".github/CONTRIBUTING.md">CONTRIBUTING.md</a> for development environment setup.
530
531 Framework Integration
532 ---------------------
533
534 - [Slim 3.0.x](https://github.com/endel/slim-lightncandy-view)
535 - [Laravel 4](https://github.com/samwalshnz/lightncandy-l4)
536 - [Laravel 5](https://github.com/ProAI/laravel-handlebars)
537 - [yii2](https://github.com/kfreiman/yii2-lightncandy)
538 - [Symfony3](https://packagist.org/packages/jays-de/handlebars-bundle)
539
540 Tools
541 -----
542
543 - CLI: https://github.com/PXLbros/LightnCandy-CLI