Beispiel #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;
 }
Beispiel #2
0
 /**
  * handle raw block
  *
  * @param string[] $token detected handlebars {{ }} token
  * @param array<string,array|string|integer> $context current compile context
  *
  * @return boolean|null Return true when in rawblock mode
  */
 protected static function rawblock(&$token, &$context)
 {
     $inner = $token[Token::POS_INNERTAG];
     trim($inner);
     // skip parse when inside raw block
     if ($context['rawblock'] && !($token[Token::POS_BEGINRAW] === '{{' && $token[Token::POS_OP] === '/' && $context['rawblock'] === $inner)) {
         return true;
     }
     $token[Token::POS_INNERTAG] = $inner;
     // Handle raw block
     if ($token[Token::POS_BEGINRAW] === '{{') {
         if ($token[Token::POS_ENDRAW] !== '}}') {
             $context['error'][] = 'Bad token ' . Token::toString($token) . ' ! Do you mean ' . Token::toString($token, array(Token::POS_ENDRAW => '}}')) . ' ?';
         }
         if ($context['rawblock']) {
             Parser::setDelimiter($context);
             $context['rawblock'] = false;
         } else {
             if ($token[Token::POS_OP]) {
                 $context['error'][] = "Wrong raw block begin with " . Token::toString($token) . ' ! Remove "' . $token[Token::POS_OP] . '" to fix this issue.';
             }
             $context['rawblock'] = $token[Token::POS_INNERTAG];
             Parser::setDelimiter($context);
             $token[Token::POS_OP] = '#';
         }
         $token[Token::POS_ENDRAW] = '}}';
     }
 }