Compile the partial as callable. This enables recursive partials or context change for partials.
Default is to compile the partial as static code
Used option: FLAG_JSOBJECT
Partials:foo | {{hello}} |
---|
NULL
OK
!
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)).''.'!';
};
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}} |
NULL
OK
!
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).'!';
};
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}} |
---|
NULL
OK
!
Do not support
, you should do compile with LightnCandy::FLAG_RUNTIMEPARTIAL flag
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}} |
---|
array(
"bar" => array(
"hello" => "World"
)
)
OK
!
OKWorld!
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:foo1 | Partial foo one |
---|---|
foo2 | Partial foo two |
foo3 | Partial foo {{moo}} |
NULL
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
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