LightnCandy: Custom Helper

Compile option 'helpers'

When compile(), you can use the compile option helpers to provide custom helpers. NOTICE: FLAG_NAMEDARG is required for named arguments, FLAG_ADVARNAME is required for string or subexpression arguments

Source Code
require_once('./vendor/autoload.php');
use LightnCandy\LightnCandy;

function my_helper_function () {
    return 'OK! (my_helper_function)';
}

function my_other_helper () {
    return 'OK! (my_other_helper)';
}

class myClass {
    public static function myStaticMethod () {
        return 'OK! (myClass::myStaticMethod)';
    }
}

$template = "1. {{my_helper_function}}\n2. {{myClass::myStaticMethod}}\n3. {{helper_name}}\n4. {{helper_name2}}\n5. {{helper_name3}}";
$phpStr = LightnCandy::compile($template, array(
    'flags' => LightnCandy::FLAG_HANDLEBARS,
    'helpers' => array(
        // 1. You may pass your function name
        //    When the function is not exist, you get compile time error
        //    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
        //    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::myStaticMethod',

        // 5. Anonymous function should be provided with alias
        //    The function will be included in generaed code always
        //    Template: {{helper_name3 ....}}
        'helper_name3' => function () {
            return 'OK! (helper_name3)';
        }
    )
));
$renderer = LightnCandy::prepare($phpStr);
echo $renderer(null);

Custom Helper Interface

The input arguments are processed by LightnCandy automatically, you do not need to worry about variable name processing or current context. You can also use double quoted string as input.

Source Code
require_once('./vendor/autoload.php');
use LightnCandy\LightnCandy;

$template = <<<VAREND
{{helper name}}              // This send the value of 'name' into the helper
{{helper "Test"}}            // This send the string "Test" into the helper (FLAG_ADVARNAME is required)
{{helper2 "Test" name}}      // This send string "Test" as first parameter,
                             // and the value of 'name' as second parameter into the helper
VAREND;

$phpStr = LightnCandy::compile($template, array(
    'flags' => LightnCandy::FLAG_HANDLEBARS,
    'helpers' => array(
      'helper' => function ($arg1) {
          return "ARG 1: $arg1";
      },
      'helper2' => function ($arg1, $arg2) {
          return "ARG 1: $arg1, ARG2: $arg2";
      }
    )
));
$renderer = LightnCandy::prepare($phpStr);
echo $renderer(array('name' => 'John'));

See Also...