/** * 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 . ')'; }
/** * 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 ''; }
/** * Post compile action for {extends ...} tag * @param Template $tpl * @param string $body */ public static function extendBody($tpl, &$body) { if ($tpl->dynamic_extends) { if (!$tpl->ext_stack) { $tpl->ext_stack[] = $tpl->getName(); } foreach ($tpl->ext_stack as &$t) { $stack[] = "'{$t}'"; } $stack[] = $tpl->dynamic_extends; $body = '<?php $tpl->getStorage()->display(array(' . implode(', ', $stack) . '), $var); ?>'; } else { $child = $tpl; while ($child && $child->extends) { $parent = $tpl->extend($child->extends); $child = $parent->extends ? $parent : false; } $tpl->extends = false; } $tpl->extend_body = false; }