/** * Tag {cycle} * * @param Tokenizer $tokens * @param Template $tpl * @return string * @throws InvalidUsageException */ public static function tagCycle(Tokenizer $tokens, Template $tpl) { if ($tokens->is("[")) { $exp = $tpl->parseArray($tokens); } else { $exp = $tpl->parseExpr($tokens); } if ($tokens->valid()) { $p = $tpl->parseParams($tokens); if (empty($p["index"])) { throw new InvalidUsageException("Cycle may contain only index attribute"); } else { return 'echo ' . __CLASS__ . '::cycle(' . $exp . ', ' . $p["index"] . ')'; } } else { $var = $tpl->tmpVar(); return 'echo ' . __CLASS__ . '::cycle(' . $exp . ", isset({$var}) ? ++{$var} : ({$var} = 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 . ')'; }