LightnCandy\Compiler::compileTemplate PHP Метод

compileTemplate() публичный статический Метод

Compile template into PHP code
public static compileTemplate ( array\arraystring | integer> &$context, string $template ) : string | null
$context array\arraystring | integer>
$template string handlebars template
Результат string | null generated PHP code
    public static function compileTemplate(&$context, $template)
    {
        array_unshift($context['parsed'], array());
        Validator::verify($context, $template);
        static::$lastParsed = $context['parsed'];
        if (count($context['error'])) {
            return;
        }
        Parser::setDelimiter($context);
        $context['compile'] = true;
        // Handle dynamic partials
        Partial::handleDynamic($context);
        // Do PHP code generation.
        $code = '';
        foreach ($context['parsed'][0] as $info) {
            if (is_array($info)) {
                $context['tokens']['current']++;
                $code .= "'" . static::compileToken($context, $info) . "'";
            } else {
                $code .= $info;
            }
        }
        array_shift($context['parsed']);
        return $code;
    }

Usage Example

Пример #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);
 }
All Usage Examples Of LightnCandy\Compiler::compileTemplate