Exemplo n.º 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;
 }
Exemplo n.º 2
0
 /**
  * Parse a subexpression then return parsed result.
  *
  * @param string $expression the full string of a sub expression
  * @param array<string,array|string|integer> $context current compile context
  *
  * @return array<boolean|integer|array> Return parsed result
  *
  * @expect array(\LightnCandy\Parser::SUBEXP, array(array('a'), array('b')), '(a b)') when input '(a b)', array('usedFeature' => array('subexp' => 0), 'flags' => array('advar' => 0, 'namev' => 0, 'this' => 0, 'exhlp' => 1, 'strpar' => 0))
  */
 public static function subexpression($expression, &$context)
 {
     $context['usedFeature']['subexp']++;
     $vars = static::analyze(substr($expression, 1, -1), $context);
     $avars = static::advancedVariable($vars, $context, $expression);
     if (isset($avars[0][0]) && !$context['flags']['exhlp']) {
         if (!Validator::helper($context, $avars[0][0], true)) {
             $context['error'][] = "Can not find custom helper function defination {$avars[0][0]}() !";
         }
     }
     return array(static::SUBEXP, $avars, $expression);
 }