Ejemplo n.º 1
0
 /**
  * {var ...}
  * {default ...}
  */
 public function macroVar(MacroNode $node, PhpWriter $writer)
 {
     if ($node->args === '' && $node->parentNode && $node->parentNode->name === 'switch') {
         return '} else {';
     } elseif ($node->name === 'assign') {
         trigger_error('Macro {assign} is deprecated; use {var} instead.', E_USER_DEPRECATED);
     }
     $var = TRUE;
     $tokens = $writer->preprocess();
     $res = new Latte\MacroTokens();
     while ($tokens->nextToken()) {
         if ($var && $tokens->isCurrent(Latte\MacroTokens::T_SYMBOL, Latte\MacroTokens::T_VARIABLE)) {
             if ($node->name === 'default') {
                 $res->append("'" . ltrim($tokens->currentValue(), '$') . "'");
             } else {
                 $res->append('$' . ltrim($tokens->currentValue(), '$'));
             }
             $var = NULL;
         } elseif ($tokens->isCurrent('=', '=>') && $tokens->depth === 0) {
             $res->append($node->name === 'default' ? '=>' : '=');
             $var = FALSE;
         } elseif ($tokens->isCurrent(',') && $tokens->depth === 0) {
             $res->append($node->name === 'default' ? ',' : ';');
             $var = TRUE;
         } elseif ($var === NULL && $node->name === 'default' && !$tokens->isCurrent(Latte\MacroTokens::T_WHITESPACE)) {
             throw new CompileException("Unexpected '" . $tokens->currentValue() . "' in {default {$node->args}}");
         } else {
             $res->append($tokens->currentToken());
         }
     }
     $out = $writer->quoteFilter($res)->joinAll();
     return $node->name === 'default' ? "extract(array({$out}), EXTR_SKIP)" : $out;
 }
Ejemplo n.º 2
0
 /**
  * Formats modifiers calling.
  * @param  MacroTokens
  * @param  string
  * @throws CompileException
  * @return MacroTokens
  */
 public function modifiersFilter(MacroTokens $tokens, $var)
 {
     $inside = FALSE;
     $res = new MacroTokens($var);
     while ($tokens->nextToken()) {
         if ($tokens->isCurrent(MacroTokens::T_WHITESPACE)) {
             $res->append(' ');
         } elseif ($inside) {
             if ($tokens->isCurrent(':', ',')) {
                 $res->append(', ');
                 $tokens->nextAll(MacroTokens::T_WHITESPACE);
             } elseif ($tokens->isCurrent('|')) {
                 $res->append(')');
                 $inside = FALSE;
             } else {
                 $res->append($tokens->currentToken());
             }
         } else {
             if ($tokens->isCurrent(MacroTokens::T_SYMBOL)) {
                 if ($this->compiler && $tokens->isCurrent('escape')) {
                     $res = $this->escapeFilter($res);
                     $tokens->nextToken('|');
                 } else {
                     $res->prepend('$template->' . $tokens->currentValue() . '(');
                     $inside = TRUE;
                 }
             } else {
                 throw new CompileException("Modifier name must be alphanumeric string, '" . $tokens->currentValue() . "' given.");
             }
         }
     }
     if ($inside) {
         $res->append(')');
     }
     return $res;
 }