LightnCandy option: FLAG_RUNTIMEPARTIAL

Compile the partial as callable. This enables recursive partials or context change for partials.

Sample Codes

Default is to compile the partial as static code

Used option: FLAG_JSOBJECT

Partials:
foo{{hello}}
Data:
NULL
Template:
OK{{> foo}}!
Result:
use \LightnCandy\SafeString as SafeString;use \LightnCandy\Runtime as LR;return function ($in = null, $options = null) {
    $helpers = array();
    $partials = array();
    $cx = array(
        'flags' => array(
            'jstrue' => false,
            'jsobj' => true,
            'jslen' => false,
            'spvar' => false,
            'prop' => false,
            'method' => false,
            'lambda' => false,
            'mustlok' => false,
            'mustlam' => false,
            'mustsec' => false,
            'echo' => false,
            'partnc' => false,
            'knohlp' => false,
            'debug' => isset($options['debug']) ? $options['debug'] : 1,
        ),
        'constants' => array(),
        'helpers' => isset($options['helpers']) ? array_merge($helpers, $options['helpers']) : $helpers,
        'partials' => isset($options['partials']) ? array_merge($partials, $options['partials']) : $partials,
        'scopes' => array(),
        'sp_vars' => isset($options['data']) ? array_merge(array('root' => $in), $options['data']) : array('root' => $in),
        'blparam' => array(),
        'partialid' => 0,
        'runtime' => '\LightnCandy\Runtime',
    );
    
    $inary=is_array($in);
    return 'OK'.''.LR::enc($cx, (($inary && isset($in['hello'])) ? $in['hello'] : null)).''.'!';
};
Source Code
require('./vendor/autoload.php');
use LightnCandy\LightnCandy;
$template = "OK{{> foo}}!";

$php = LightnCandy::compile($template, array(
  "flags" => LightnCandy::FLAG_JSOBJECT,
  "partials" => array(
    "foo" => "{{hello}}"
  )
));

echo $php

Compile used partial as embed callable code

Used option: FLAG_JSOBJECT FLAG_RUNTIMEPARTIAL

Partials:
foo{{hello}}
moo{{not used}}
Data:
NULL
Template:
OK{{> foo}}!
Result:
use \LightnCandy\SafeString as SafeString;use \LightnCandy\Runtime as LR;return function ($in = null, $options = null) {
    $helpers = array();
    $partials = array('foo' => function ($cx, $in, $sp) {$inary=is_array($in);return ''.$sp.''.LR::enc($cx, (($inary && isset($in['hello'])) ? $in['hello'] : null)).'';});
    $cx = array(
        'flags' => array(
            'jstrue' => false,
            'jsobj' => true,
            'jslen' => false,
            'spvar' => false,
            'prop' => false,
            'method' => false,
            'lambda' => false,
            'mustlok' => false,
            'mustlam' => false,
            'mustsec' => false,
            'echo' => false,
            'partnc' => false,
            'knohlp' => false,
            'debug' => isset($options['debug']) ? $options['debug'] : 1,
        ),
        'constants' => array(),
        'helpers' => isset($options['helpers']) ? array_merge($helpers, $options['helpers']) : $helpers,
        'partials' => isset($options['partials']) ? array_merge($partials, $options['partials']) : $partials,
        'scopes' => array(),
        'sp_vars' => isset($options['data']) ? array_merge(array('root' => $in), $options['data']) : array('root' => $in),
        'blparam' => array(),
        'partialid' => 0,
        'runtime' => '\LightnCandy\Runtime',
    );
    
    $inary=is_array($in);
    return 'OK'.LR::p($cx, 'foo', array(array($in),array()),0).'!';
};
Source Code
require('./vendor/autoload.php');
use LightnCandy\LightnCandy;
$template = "OK{{> foo}}!";

$php = LightnCandy::compile($template, array(
  "flags" => LightnCandy::FLAG_JSOBJECT | LightnCandy::FLAG_RUNTIMEPARTIAL,
  "partials" => array(
    "foo" => "{{hello}}",
    "moo" => "{{not used}}"
  )
));

echo $php

Default to not support context change on partial

Used option: FLAG_ERROR_LOG

Partials:
foo{{hello}}
Data:
NULL
Template:
OK{{> foo bar}}!
Result:
Do not support {{> foo bar}}, you should do compile with LightnCandy::FLAG_RUNTIMEPARTIAL flag
Source Code
require('./vendor/autoload.php');
use LightnCandy\LightnCandy;
$template = "OK{{> foo bar}}!";

$php = LightnCandy::compile($template, array(
  "flags" => LightnCandy::FLAG_ERROR_LOG,
  "partials" => array(
    "foo" => "{{hello}}"
  )
));

Use another context for the partial

Used option: FLAG_RUNTIMEPARTIAL

Partials:
foo{{hello}}
Data:
array(
  "bar" => array(
    "hello" => "World"
  )
)
Template:
OK{{> foo bar}}!
Result:
OKWorld!
Source Code
require('./vendor/autoload.php');
use LightnCandy\LightnCandy;
$template = "OK{{> foo bar}}!";

$php = LightnCandy::compile($template, array(
  "flags" => LightnCandy::FLAG_RUNTIMEPARTIAL,
  "partials" => array(
    "foo" => "{{hello}}"
  )
));
$render = LightnCandy::prepare($php);
$data = array(
  "bar" => array(
    "hello" => "World"
  )
);
echo $render($data);

When using dynamic partial, all partials will be compiled into your render function.

Used option: FLAG_RUNTIMEPARTIAL

Partials:
foo1Partial foo one
foo2Partial foo two
foo3Partial foo {{moo}}
Data:
NULL
Template:
{{> (foo)}}
Result:
use \LightnCandy\SafeString as SafeString;use \LightnCandy\Runtime as LR;return function ($in = null, $options = null) {
    $helpers = array(            'foo' => function() {
      return 'foo2';
    },
);
    $partials = array('foo3' => function ($cx, $in, $sp) {$inary=is_array($in);return ''.$sp.'Partial foo '.htmlspecialchars((string)(($inary && isset($in['moo'])) ? $in['moo'] : null), ENT_QUOTES, 'UTF-8').'';},
'foo2' => function ($cx, $in, $sp) {$inary=is_array($in);return ''.$sp.'Partial foo two';},
'foo1' => function ($cx, $in, $sp) {$inary=is_array($in);return ''.$sp.'Partial foo one';});
    $cx = array(
        'flags' => array(
            'jstrue' => false,
            'jsobj' => false,
            'jslen' => false,
            'spvar' => false,
            'prop' => false,
            'method' => false,
            'lambda' => false,
            'mustlok' => false,
            'mustlam' => false,
            'mustsec' => false,
            'echo' => false,
            'partnc' => false,
            'knohlp' => false,
            'debug' => isset($options['debug']) ? $options['debug'] : 1,
        ),
        'constants' => array(),
        'helpers' => isset($options['helpers']) ? array_merge($helpers, $options['helpers']) : $helpers,
        'partials' => isset($options['partials']) ? array_merge($partials, $options['partials']) : $partials,
        'scopes' => array(),
        'sp_vars' => isset($options['data']) ? array_merge(array('root' => $in), $options['data']) : array('root' => $in),
        'blparam' => array(),
        'partialid' => 0,
        'runtime' => '\LightnCandy\Runtime',
    );
    
    $inary=is_array($in);
    return ''.LR::p($cx, LR::hbch($cx, 'foo', array(array(),array()), 'raw', $in), array(array($in),array()),0).'';
};

Check the code to know used helper codes

Source Code
require('./vendor/autoload.php');
use LightnCandy\LightnCandy;
$template = "{{> (foo)}}";

$php = LightnCandy::compile($template, array(
  "flags" => LightnCandy::FLAG_RUNTIMEPARTIAL,
  "partials" => array(
    "foo1" => "Partial foo one",
    "foo2" => "Partial foo two",
    "foo3" => "Partial foo {{moo}}"
  ),
  "helpers" => array(
    "foo" => function () {
      return 'foo2';
    }
  )
));

echo $php

See Also...