/** * 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); }
/** * compile a template into a closure function * * @param array<string,array|string|integer> $context Current context of compiler progress. * @param string $template template string * * @return string $code compiled PHP code */ public static function compile(&$context, $template) { $tmpContext = $context; $tmpContext['inlinepartial'] = array(); $tmpContext['partialblock'] = array(); $code = Compiler::compileTemplate($tmpContext, str_replace('function', static::$TMP_JS_FUNCTION_STR, $template)); Context::merge($context, $tmpContext); if (!$context['flags']['noind']) { $sp = ', $sp'; $code = preg_replace('/^/m', "'{$context['ops']['seperator']}\$sp{$context['ops']['seperator']}'", $code); // callbacks inside partial should be aware of $sp $code = preg_replace('/\\bfunction\\s*\\(([^\\(]*?)\\)\\s*{/', 'function(\\1)use($sp){', $code); } else { $sp = ''; } $code = str_replace(static::$TMP_JS_FUNCTION_STR, 'function', $code); return "function (\$cx, \$in{$sp}) {{$context['ops']['op_start']}'{$code}'{$context['ops']['op_end']}}"; }