/** * {control name[:method] [params]} */ public function macroControl(MacroNode $node, PhpWriter $writer) { $words = $node->tokenizer->fetchWords(); if (!$words) { throw new CompileException('Missing control name in {control}'); } $name = $writer->formatWord($words[0]); $method = isset($words[1]) ? ucfirst($words[1]) : ''; $method = Strings::match($method, '#^\\w*\\z#') ? "render{$method}" : "{\"render{$method}\"}"; $tokens = $node->tokenizer; $pos = $tokens->position; $param = $writer->formatArray(); $tokens->position = $pos; while ($tokens->nextToken()) { if ($tokens->isCurrent('=>') && !$tokens->depth) { $wrap = TRUE; break; } } if (empty($wrap)) { $param = substr($param, 1, -1); // removes array() or [] } return "/* line {$node->startLine} */ " . ($name[0] === '$' ? "if (is_object({$name})) \$_tmp = {$name}; else " : '') . '$_tmp = $this->global->uiControl->getComponent(' . $name . '); ' . 'if ($_tmp instanceof Nette\\Application\\UI\\IRenderable) $_tmp->redrawControl(NULL, FALSE); ' . ($node->modifiers === '' ? "\$_tmp->{$method}({$param});" : $writer->write("ob_start(function () {}); \$_tmp->{$method}({$param}); echo %modify(ob_get_clean());")); }
/** * @param Nette\Latte\MacroNode $node * @param Nette\Latte\PhpWriter $writer * @return string * @throws Nette\Latte\CompileException */ public function macroAttrImg(MacroNode $node, PhpWriter $writer) { $this->isUsed = TRUE; $arguments = Helpers::prepareMacroArguments($node->args); if ($arguments["name"] === NULL) { throw new Nette\Latte\CompileException("Please provide filename."); } $namespace = $arguments["namespace"]; unset($arguments["namespace"]); $arguments = array_map(function ($value) use($writer) { return $value ? $writer->formatWord($value) : 'NULL'; }, $arguments); $command = '$_imagePipe'; $command .= $namespace !== NULL ? '->setNamespace(' . $writer->formatWord(trim($namespace)) . ')' : ''; $command .= '->request(' . implode(", ", $arguments) . ')'; return $writer->write('?> src="<?php echo %escape(' . $writer->formatWord($command) . ')?>" <?php'); }
/** * {ifLocale cs,en}cs,en only{/ifLocale} * n:ifLocale='cs' */ public function macroLocale(MacroNode $node, PhpWriter $writer) { $this->localeUsed = TRUE; $words = []; $words = $node->args; if (!$words) { throw new CompileException("Missing locale in n:{$node->name}."); } $node = $writer->formatWord($words); return $writer->write('$_localeParam = ' . $node . '; if($_localeParam === NULL || in_array($locale, (is_array($_localeParam) ? $_localeParam : (array) explode(",",$_localeParam)), TRUE)) {'); }
/** * {control name[:method] [params]} */ public function macroControl(MacroNode $node, PhpWriter $writer) { $words = $node->tokenizer->fetchWords(); if (!$words) { throw new CompileException('Missing control name in {control}'); } $name = $writer->formatWord($words[0]); $method = isset($words[1]) ? ucfirst($words[1]) : ''; $method = Strings::match($method, '#^\\w*\\z#') ? "render{$method}" : "{\"render{$method}\"}"; $param = $writer->formatArgs(); if (Strings::contains($node->args, '=>')) { $param = "[{$param}]"; } return ($name[0] === '$' ? "if (is_object({$name})) \$_l->tmp = {$name}; else " : '') . '$_l->tmp = $_control->getComponent(' . $name . '); ' . 'if ($_l->tmp instanceof Nette\\Application\\UI\\IRenderable) $_l->tmp->redrawControl(NULL, FALSE); ' . ($node->modifiers === '' ? "\$_l->tmp->{$method}({$param})" : $writer->write("ob_start(); \$_l->tmp->{$method}({$param}); echo %modify(ob_get_clean())")); }
/** * @param MacroNode $node * @param PhpWriter $writer * @return string * @throws CompileException */ public function wizardStart(MacroNode $node, PhpWriter $writer) { $words = $node->tokenizer->fetchWords(); if (!$words) { throw new CompileException('Missing control name in {wizard}'); } $name = $writer->formatWord($words[0]); $method = isset($words[1]) ? ucfirst($words[1]) : ''; $method = Strings::match($method, '#^\\w*\\z#') ? "render{$method}" : "{\"render{$method}\"}"; $param = $writer->formatArray(); if (!Strings::contains($node->args, '=>')) { $param = substr($param, $param[0] === '[' ? 1 : 6, -1); // removes array() or [] } return ($name[0] === '$' ? "if (is_object({$name})) \$_l->tmp = {$name}; else " : '') . '$_l->tmp = $_control->getComponent(' . $name . '); ' . 'if (!$_l->tmp instanceof WebChemistry\\Forms\\Controls\\IWizard) throw new \\Exception(\'Wizard must be instance of WebChemistry\\Forms\\Controls\\IWizard\');' . '$wizard = new WebChemistry\\Forms\\Controls\\Wizard\\Facade($_l->tmp);'; }
/** * {control name[:method] [params]} */ public function macroControl(MacroNode $node, PhpWriter $writer) { $words = $node->tokenizer->fetchWords(); if (!$words) { throw new CompileException('Missing control name in {control}'); } $name = $writer->formatWord($words[0]); $method = isset($words[1]) ? ucfirst($words[1]) : ''; $method = Strings::match($method, '#^\w*\z#') ? "render$method" : "{\"render$method\"}"; $param = $writer->formatArray(); if (!Strings::contains($node->args, '=>')) { $param = substr($param, $param[0] === '[' ? 1 : 6, -1); // removes array() or [] } return ($name[0] === '$' ? "if (is_object($name)) \$_l->tmp = $name; else " : '') . '$_l->tmp = $_control->getComponent(' . $name . '); ' . 'if ($_l->tmp instanceof Nette\Application\UI\IRenderable) $_l->tmp->redrawControl(NULL, FALSE); ' . ($node->modifiers === '' ? "\$_l->tmp->$method($param)" : $writer->write("ob_start(); \$_l->tmp->$method($param); echo %modify(ob_get_clean())")); }
/** * {block [[#]name]} * {snippet [name [,]] [tag]} * {snippetArea [name]} * {define [#]name} */ public function macroBlock(MacroNode $node, PhpWriter $writer) { $name = $node->tokenizer->fetchWord(); if ($node->name === '#') { trigger_error('Shortcut {#block} is deprecated.', E_USER_DEPRECATED); } elseif ($node->name === 'block' && $name === FALSE) { // anonymous block return $node->modifiers === '' ? '' : 'ob_start(function () {})'; } $node->data->name = $name = ltrim($name, '#'); if ($name == NULL) { if ($node->name === 'define') { throw new CompileException('Missing block name.'); } } elseif (strpos($name, '$') !== FALSE) { // dynamic block/snippet if ($node->name === 'snippet') { for ($parent = $node->parentNode; $parent && !($parent->name === 'snippet' || $parent->name === 'snippetArea'); $parent = $parent->parentNode) { } if (!$parent) { throw new CompileException('Dynamic snippets are allowed only inside static snippet/snippetArea.'); } $parent->data->dynamic = TRUE; $node->data->leave = TRUE; $node->closingCode = "<?php \$_l->dynSnippets[\$_l->dynSnippetId] = ob_get_flush() ?>"; if ($node->prefix) { $node->attrCode = $writer->write("<?php echo ' id=\"' . (\$_l->dynSnippetId = \$_control->getSnippetId({$writer->formatWord($name)})) . '\"' ?>"); return $writer->write('ob_start()'); } $tag = trim($node->tokenizer->fetchWord(), '<>'); $tag = $tag ? $tag : 'div'; $node->closingCode .= "\n</{$tag}>"; return $writer->write("?>\n<{$tag} id=\"<?php echo \$_l->dynSnippetId = \$_control->getSnippetId({$writer->formatWord($name)}) ?>\"><?php ob_start()"); } else { $node->data->leave = TRUE; $fname = $writer->formatWord($name); $node->closingCode = '<?php }} ' . ($node->name === 'define' ? '' : "call_user_func(reset(\$_b->blocks[{$fname}]), \$_b, get_defined_vars())") . ' ?>'; $func = '_lb' . substr(md5($this->getCompiler()->getTemplateId() . $name), 0, 10) . '_' . preg_replace('#[^a-z0-9_]#i', '_', $name); return "\n\n//\n// block {$name}\n//\n" . "if (!function_exists(\$_b->blocks[{$fname}]['{$this->getCompiler()->getTemplateId()}'] = '{$func}')) { " . "function {$func}(\$_b, \$_args) { foreach (\$_args as \$__k => \$__v) \$\$__k = \$__v"; } } // static snippet/snippetArea if ($node->name === 'snippet' || $node->name === 'snippetArea') { if ($node->prefix && isset($node->htmlNode->attrs['id'])) { throw new CompileException('Cannot combine HTML attribute id with n:snippet.'); } $node->data->name = $name = '_' . $name; } if (isset($this->namedBlocks[$name])) { throw new CompileException("Cannot redeclare static {$node->name} '{$name}'"); } $prolog = $this->namedBlocks ? '' : "if (\$_l->extends) { ob_end_clean(); return \$template->renderChildTemplate(\$_l->extends, get_defined_vars()); }\n"; $this->namedBlocks[$name] = TRUE; $include = 'call_user_func(reset($_b->blocks[%var]), $_b, ' . ($node->name === 'snippet' || $node->name === 'snippetArea' ? '$template->getParameters()' : 'get_defined_vars()') . ')'; if ($node->modifiers) { $include = "ob_start(function () {}); {$include}; echo %modify(ob_get_clean())"; } if ($node->name === 'snippet') { if ($node->prefix) { $node->attrCode = $writer->write('<?php echo \' id="\' . $_control->getSnippetId(%var) . \'"\' ?>', (string) substr($name, 1)); return $writer->write($prolog . $include, $name); } $tag = trim($node->tokenizer->fetchWord(), '<>'); $tag = $tag ? $tag : 'div'; return $writer->write("{$prolog} ?>\n<{$tag} id=\"<?php echo \$_control->getSnippetId(%var) ?>\"><?php {$include} ?>\n</{$tag}><?php ", (string) substr($name, 1), $name); } elseif ($node->name === 'define') { return $prolog; } else { // block, snippetArea return $writer->write($prolog . $include, $name); } }
public function macroNg(MacroNode $node, PhpWriter $writer) { $name = $node->tokenizer->fetchWord(); return $writer->write("echo '{{'.{$writer->formatWord($name)}"); }
/** * {/block} * {/snippet} * {/snippetArea} * {/define} */ public function macroBlockEnd(MacroNode $node, PhpWriter $writer) { if (isset($node->data->name)) { // block, snippet, define if ($asInner = $node->name === 'snippet' && $node->prefix === MacroNode::PREFIX_NONE) { $node->content = $node->innerContent; } if (($node->name === 'snippet' || $node->name === 'snippetArea') && strpos($node->data->name, '$') === FALSE) { $type = $node->name === 'snippet' ? SnippetDriver::TYPE_STATIC : SnippetDriver::TYPE_AREA; $node->content = '<?php $this->global->snippetDriver->enter(' . $writer->formatWord(substr($node->data->name, 1)) . ', "' . $type . '"); ?>' . preg_replace('#(?<=\\n)[ \\t]+\\z#', '', $node->content) . '<?php $this->global->snippetDriver->leave(); ?>'; } if (empty($node->data->leave)) { if (preg_match('#\\$|n:#', $node->content)) { $node->content = '<?php ' . (isset($node->data->args) ? 'extract($this->params); ' . $node->data->args : 'extract($_args);') . ' ?>' . $node->content; } $this->namedBlocks[$node->data->name] = $tmp = preg_replace('#^\\n+|(?<=\\n)[ \\t]+\\z#', '', $node->content); $node->content = substr_replace($node->content, $node->openingCode . "\n", strspn($node->content, "\n"), strlen($tmp)); $node->openingCode = '<?php ?>'; } elseif (isset($node->data->func)) { $node->content = rtrim($node->content, " \t"); $this->getCompiler()->addMethod($node->data->func, $this->getCompiler()->expandTokens("extract(\$_args);\n?>{$node->content}<?php"), '$_args'); $node->content = ''; } if ($asInner) { // n:snippet -> n:inner-snippet $node->innerContent = $node->openingCode . $node->content . $node->closingCode; $node->closingCode = $node->openingCode = '<?php ?>'; } return ' '; // consume next new line } elseif ($node->modifiers) { // anonymous block with modifier $node->modifiers .= '|escape'; return $writer->write('$_fi = new LR\\FilterInfo(%var); echo %modifyContent(ob_get_clean());', $node->context[0]); } }