LightnCandy\Compiler::composePHPRender PHP Méthode

composePHPRender() public static méthode

Compose LightnCandy render codes for include()
public static composePHPRender ( array\arraystring | integer> $context, string $code ) : string
$context array\arraystring | integer>
$code string generated PHP code
Résultat string Composed PHP code
    public static function composePHPRender($context, $code)
    {
        $flagJStrue = Expression::boolString($context['flags']['jstrue']);
        $flagJSObj = Expression::boolString($context['flags']['jsobj']);
        $flagSPVar = Expression::boolString($context['flags']['spvar']);
        $flagProp = Expression::boolString($context['flags']['prop']);
        $flagMethod = Expression::boolString($context['flags']['method']);
        $flagLambda = Expression::boolString($context['flags']['lambda']);
        $flagMustlok = Expression::boolString($context['flags']['mustlok']);
        $flagMustlam = Expression::boolString($context['flags']['mustlam']);
        $flagEcho = Expression::boolString($context['flags']['echo']);
        $flagPartNC = Expression::boolString($context['flags']['partnc']);
        $flagKnownHlp = Expression::boolString($context['flags']['knohlp']);
        $constants = Exporter::constants($context);
        $helpers = Exporter::helpers($context);
        $partials = implode(",\n", $context['partialCode']);
        $debug = Runtime::DEBUG_ERROR_LOG;
        $use = $context['flags']['standalone'] ? Exporter::runtime($context) : "use {$context['runtime']} as {$context['runtimealias']};";
        $safeString = $context['usedFeature']['enc'] > 0 && $context['flags']['standalone'] === 0 ? "use {$context['safestring']} as SafeString;" : '';
        $exportSafeString = $context['usedFeature']['enc'] > 0 && $context['flags']['standalone'] > 0 ? Exporter::safestring($context) : '';
        // Return generated PHP code string.
        return <<<VAREND
{$safeString}{$use}{$exportSafeString}return function (\$in = null, \$options = null) {
    \$helpers = {$helpers};
    \$partials = array({$partials});
    \$cx = array(
        'flags' => array(
            'jstrue' => {$flagJStrue},
            'jsobj' => {$flagJSObj},
            'spvar' => {$flagSPVar},
            'prop' => {$flagProp},
            'method' => {$flagMethod},
            'lambda' => {$flagLambda},
            'mustlok' => {$flagMustlok},
            'mustlam' => {$flagMustlam},
            'echo' => {$flagEcho},
            'partnc' => {$flagPartNC},
            'knohlp' => {$flagKnownHlp},
            'debug' => isset(\$options['debug']) ? \$options['debug'] : {$debug},
        ),
        'constants' => {$constants},
        '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' => '{$context['runtime']}',
    );
    {$context['renderex']}
    {$context['ops']['op_start']}'{$code}'{$context['ops']['op_end']}
};
VAREND;
    }

Usage Example

Exemple #1
0
 /**
  * Compile handlebars template into PHP code.
  *
  * @param string $template handlebars template string
  * @param array<string,array|string|integer> $options LightnCandy compile time and run time options, default is array('flags' => LightnCandy::FLAG_BESTPERFORMANCE)
  *
  * @return string|false Compiled PHP code when successed. If error happened and compile failed, return false.
  */
 public static function compile($template, $options = array('flags' => self::FLAG_BESTPERFORMANCE))
 {
     $context = Context::create($options);
     if (static::handleError($context)) {
         return false;
     }
     $code = Compiler::compileTemplate($context, SafeString::escapeTemplate($template));
     static::$lastParsed = Compiler::$lastParsed;
     // return false when fatal error
     if (static::handleError($context)) {
         return false;
     }
     // Or, return full PHP render codes as string
     return Compiler::composePHPRender($context, $code);
 }