### Install LightnCandy with Composer Source: https://github.com/zordius/lightncandy/blob/master/README.md Use Composer to install the LightnCandy package. This command fetches the dev-master version. ```bash composer require zordius/lightncandy:dev-master ``` -------------------------------- ### LightnCandy::prepare Source: https://github.com/zordius/lightncandy/blob/master/docs/classes/LightnCandy-LightnCandy.html Get a working render function by a string of PHP code. ```APIDOC ## LightnCandy::prepare ### Description Get a working render function by a string of PHP code. This method may requires php setting allow_url_include=1 and allow_url_fopen=1 , or access right to tmp file system. ### Parameters - **$php** (string) - Required - PHP code - **$tmpDir** (string|null) - Optional - Change temp directory for php include file saved by prepare() when cannot include PHP code with data:// format. - **$delete** (bool) - Optional - Delete temp php file when set to true. Default is true, set it to false for debug propose ### Response - **Return** (Closure|false) - result of include() ``` -------------------------------- ### Compile Template with Options Source: https://github.com/zordius/lightncandy/blob/master/README.md Compile a template using LightnCandy, applying specific options. This example enables error logging and standalone PHP compilation. ```php LightnCandy::compile($template, array( 'flags' => LightnCandy::FLAG_ERROR_LOG | LightnCandy::FLAG_STANDALONEPHP )); ``` -------------------------------- ### Convert Token to String Source: https://github.com/zordius/lightncandy/blob/master/docs/classes/LightnCandy-Parser.html Examples of converting tokens to strings with optional merging. ```php public static toString(array $token[, array|null $merge = null ]) : string ``` ```php 'c' when input array(0, 'a', 'b', 'c', 'd', 'e') ``` ```php 'cd' when input array(0, 'a', 'b', 'c', 'd', 'e', 'f') ``` ```php 'qd' when input array(0, 'a', 'b', 'c', 'd', 'e', 'f'), array(3 => 'q') ``` -------------------------------- ### Parse subexpressions Source: https://github.com/zordius/lightncandy/blob/master/docs/classes/LightnCandy-Parser.html Examples of parsing subexpressions with various input configurations. ```php array(false, array(array('foo'), array(-1, "' =='"), array('bar'))) when input array(0,0,0,0,0,0,0,"foo ' ==' bar"), array('flags' => array('strpar' => 0, 'advar' => 1, 'namev' => 1, 'noesc' => 0, 'this' => 0), 'rawblock' => false) ``` ```php array(false, array(array('a'), array(' b c'))) when input array(0,0,0,0,0,0,0,'a \[ b c\]'), array('flags' => array('strpar' => 0, 'advar' => 1, 'this' => 1, 'namev' => 1, 'noesc' => 0), 'rawblock' => false) ``` ```php array(false, array(array('a'), 'q' => array(-1, "' d e'"))) when input array(0,0,0,0,0,0,0,"a q=' d e'"), array('flags' => array('strpar' => 0, 'advar' => 1, 'this' => 1, 'namev' => 1, 'noesc' => 0), 'rawblock' => false) ``` ```php array(false, array('q' => array(-2, array(array('foo'), array('bar')), '( foo bar)'))) when input array(0,0,0,0,0,0,0,'q=( foo bar)'), array('flags' => array('strpar' => 0, 'advar' => 1, 'this' => 1, 'namev' => 1, 'noesc' => 0, 'exhlp' => 0, 'lambda' => 0), 'usedFeature' => array('subexp' => 0), 'ops' => array('seperator' => 0), 'rawblock' => false, 'helperresolver' => 0) ``` ```php array(false, array(array('foo'))) when input array(0,0,0,0,0,0,'>','foo'), array('flags' => array('strpar' => 0, 'advar' => 1, 'this' => 1, 'namev' => 1, 'noesc' => 0, 'exhlp' => 0, 'lambda' => 0), 'usedFeature' => array('subexp' => 0), 'ops' => array('seperator' => 0), 'rawblock' => false) ``` ```php array(false, array(array('foo'))) when input array(0,0,0,0,0,0,'>','"foo"'), array('flags' => array('strpar' => 0, 'advar' => 1, 'this' => 1, 'namev' => 1, 'noesc' => 0, 'exhlp' => 0, 'lambda' => 0), 'usedFeature' => array('subexp' => 0), 'ops' => array('seperator' => 0), 'rawblock' => false) ``` ```php array(false, array(array('foo'))) when input array(0,0,0,0,0,0,'>','\[foo\] '), array('flags' => array('strpar' => 0, 'advar' => 1, 'this' => 1, 'namev' => 1, 'noesc' => 0, 'exhlp' => 0, 'lambda' => 0), 'usedFeature' => array('subexp' => 0), 'ops' => array('seperator' => 0), 'rawblock' => false) ``` ```php array(false, array(array('foo'))) when input array(0,0,0,0,0,0,'>','\'foo\''), array('flags' => array('strpar' => 0, 'advar' => 1, 'this' => 1, 'namev' => 1, 'noesc' => 0, 'exhlp' => 0, 'lambda' => 0), 'usedFeature' => array('subexp' => 0), 'ops' => array('seperator' => 0), 'rawblock' => false) ``` ```php array(\LightnCandy\Parser::SUBEXP, array(array('a'), array('b')), '(a b)') when input '(a b)', array('usedFeature' => array('subexp' => 0), 'flags' => array('advar' => 0, 'namev' => 0, 'this' => 0, 'exhlp' => 1, 'strpar' => 0)) ``` -------------------------------- ### LightnCandy Token Delimiter Setup Source: https://github.com/zordius/lightncandy/blob/master/docs/classes/LightnCandy-Parser.html Demonstrates how to set the delimiters for LightnCandy tokens. This method allows for default delimiters or custom left and right delimiter strings to be defined. ```APIDOC ## POST /api/tokens/setDelimiter ### Description Sets the delimiter for LightnCandy tokens, either using default values or custom strings for the left and right delimiters. ### Method `public static setDelimiter(array &$context[, string|null $left = null ][, string|null $right = null ]) : mixed` ### Parameters #### Path Parameters - **context** (array) - Required - The current context array to modify. - **left** (string|null) - Optional - The string to use for the left delimiter. - **right** (string|null) - Optional - The string to use for the right delimiter. ### Response #### Success Response (200) - **mixed** - The result of setting the delimiter, typically indicating success or failure. #### Response Example ```json { "status": "success", "message": "Delimiters set successfully." } ``` ``` -------------------------------- ### LightnCandy Token to String Conversion Source: https://github.com/zordius/lightncandy/blob/master/docs/classes/LightnCandy-Parser.html Explains the `toString` method, which converts a LightnCandy token into its string representation. It supports merging additional token strings and provides examples for various scenarios. ```APIDOC ## POST /api/tokens/toString ### Description Converts a detected Handlebars token (or a list of tokens) into its string representation. It can optionally merge additional token strings. ### Method `public static toString(array $token[, array|null $merge = null ]) : string` ### Parameters #### Path Parameters - **token** (array) - Required - The detected Handlebars token(s) to convert. - **merge** (array|null) - Optional - Additional token strings to merge with the primary token. ### Request Example ```json { "token": [0, "a", "b", "c", "d", "e"], "merge": { "3": "q" } } ``` ### Response #### Success Response (200) - **string** - The resulting string representation of the token(s). #### Response Example ```json { "result": "cqd" } ``` ``` -------------------------------- ### compileSubExpression() Source: https://github.com/zordius/lightncandy/blob/master/docs/classes/LightnCandy-Compiler.html Get string presentation of a sub expression. ```APIDOC ## compileSubExpression() ### Description Get string presentation of a sub expression. ### Parameters - **context** (array) - Required - current compile context - **vars** (array) - Required - parsed arguments list ### Response - **Return** (array) - code representing passed expression ``` -------------------------------- ### Partials and Dynamic Partials Source: https://github.com/zordius/lightncandy/blob/master/README.md Explains how to include other templates (partials) and how to use dynamic partials. ```APIDOC ## Partials and Dynamic Partials ### Description This section details the usage of partials for template inclusion and dynamic partial rendering. ### Method N/A (Template Syntax) ### Endpoint N/A (Template Syntax) ### Parameters N/A ### Request Example ```handlebars {{>file}} {{>file foo}} {{>file foo bar=another}} {{>(helper) foo}} {{#> foo}}block{{/foo}} {{#> @partial-block}} ``` ### Response N/A (Template Syntax) ``` -------------------------------- ### prepare() Source: https://github.com/zordius/lightncandy/blob/master/docs/classes/LightnCandy-LightnCandy.html Generates a render function from compiled PHP code. ```APIDOC ## prepare() ### Description Get a working render function by a string of PHP code. This method may require php setting allow_url_include=1 and allow_url_fopen=1, or access right to tmp file system. ### Response - **return** (Closure|false) - A closure representing the render function or false on failure. ``` -------------------------------- ### getVariableNameOrSubExpression Method Source: https://github.com/zordius/lightncandy/blob/master/docs/classes/LightnCandy-Compiler.html This method is used to get the string representation of a subexpression or a variable within the LightnCandy compilation context. ```APIDOC ## protected static getVariableNameOrSubExpression(array &$context, array $var) : array ### Description Get string presentation of a subexpression or a variable. ### Method `protected static` ### Endpoint `Compiler.php` : 227 ### Parameters #### Path Parameters - **context** (array) - Required - current compile context - **var** (array) - Required - variable parsed path ### Return values #### Success Response (200) - **array** - variable names ### Request Example ```json { "context": [ "spvar", "debug" ], "var": [ "this", "id" ] } ``` ### Response Example ```json { "variableNames": [ "LR::v($cx, $in, isset($in) ? $in : null, array('id'))" ] } ``` ``` -------------------------------- ### Initialize CSS variables Source: https://github.com/zordius/lightncandy/blob/master/docs/classes/LightnCandy-Expression.html Initializes CSS variables for the documentation theme. ```javascript cssVars({}); ``` -------------------------------- ### exch() Source: https://github.com/zordius/lightncandy/blob/master/docs/classes/LightnCandy-Runtime.html Executes a custom helper with prepared options. ```APIDOC ## static exch() ### Description Execute custom helper with prepared options. ### Parameters - **$cx** (array) - Required - render time context for lightncandy - **$ch** (string) - Required - the name of custom helper to be executed - **$vars** (array|string|int|null) - Required - variables for the helper - **$options** (array) - Required - the options object ### Response - **string** - The rendered string of the token ``` -------------------------------- ### Rendering with Callback and Inverse Callback Source: https://github.com/zordius/lightncandy/blob/master/docs/classes/LightnCandy-Runtime.html Demonstrates rendering using both a primary callback and an inverse callback. The inverse callback is invoked under specific conditions, allowing for alternative rendering logic. ```PHP expect 'inv' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'lambda' => 0)), array(), null, 0, true, function ($c, $i) {return 'cb';}, function ($c, $i) {return 'inv';} ``` ```PHP expect 'inv' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'lambda' => 0)), array(), null, 0, false, function ($c, $i) {return 'cb';}, function ($c, $i) {return 'inv';} ``` ```PHP expect 'inv' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'lambda' => 0)), false, null, 0, true, function ($c, $i) {return 'cb';}, function ($c, $i) {return 'inv';} ``` ```PHP expect 'inv' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'mustsec' => 0, 'lambda' => 0)), false, null, 0, false, function ($c, $i) {return 'cb';}, function ($c, $i) {return 'inv';} ``` ```PHP expect 'inv' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'lambda' => 0)), '', null, 0, true, function ($c, $i) {return 'cb';}, function ($c, $i) {return 'inv';} ``` ```PHP expect 'cb' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'mustsec' => 0, 'lambda' => 0)), '', null, 0, false, function ($c, $i) {return 'cb';}, function ($c, $i) {return 'inv';} ``` ```PHP expect 'inv' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'lambda' => 0)), 0, null, 0, true, function ($c, $i) {return 'cb';}, function ($c, $i) {return 'inv';} ``` ```PHP expect 'cb' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'mustsec' => 0, 'lambda' => 0)), 0, null, 0, false, function ($c, $i) {return 'cb';}, function ($c, $i) {return 'inv';} ``` ```PHP expect 'inv' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'lambda' => 0)), new stdClass, null, 0, true, function ($c, $i) {return 'cb';}, function ($c, $i) {return 'inv';} ``` ```PHP expect 'cb' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'mustsec' => 0, 'lambda' => 0)), new stdClass, null, 0, false, function ($c, $i) {return 'cb';}, function ($c, $i) {return 'inv';} ``` -------------------------------- ### Get expression array presentation Source: https://github.com/zordius/lightncandy/blob/master/docs/classes/LightnCandy-Parser.html Returns an array representation for a given expression. This method is used internally for parsing and structuring expressions within the context of compilation. ```php protected static getExpression(string $v, array &$context, int $pos) : array ``` -------------------------------- ### LightnCandy::prepare() - Create Render Function from PHP Code Source: https://context7.com/zordius/lightncandy/llms.txt Converts a compiled PHP code string into an executable closure without requiring file operations. Useful for development and testing. ```APIDOC ## LightnCandy::prepare() ### Description Converts a compiled PHP code string into an executable closure without requiring file operations. Useful for development and testing scenarios where you don't want to save the compiled template to disk. ### Method `LightnCandy::prepare(string $phpCode)` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```php {{#each items}}
  • {{name}}: ${{price}}
  • {{/each}}'; $phpCode = LightnCandy::compile($template, array( 'flags' => LightnCandy::FLAG_HANDLEBARSJS )); // Create render function directly from PHP code string $renderer = LightnCandy::prepare($phpCode); $data = array( 'items' => array( array('name' => 'Apple', 'price' => 1.50), array('name' => 'Orange', 'price' => 2.00), array('name' => 'Banana', 'price' => 0.75) ) ); echo $renderer($data); // Output:
    • Apple: $1.5
    • Orange: $2
    • Banana: $0.75
    ``` ### Response #### Success Response (200) Returns an executable closure (renderer function). #### Response Example ```php // Example of returned closure (simplified) function($data) { // ... rendering logic ... return '
    • Apple: $1.5
    • Orange: $2
    • Banana: $0.75
    '; } ``` ``` -------------------------------- ### LightnCandy Runtime Methods Source: https://github.com/zordius/lightncandy/blob/master/docs/classes/LightnCandy-Runtime.html Methods for handling template logic including partials, sections, logging, and context management. ```APIDOC ## static in() ### Description Handles inline partials for {{#* inlinepartial}}. ### Parameters - **cx** (array) - Required - render time context for lightncandy - **p** (string) - Required - partial name - **code** (Closure) - Required - the compiled partial code ### Response - **mixed** - Returns the result of the partial execution. ## static isec() ### Description Handles inverted sections for {{^var}}. ### Parameters - **cx** (array) - Required - render time context for lightncandy - **v** (array|string|int|null) - Required - value to be tested ### Response - **bool** - Returns true when the value is not null nor false. ## static lo() ### Description Handles logging for {{log}}. ### Parameters - **cx** (array) - Required - render time context for lightncandy - **v** (string) - Required - expression ### Response - **mixed** ## static m() ### Description Get merged context. ### Parameters - **cx** (array) - Required - render time context for lightncandy - **a** (array|string|int|null) - Required - the context to be merged - **b** (array|string|int|null) - Required - the new context to overwrite ### Response - **array|string|int** - the merged context object ## static miss() ### Description Handle missing data error. ### Parameters - **cx** (array) - Required - render time context for lightncandy - **v** (string) - Required - expression ### Response - **mixed** ## static p() ### Description Handles partials for {{> partial}}. ### Parameters - **cx** (array) - Required - render time context for lightncandy - **p** (string) - Required - partial name - **v** (array|string|int|null) - Required - value to be the new context - **pid** (mixed) - Required - **sp** (mixed) - Optional - default is '' ### Response - **string** - The rendered string of the partial ``` -------------------------------- ### Get Literal Variable Name Source: https://github.com/zordius/lightncandy/blob/master/docs/classes/LightnCandy-Parser.html The `getLiteral` method returns an array representation for a literal variable name. The `asis` parameter controls whether the name is kept as is, and `quote` adds single quotes. ```php protected static getLiteral(string $name, bool $asis[, bool $quote = false ]) : array ``` -------------------------------- ### Basic Rendering with String Output Source: https://github.com/zordius/lightncandy/blob/master/docs/classes/LightnCandy-Runtime.html Renders a string based on input flags and a callback function. Used when a specific string output is expected. ```PHP expect '' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'mustsec' => 0, 'lambda' => 0)), false, null, false, false, function () {return 'A';} ``` ```PHP expect '' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'mustsec' => 0, 'lambda' => 0)), null, null, null, false, function () {return 'A';} ``` ```PHP expect 'A' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'mustsec' => 0, 'lambda' => 0)), true, null, true, false, function () {return 'A';} ``` ```PHP expect 'A' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'mustsec' => 0, 'lambda' => 0)), 0, null, 0, false, function () {return 'A';} ``` -------------------------------- ### LightnCandy Subexpression Parsing Source: https://github.com/zordius/lightncandy/blob/master/docs/classes/LightnCandy-Parser.html Details the `subexpression` method, which parses a given expression string and returns the parsed result within the current compilation context. Includes an example of parsing a simple subexpression. ```APIDOC ## POST /api/parser/subexpression ### Description Parses a subexpression string and returns the structured result. This method is crucial for handling nested expressions within templates. ### Method `public static subexpression(string $expression, array &$context) : array` ### Parameters #### Path Parameters - **expression** (string) - Required - The full string of the subexpression to parse. - **context** (array) - Required - The current compilation context. ### Request Example ```json { "expression": "(a b)", "context": { "usedFeature": {"subexp": 0}, "flags": {"advar": 0, "namev": 0, "this": 0, "exhlp": 1, "strpar": 0} } } ``` ### Response #### Success Response (200) - **array** - The parsed result of the subexpression. #### Response Example ```json { "result": [ "__subexp__", [ ["a"], ["b"] ] ] } ``` ``` -------------------------------- ### LightnCandy\Runtime::wi() Source: https://github.com/zordius/lightncandy/blob/master/docs/classes/LightnCandy-Runtime.html Handles the logic for the {{#with}} block helper in LightnCandy templates. ```APIDOC ## static wi() ### Description Implements the logic for the {{#with}} block helper. ### Parameters - **$cx** (array) - Required - Render time context for lightncandy - **$v** (array|string|int|null) - Required - Value to be the new context - **$bp** (array|null) - Required - Block parameters - **$in** (array|string|int|null) - Required - Input data with current scope - **$cb** (Closure) - Required - Callback function to render child context - **$else** (Closure|null) - Optional - Callback function to render child context when {{else}} ### Response - **Return** (string) - The rendered string of the token ``` -------------------------------- ### Compile and Render with Debugging Source: https://github.com/zordius/lightncandy/blob/master/README.md Compiles a template with debug flags and demonstrates various runtime debug output options. ```php $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 $phpStr = LightnCandy::compile($template, array( 'flags' => LightnCandy::FLAG_RENDER_DEBUG | LightnCandy::FLAG_HANDLEBARSJS )); // Save the compiled PHP code into a php file file_put_contents('render.php', ''); // Get the render function from the php file $renderer = include('render.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)); ``` -------------------------------- ### Create Render Function from PHP Code Source: https://context7.com/zordius/lightncandy/llms.txt LightnCandy::prepare() converts compiled PHP code string into an executable closure without file operations. This is useful for development and testing. ```php {{#each items}}
  • {{name}}: ${{price}}
  • {{/each}}'; $phpCode = LightnCandy::compile($template, array( 'flags' => LightnCandy::FLAG_HANDLEBARSJS )); // Create render function directly from PHP code string $renderer = LightnCandy::prepare($phpCode); $data = array( 'items' => array( array('name' => 'Apple', 'price' => 1.50), array('name' => 'Orange', 'price' => 2.00), array('name' => 'Banana', 'price' => 0.75) ) ); echo $renderer($data); // Output:
    • Apple: $1.5
    • Orange: $2
    • Banana: $0.75
    ``` -------------------------------- ### Access root context and data variables Source: https://github.com/zordius/lightncandy/blob/master/README.md Demonstrates accessing the current context and root data within a helper. ```php $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}} } ) )); ``` ```javascript Handlebars.registerHelper('getRoot', function(options) { console.log(this); // dump current context return options.data.root; // same as {{@root}} }); ``` -------------------------------- ### LightnCandy Namespace Overview Source: https://github.com/zordius/lightncandy/blob/master/docs/files/src-loader.html Overview of the LightnCandy namespace and its associated PHP loader. ```APIDOC ## LightnCandy Namespace ### Description The LightnCandy namespace contains the core logic for the template engine. The library includes a loader.php file which serves as the primary PHP loader for the library. ### Author Zordius (zordius@gmail.com) ``` -------------------------------- ### LightnCandy Context Methods Source: https://github.com/zordius/lightncandy/blob/master/docs/classes/LightnCandy-Context.html Static methods for managing and manipulating LightnCandy contexts. ```APIDOC ## POST /context/create ### Description Creates a context from provided options. ### Method POST ### Endpoint `/context/create` ### Parameters #### Request Body - **options** (array) - Required - Input options for context creation. ### Response #### Success Response (200) - **context** (array) - The created context from options. ## POST /context/merge ### Description Merges a temporary context into a master context. ### Method POST ### Endpoint `/context/merge` ### Parameters #### Request Body - **context** (array) - Required - The master context to merge into. - **tmp** (array) - Required - The temporary context to be merged. ### Response #### Success Response (200) - **result** (mixed) - Indicates the result of the merge operation. ## POST /context/updateHelperTable ### Description Updates a specific custom helper table within the context from options. ### Method POST ### Endpoint `/context/updateHelperTable` ### Parameters #### Request Body - **context** (array) - Required - The prepared context to update. - **options** (array) - Required - Input options containing helper table data. - **tname** (string) - Optional - The name of the helper table to update. Defaults to 'helpers'. ### Response #### Success Response (200) - **updated_context** (array) - The context with the updated helper table. ``` -------------------------------- ### Implement #mywith context change helper Source: https://github.com/zordius/lightncandy/blob/master/README.md A custom helper that mimics the behavior of the built-in #with helper. ```php // 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); } ) )); ``` ```javascript // Handlebars.js sample, #mywith works same with #with Handlebars.registerHelper('mywith', function(context, options) { return options.fn(context); }); ``` -------------------------------- ### LightnCandy Compiler Overview Source: https://github.com/zordius/lightncandy/blob/master/docs/files/src-compiler.html This section provides an overview of the LightnCandy compiler's documentation structure. ```APIDOC ## LightnCandy Compiler Documentation ### Description This page details the structure of the LightnCandy compiler's documentation, including namespaces, packages, reports, and indices. ### Method N/A ### Endpoint N/A ### Parameters N/A ### Request Example N/A ### Response N/A ``` -------------------------------- ### Packages Source: https://github.com/zordius/lightncandy/blob/master/docs/files/src-compiler.html Lists the available packages within the LightnCandy project. ```APIDOC ## Packages ### [LightnCandy](packages/LightnCandy.html) This package contains the main LightnCandy compiler classes. ``` -------------------------------- ### Registering Custom Helper Functions in PHP Source: https://context7.com/zordius/lightncandy/llms.txt Define simple, block, and conditional helpers within the LightnCandy compile options. Use SafeString to return HTML content without automatic escaping. ```php LightnCandy::FLAG_HANDLEBARSJS, 'helpers' => array( // Simple helper - returns formatted string 'formatDate' => function($timestamp) { return date('Y-m-d H:i:s', $timestamp); }, // Block helper - wraps content in HTML 'bold' => function($options) { return '' . $options['fn']() . ''; }, // Block helper with iteration 'list' => function($items, $options) { $out = '
      '; foreach ($items as $item) { $out .= '
    • ' . $options['fn']($item) . '
    • '; } return $out . '
    '; }, // Conditional block helper 'ifEquals' => function($a, $b, $options) { if ($a === $b) { return $options['fn'](); } return $options['inverse'](); }, // Return SafeString to prevent HTML escaping 'link' => function($url, $text) { return new SafeString("$text"); } ) )); $renderer = LightnCandy::prepare($phpCode); echo $renderer(array( 'timestamp' => time(), 'message' => 'Read this!', 'items' => array( array('name' => 'Item 1'), array('name' => 'Item 2') ), 'status' => 'active' )); ``` -------------------------------- ### Implement #myeach context change helper Source: https://github.com/zordius/lightncandy/blob/master/README.md A custom helper that iterates over a context, mimicking the built-in #each helper. ```php // 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; } ) )); ``` ```javascript // Handlebars.js sample, #myeach works same with #each Handlebars.registerHelper('myeach', function(context, options) { var ret = '', i, j = context.length; for (i = 0; i < j; i++) { ret = ret + options.fn(context[i]); } return ret; }); ``` -------------------------------- ### Helper Execution and Arguments Source: https://github.com/zordius/lightncandy/blob/master/README.md Covers how to execute custom helpers with various argument types, including named arguments and subexpressions. ```APIDOC ## Helper Execution and Arguments ### Description This section explains how to execute custom helpers with different argument types, including string, number, boolean, null, undefined, named arguments, and subexpressions. ### Method N/A (Template Syntax) ### Endpoint N/A (Template Syntax) ### Parameters N/A ### Request Example ```handlebars {{{helper var}}} {{helper var}} {{helper "str"}} {{helper 'str'}} {{helper 123 null true false undefined}} {{helper name1=var name2=var2}} {{#helper ...}}...{{/helper}} {{helper (helper2 foo) bar}} ``` ### Response N/A (Template Syntax) ``` -------------------------------- ### Optimize Rendering with FLAG_BESTPERFORMANCE Source: https://context7.com/zordius/lightncandy/llms.txt Uses echo statements and standalone runtime for maximum performance. Requires capturing output via output buffering when using FLAG_ECHO. ```php {{title}}

    {{heading}}

    {{#each sections}}

    {{name}}

    {{content}}

    {{/each}} '; // FLAG_BESTPERFORMANCE is the default and optimal for production $phpCode = LightnCandy::compile($template, array( 'flags' => LightnCandy::FLAG_BESTPERFORMANCE | LightnCandy::FLAG_HANDLEBARS )); file_put_contents('optimized_template.php', ' 'My Page', 'heading' => 'Welcome', 'sections' => array( array('name' => 'About', 'content' => 'About us content'), array('name' => 'Contact', 'content' => 'Contact information') ) )); $html = ob_get_clean(); ``` -------------------------------- ### Compiler::with() Source: https://github.com/zordius/lightncandy/blob/master/docs/classes/LightnCandy-Compiler.html Compiles 'with' tokens within the LightnCandy templating engine. ```APIDOC ## protected static with(array &$context, array $vars) : string|null ### Description Compiles 'with' tokens within the LightnCandy templating engine. ### Method `protected static with` ### Parameters - **context** (array) - Required - current compile context - **vars** (array) - Required - parsed arguments list ### Return values - **string|null** - Return compiled code segment for the token ``` -------------------------------- ### LightnCandy Exporter Methods Source: https://github.com/zordius/lightncandy/blob/master/docs/classes/LightnCandy-Exporter.html Documentation for the static methods available in the LightnCandy\Exporter class. ```APIDOC ## constants() ### Description Export Runtime constants. ### Parameters - **context** (array) - Required - current compile context ### Response - **return** (string) - The exported constants code. ## getClassMethods() ### Description Get methods from ReflectionClass. ### Parameters - **context** (array) - Required - current compile context - **class** (ReflectionClass) - Required - instance of the ReflectionClass ### Response - **return** (array) - List of methods. ## helpers() ### Description Export required custom helper functions. ### Parameters - **context** (array) - Required - current compile context ### Response - **return** (string) - The exported helper functions code. ## runtime() ### Description Export required standalone Runtime methods. ### Parameters - **context** (array) - Required - current compile context ### Response - **return** (string) - The exported runtime methods code. ``` -------------------------------- ### Namespaces Source: https://github.com/zordius/lightncandy/blob/master/docs/files/src-compiler.html Lists the available namespaces within the LightnCandy project. ```APIDOC ## Namespaces ### [LightnCandy](namespaces/lightncandy.html) This namespace contains the core functionality of the LightnCandy compiler. ``` -------------------------------- ### compileLog() Source: https://github.com/zordius/lightncandy/blob/master/docs/classes/LightnCandy-Compiler.html Compiles Handlebars log tokens into PHP code. ```APIDOC ## compileLog() ### Description Return compiled PHP code for a handlebars log token ### Method `protected static compileLog(array &$context, array &$vars, bool $raw) : string` ### Parameters #### Path Parameters - **context** (array) - Required - current compile context - **vars** (array) - Required - parsed arguments list - **raw** (bool) - Required - is this {{{ token or not ### Return values string — Return compiled code segment for the token ``` -------------------------------- ### Rendering with Array Input and String Formatting Source: https://github.com/zordius/lightncandy/blob/master/docs/classes/LightnCandy-Runtime.html Renders formatted strings from array inputs using a callback. Useful for generating dynamic string outputs based on array elements. ```PHP expect '-a=' when input array('scopes' => array(), 'flags' => array('spvar' => 0, 'mustlam' => 0, 'lambda' => 0)), array('a'), null, array('a'), false, function ($c, $i) {return "-$i=";} ``` ```PHP expect '-a=-b=' when input array('scopes' => array(), 'flags' => array('spvar' => 0, 'mustlam' => 0, 'lambda' => 0)), array('a','b'), null, array('a','b'), false, function ($c, $i) {return "-$i=";} ``` ```PHP expect '' when input array('scopes' => array(), 'flags' => array('spvar' => 0, 'mustlam' => 0, 'lambda' => 0)), 'abc', null, 'abc', true, function ($c, $i) {return "-$i=";} ``` ```PHP expect '-b=' when input array('scopes' => array(), 'flags' => array('spvar' => 0, 'mustlam' => 0, 'lambda' => 0)), array('a' => 'b'), null, array('a' => 'b'), true, function ($c, $i) {return "-$i=";} ``` -------------------------------- ### customHelper() Source: https://github.com/zordius/lightncandy/blob/master/docs/classes/LightnCandy-Compiler.html Compiles Handlebars custom helper tokens into PHP code. ```APIDOC ## customHelper() ### Description Return compiled PHP code for a handlebars custom helper token ### Method `protected static customHelper(array &$context, array $vars, bool $raw, bool $nosep[, bool $subExp = false ]) : string|null` ### Parameters #### Path Parameters - **context** (array) - Required - current compile context - **vars** (array) - Required - parsed arguments list - **raw** (bool) - Required - is this {{{ token or not - **nosep** (bool) - Required - true to compile without seperator - **subExp** (bool) - Optional - true when compile for subexpression ### Return values string|null — Return compiled code segment for the token when the token is custom helper ``` -------------------------------- ### Customize Rendering Runtime Class Source: https://github.com/zordius/lightncandy/blob/master/README.md Extends the LightnCandy\Runtime class to provide custom logic for template tags. ```php // 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' )); ``` -------------------------------- ### Execute Helper Source: https://github.com/zordius/lightncandy/blob/master/docs/classes/LightnCandy-Runtime.html Generic helper execution method for the runtime. ```php public static hbch(mixed &$cx, mixed $ch, mixed $vars, mixed $op, mixed &$_this) : mixed ``` -------------------------------- ### Handle {{#with}} Block (wi) Source: https://github.com/zordius/lightncandy/blob/master/docs/classes/LightnCandy-Runtime.html The wi() method is specifically designed to handle the {{#with}} block in LightnCandy templates. It manages context changes and callback rendering for the block's content and optional else clause. ```php public static wi(array $cx, array|string|int|null $v, array|null $bp, array|string|int|null $in, Closure $cb[, Closure|null $else = null ]): string ``` -------------------------------- ### pushToken() Source: https://github.com/zordius/lightncandy/blob/master/docs/classes/LightnCandy-Validator.html Pushes a token into the stack if it is not an empty string. ```APIDOC ## pushToken() ### Description Push a token into the stack when it is not empty string. ### Parameters - **$context** (array) - Required - Current context - **$token** (string|array) - Required - A parsed token or a string ### Response - **mixed** - Returns the result of the push operation. ```