Example #1
0
 /**
  * Import macros from templates
  *
  * @param Tokenizer $tokens
  * @param Template $tpl
  * @throws UnexpectedTokenException
  * @throws InvalidUsageException
  * @return string
  */
 public static function tagImport(Tokenizer $tokens, Template $tpl)
 {
     $import = array();
     if ($tokens->is('[')) {
         $tokens->next();
         while ($tokens->valid()) {
             if ($tokens->is(Tokenizer::MACRO_STRING)) {
                 $import[$tokens->current()] = true;
                 $tokens->next();
             } elseif ($tokens->is(']')) {
                 $tokens->next();
                 break;
             } elseif ($tokens->is(',')) {
                 $tokens->next();
             } else {
                 break;
             }
         }
         if ($tokens->current() != "from") {
             throw new UnexpectedTokenException($tokens);
         }
         $tokens->next();
     }
     $tpl->parsePlainArg($tokens, $name);
     if (!$name) {
         throw new InvalidUsageException("Invalid usage tag {import}");
     }
     if ($tokens->is(T_AS)) {
         $alias = $tokens->next()->get(Tokenizer::MACRO_STRING);
         if ($alias === "macro") {
             $alias = "";
         }
         $tokens->next();
     } else {
         $alias = "";
     }
     $donor = $tpl->getStorage()->getRawTemplate()->load($name, true);
     if ($donor->macros) {
         foreach ($donor->macros as $name => $macro) {
             if ($p = strpos($name, ".")) {
                 $name = substr($name, $p);
             }
             if ($import && !isset($import[$name])) {
                 continue;
             }
             if ($alias) {
                 $name = $alias . '.' . $name;
             }
             $tpl->macros[$name] = $macro;
         }
         $tpl->addDepend($donor);
     }
     return '';
 }
Example #2
0
 /**
  * Accessor {$.fetch(...)}
  * @param Tokenizer $tokens
  * @param Template $tpl
  * @return string
  */
 public static function fetch(Tokenizer $tokens, Template $tpl)
 {
     $tokens->skip('(');
     $name = $tpl->parsePlainArg($tokens, $static);
     if ($static) {
         if (!$tpl->getStorage()->templateExists($static)) {
             throw new \RuntimeException("Template {$static} not found");
         }
     }
     if ($tokens->is(',')) {
         $tokens->skip()->need('[');
         $vars = $tpl->parseArray($tokens) . ' + $var';
     } else {
         $vars = '$var';
     }
     $tokens->skip(')');
     return '$tpl->getStorage()->fetch(' . $name . ', ' . $vars . ')';
 }