LightnCandy\Validator::verify PHP Method

verify() public static method

Verify template
public static verify ( array\arraystring | integer> &$context, string $template )
$context array\arraystring | integer>
$template string handlebars template
    public static function verify(&$context, $template)
    {
        $template = SafeString::stripExtendedComments($template);
        $context['level'] = 0;
        Parser::setDelimiter($context);
        while (preg_match($context['tokens']['search'], $template, $matches)) {
            // Skip a token when it is slash escaped
            if ($context['flags']['slash'] && $matches[Token::POS_LSPACE] === '' && preg_match('/^(.*?)(\\\\+)$/s', $matches[Token::POS_LOTHER], $escmatch)) {
                if (strlen($escmatch[2]) % 4) {
                    static::pushToken($context, substr($matches[Token::POS_LOTHER], 0, -2) . $context['tokens']['startchar']);
                    $matches[Token::POS_BEGINTAG] = substr($matches[Token::POS_BEGINTAG], 1);
                    $template = implode('', array_slice($matches, Token::POS_BEGINTAG));
                    continue;
                } else {
                    $matches[Token::POS_LOTHER] = $escmatch[1] . str_repeat('\\', strlen($escmatch[2]) / 2);
                }
            }
            $context['tokens']['count']++;
            $V = static::token($matches, $context);
            static::pushLeft($context);
            if ($V) {
                if (is_array($V)) {
                    array_push($V, $matches, $context['tokens']['partialind']);
                }
                static::pushToken($context, $V);
            }
            $template = "{$matches[Token::POS_RSPACE]}{$matches[Token::POS_ROTHER]}";
        }
        static::pushToken($context, $template);
        if ($context['level'] > 0) {
            array_pop($context['stack']);
            array_pop($context['stack']);
            $token = array_pop($context['stack']);
            $context['error'][] = 'Unclosed token ' . ($context['rawblock'] ? "{{{{{$token}}}}}" : ($context['partialblock'] ? "{{#>{$token}}}" : "{{#{$token}}}")) . ' !!';
        }
    }

Usage Example

示例#1
0
 /**
  * Compile template into PHP code
  *
  * @param array<string,array|string|integer> $context Current context
  * @param string $template handlebars template
  *
  * @return string|null generated PHP code
  */
 public static function compileTemplate(&$context, $template)
 {
     array_unshift($context['parsed'], array());
     Validator::verify($context, $template);
     if (count($context['error'])) {
         return;
     }
     // Do PHP code generation.
     Parser::setDelimiter($context);
     // Handle dynamic partials
     Partial::handleDynamic($context);
     $code = '';
     foreach ($context['parsed'][0] as $info) {
         if (is_array($info)) {
             $context['tokens']['current']++;
             $tmpl = static::compileToken($context, $info);
             if ($tmpl == $context['ops']['seperator']) {
                 $tmpl = '';
             } else {
                 $tmpl = "'{$tmpl}'";
             }
             $code .= $tmpl;
         } else {
             $code .= $info;
         }
     }
     static::$lastParsed = array_shift($context['parsed']);
     return $code;
 }