private static function prepareMacro(MacroNode $node, PhpWriter $writer) { $media = null; $module = null; while ($node->tokenizer->hasNext()) { $words[] = $node->tokenizer->fetchWord(); } foreach ($words as $index => $word) { if (Strings::startsWith($word, "module:")) { $module = self::parseModule($word); unset($words[$index]); } if (Strings::startsWith($word, "media:")) { $media = self::parseMedia($word); unset($words[$index]); } if (Strings::startsWith($word, "alt=")) { unset($words[$index]); } } $param = implode(', ', $words); $result = $writer->write("\$assetsLoader = \$control->getWidget('assetsLoader');"); if (isset($module)) { $result .= $writer->write("\$assetsLoader->setModule('{$module}');"); } else { $result .= $writer->write("\$assetsLoader->setModule(null);"); } $result .= $writer->write("\$assetsLoader->setMedia('{$media}');"); $result .= 'if ($assetsLoader instanceof Nette\\Application\\UI\\IPartiallyRenderable) $assetsLoader->validateControl();'; return array('php' => $result, 'param' => $param); }
/** * {element name[:method] [key]} */ public function macroElement(MacroNode $node, PhpWriter $writer) { $rawName = $node->tokenizer->fetchWord(); if ($rawName === FALSE) { throw new CompileException("Missing element type in {element}"); } $rawName = explode(':', $rawName, 2); $name = $writer->formatWord($rawName[0]); $method = isset($rawName[1]) ? ucfirst($rawName[1]) : ''; $method = Strings::match($method, '#^\\w*$#') ? "render{$method}" : "{\"render{$method}\"}"; $idRaw = $node->tokenizer->fetchWord(); $param = $writer->formatArray(); if (!Strings::contains($node->args, '=>')) { $param = substr($param, 6, -1); // removes array() } if (!$idRaw) { throw new CompileException("Missing element title in {element}"); } if (substr($idRaw, 0, 1) !== '$') { $id = Helpers::encodeName($idRaw); } else { $id = $idRaw; } return '$_ctrl = $_presenter->getComponent(\\CmsModule\\Content\\ElementManager::ELEMENT_PREFIX . ' . '"' . $id . '"' . ' . \'_\' . ' . $name . '); ' . '$_ctrl->setName("' . trim($idRaw, '"\'') . '");' . 'if ($presenter->isPanelOpened() && (!isset($__element) || !$__element)) { echo "<span id=\\"' . \CmsModule\Content\ElementManager::ELEMENT_PREFIX . (substr($id, 0, 1) === '$' ? '{' . $id . '}' : $id) . '_' . $rawName[0] . '\\" style=\\"display: inline-block; min-width: 50px; min-height: 25px;\\" class=\\"venne-element-container\\" data-venne-element-id=\\"' . trim($id, '"\'') . '\\" data-venne-element-name=\\"' . $rawName[0] . '\\" data-venne-element-route=\\"" . $presenter->route->id . "\\" data-venne-element-language=\\"" . $presenter->language->id . "\\" data-venne-element-buttons=\\"" . (str_replace(\'"\', "\'", json_encode($_ctrl->getViews()))) . "\\">"; }' . 'if ($_ctrl instanceof Nette\\Application\\UI\\IRenderable) $_ctrl->validateControl(); ' . "\$_ctrl->{$method}({$param});" . 'if ($presenter->isPanelOpened()) { echo "</span>"; }'; }
/** * n:input */ public function macroAttrInput(MacroNode $node, PhpWriter $writer) { if ($node->htmlNode->attrs) { $reset = array_fill_keys(array_keys($node->htmlNode->attrs), NULL); return $writer->write('echo $_form[%node.word]->getControl()->addAttributes(%var)->attributes()', $reset); } return $writer->write('echo $_form[%node.word]->getControl()->attributes()'); }
/** * <input n:name> or alias n:input */ public function macroAttrName(MacroNode $node, PhpWriter $writer) { if ($node->htmlNode->attrs) { $reset = array_fill_keys(array_keys($node->htmlNode->attrs), NULL); return $writer->write('$_input = (is_object(%node.word) ? %node.word : $_form[%node.word]); echo $_input->getControl()->addAttributes(%var)->attributes()', $reset); } return $writer->write('$_input = (is_object(%node.word) ? %node.word : $_form[%node.word]); echo $_input->getControl()->attributes()'); }
/** * {input ...} */ public function macroInput(MacroNode $node, PhpWriter $writer) { $class = get_class($this); $words = $node->tokenizer->fetchWords(); if (!$words) { throw new Latte\CompileException("Missing name in {{$node->name}}."); } $name = array_shift($words); return $writer->write(($name[0] === '$' ? '$_input = is_object(%0.word) ? %0.word : $_form[%0.word];' : '$_input = $_form[%0.word];') . 'echo ' . $class . '::input($_input->getControl(%1.raw)->addAttributes(%node.array), $_input)', $name, implode(', ', array_map(array($writer, 'formatWord'), $words))); }
public function macroTemplateEnd(MacroNode $node, PhpWriter $writer) { if ($node->content !== NULL) { $variable = $this->templateVariable; $this->templateVariable = NULL; return $writer->write('*/ $_datagrid->setRowVariable(\'' . $variable . '\'); $_datagrid->setCustomRowTemplate(\'' . $writer->formatWord($node->content) . '\');'); } else { $node->openingCode = '<?php ?>'; $variable = $node->tokenizer->fetchWord(); $customTemplate = trim($node->tokenizer->fetchWord(), "'\""); return $writer->write('$_datagrid->setRowVariable(\'' . $variable . '\'); $_datagrid->setCustomRowTemplate(Brabijan\\Datagrid\\Macros\\Helpers::getCustomTemplateFile("' . $customTemplate . '", $_l->templates[%var]));', $this->getCompiler()->getTemplateId()); } }
/** * Table heading controls * @param \Nette\Latte\MacroNode $node * @param \Nette\Latte\PhpWriter $writer * @return string */ public function macroControls(Latte\MacroNode $node, Latte\PhpWriter $writer) { $code = '$_column = is_object(%node.word) ? %node.word : $_table[%node.word]; if ($_column->isSortable()) : ?> <td> <div class="order"> <a href="<?php echo $_control->link(\'this!\', array(\'order\' => $_column->name, \'sort\' => false)); ?>"><span>Ascending</span></a> <a href="<?php echo $_control->link(\'this!\', array(\'order\' => $_column->name, \'sort\' => true)); ?>"><span>Descending</span></a> </div> <?php echo $_form[$_column->getColumn()]->getControl()->addAttributes(%node.array); ?> </td> <?php endif;'; if ($node->isEmpty = substr($node->args, -1) === '/') { $node->setArgs(substr($node->args, 0, -1)); } return $writer->write($code); }
public function filter(\Nette\Latte\MacroNode $node, \Nette\Latte\PhpWriter $writer) { $path = $node->tokenizer->fetchWord(); $params = $writer->formatArray(); $path = $this->moduleHelpers->expandPath($path, 'Resources/public'); if (!$this->debugMode) { $sass = new \SassParser(); $file = new \SplFileInfo($path); $targetFile = $file->getBasename() . '-' . md5($path . filemtime($path)) . '.css'; $targetDir = $this->wwwCacheDir . '/sass'; $target = $targetDir . '/' . $targetFile; $targetUrl = substr($target, strlen($this->wwwDir)); if (!file_exists($targetDir)) { umask(00); mkdir($targetDir, 0777, true); } $css = $sass->toCss($path); file_put_contents($target, $css); return '$control->getPresenter()->getContext()->getService("assets.assetManager")->addStylesheet("' . $targetUrl . '", ' . $params . '); '; } else { return ' $_sass_file = new \\SplFileInfo("' . $path . '"); $_sass_targetFile = $_sass_file->getBasename() . \'-\' . md5(\'' . $path . '\') . \'-\' . md5(\'' . $path . '\' . filemtime("' . $path . '")) . \'.css\'; $_sass_targetDir = \'' . $this->wwwCacheDir . '/sass\'; $_sass_target = $_sass_targetDir . \'/\' . $_sass_targetFile; $_sass_targetUrl = substr($_sass_target, strlen(\'' . $this->wwwDir . '\')); if (!file_exists($_sass_target)) { $_sass = new \\SassParser(); if (!file_exists($_sass_targetDir)) { umask(0000); mkdir($_sass_targetDir, 0777, true); } // Remove old files foreach (\\Nette\\Utils\\Finder::findFiles($_sass_file->getBasename() . \'-\' . md5(\'' . $path . '\') . \'-*\')->from($_sass_targetDir) as $_sass_old) { unlink($_sass_old->getPathname()); } $_sass_css = $_sass->toCss(\'' . $path . '\', $_sass_target); file_put_contents($_sass_target, $_sass_css); } $control->getPresenter()->getContext()->getService("assets.assetManager")->addStylesheet($_sass_targetUrl, ' . $params . '); '; } }
public function macroWidget(MacroNode $node, PhpWriter $writer) { $pair = $node->tokenizer->fetchWord(); if ($pair === FALSE) { throw new CompileException("Missing widget name in {widget}"); } $pair = explode(':', $pair, 2); $name = $writer->formatWord($pair[0]); $method = isset($pair[1]) ? ucfirst($pair[1]) : ''; $method = Strings::match($method, '#^\\w*\\z#') ? "render{$method}" : "{\"render{$method}\"}"; $param = $writer->formatArray(); if (!Strings::contains($node->args, '=>')) { $param = substr($param, 6, -1); // removes array() } return ($name[0] === '$' ? "if (is_object({$name})) \$_ctrl = {$name}; else " : '') . '$_ctrl = $presenter->getComponent("widgets")->getComponent(' . $name . '); ' . 'if ($_ctrl instanceof Nette\\Application\\UI\\IRenderable) $_ctrl->validateControl(); ' . "\$_ctrl->{$method}({$param})"; }
/** * {_'something'} * {_'something: %foo', foo => 'bar'} * {_ foo => 'bar'}something %foo{/_} * {_ n => 55}%n message{_}%n messages{/_} */ public function macroTranslate(MacroNode $node, PhpWriter $writer) { $tokens = $writer->preprocess(); $msgTokens = new MacroTokens(); $argTokens = NULL; $currTokens = $msgTokens; while ($tokens->nextToken()) { if ($tokens->depth === 0) { if ($tokens->isCurrent('=>') && $argTokens === NULL) { $argTokens = $msgTokens; $msgTokens = NULL; } elseif ($tokens->isCurrent(',') && $argTokens === NULL) { $argTokens = new MacroTokens(); $currTokens = $argTokens; continue; } } $currTokens->append($tokens->currentToken()); } if ($node->parentNode && $node->parentNode->name == '_') { if (isset($node->parentNode->data->plural)) { throw new CompileException("Invalid plural form of {_} macro"); } $node->isEmpty = true; $node->parentNode->data->plural = true; return 'ob_start()'; } $msg = $msgTokens !== NULL ? trim($writer->quoteFilter($msgTokens)->joinAll()) : NULL; if ($msg == "") { $msg = NULL; } $args = 'array(' . ($argTokens !== NULL ? trim($writer->quoteFilter($argTokens)->joinAll()) . ', ' : '') . '\'__hint__\' => $template->getFile() . ":' . $this->compiler->getLine() . '")'; if ($node->closing) { if (isset($node->data->plural)) { return $writer->write('echo %modify($template->translate(array_reverse(array(ob_get_clean(), ob_get_clean())), ' . $args . '))'); } else { return $writer->write('echo %modify($template->translate(ob_get_clean(), ' . $args . '))'); } } elseif ($msg !== NULL) { $node->isEmpty = true; return $writer->write('echo %modify($template->translate(' . $msg . ', ' . $args . '))'); } else { return 'ob_start()'; } }
public function filter(\Nette\Latte\MacroNode $node, PhpWriter $writer) { $files = array(); $pos = 0; while ($file = $node->tokenizer->fetchWord()) { if (strpos($file, '=>') !== FALSE) { $node->tokenizer->position = $pos; break; } $files[] = $this->wwwDir . '/' . $this->moduleHelpers->expandResource($file); $pos = $node->tokenizer->position; } if (!count($files)) { throw new CompileException("Missing file name in {js}"); } eval('$args = ' . $writer->formatArray() . ';'); return "\$_control['js']->render('" . join('\', \'', $files) . "', array('config' => " . var_export($args, TRUE) . "));"; }
/** * Generates code. * @return string */ private function compile(MacroNode $node, $def) { $writer = Latte\PhpWriter::using($node, $this->parser->context); if (is_string($def)) { $code = $writer->write($def); } else { $code = callback($def)->invoke($node, $writer); if ($code === FALSE) { return FALSE; } } return "<?php {$code} ?>"; }
/** * {ifLinkExists destination [,] [params]} * n:ifLinkExists="destination [,] [params]" */ public function macroIsPublished(MacroNode $node, PhpWriter $writer) { $c = ' if ($_macroIfLinkExistsRet) { $_isPublishedRet = TRUE; if (isset($_macroIfLinkExistsArgs[\'special\'])) { $_macroIsPublishedPage = $_presenter->entityManager->getRepository(\'CmsModule\\Content\\Entities\\PageEntity\')->findOneBy(array(\'special\' => $_macroIfLinkExistsArgs[\'special\'])); if (!$_macroIsPublishedPage->published || !$_macroIsPublishedPage->mainRoute->published) { $_isPublishedRet = FALSE; } } else if (isset($_macroIfLinkExistsArgs[\'route\']) && (!$_macroIfLinkExistsArgs[\'route\']->published || !$_macroIfLinkExistsArgs[\'route\']->page->published)) { $_isPublishedRet = FALSE; } } else { $_isPublishedRet = FALSE; } '; if ($node->prefix === $node::PREFIX_TAG) { return $writer->write($c . ($node->htmlNode->closing ? 'if (array_pop($_l->ifs)):' : 'if ($_l->ifs[] = ($_isPublishedRet)):')); } return $writer->write($c . 'if ($_isPublishedRet):'); }
/** * Generates code. * @return string */ private function compile(MacroNode $node, $def) { $node->tokenizer->reset(); $writer = Latte\PhpWriter::using($node, $this->compiler); if (is_string($def)) { return $writer->write($def); } else { return Nette\Callback::create($def)->invoke($node, $writer); } }
/** * {= ...} * {? ...} */ public function macroExpr(MacroNode $node, PhpWriter $writer) { return $writer->write(($node->name === '?' ? '' : 'echo ') . '%modify(%node.args)'); }
/** * @param MacroNode $node * @param PhpWriter $writer * @return string */ public function macroAttrLinkConfirm(MacroNode $node, PhpWriter $writer) { return $writer->write('?> onclick="return confirm(\'<?php echo %escape("' . $node->args . '")?>\');" <?php'); }
/** * {inputError ...} */ public function macroInputError(MacroNode $node, PhpWriter $writer) { $name = $node->tokenizer->fetchWord(); if (!$name) { return $writer->write('echo %escape($_input->getError())'); } elseif ($name[0] === '$') { return $writer->write('$_input = is_object(%0.word) ? %0.word : $_form[%0.word]; echo %escape($_input->getError())', $name); } else { return $writer->write('echo %escape($_form[%0.word]->getError())', $name); } }
/** * {status ...} */ public function macroStatus(MacroNode $node, PhpWriter $writer) { return $writer->write((substr($node->args, -1) === '?' ? 'if (!$netteHttpResponse->isSent()) ' : '') . '$netteHttpResponse->setCode(%var)', (int) $node->args); }
/** * {link destination [,] [params]} * {plink destination [,] [params]} * n:href="destination [,] [params]" * n:link */ public function macroLink(MacroNode $node, PhpWriter $writer) { if ($node->args == '') { if ($node->htmlNode) { $node->attrCode = $writer->write(' href="<?php echo $_macroIfLinkExistslink; ?>"'); return; } return $writer->write('echo $_macroIfLinkExistslink'); } return parent::macroLink($node, $writer); }
/** * @param \Nette\Latte\MacroNode $node * @param \Nette\Latte\PhpWriter $writer */ public function macroContainer(MacroNode $node, PhpWriter $writer) { if ($this->validateParent($node) === FALSE) { return FALSE; } return $writer->write('$__form->render($__form[%node.word])'); }
/** * @param \Nette\Latte\MacroNode $node * @param \Nette\Latte\PhpWriter $writer */ public function macroContainer(MacroNode $node, PhpWriter $writer) { return $writer->write('$__form->render($__form[%node.word], %node.array)'); }
/** * n:slink="..." */ public function macroStorageLink(\Nette\Latte\MacroNode $node, \Nette\Latte\PhpWriter $writer) { return $writer->write('echo rtrim(\\MouseOver\\Storage\\Application\\StorageMacros::link(%node.word, %node.array, $_presenter));'); }
public static function macroFile(MacroNode $node, PhpWriter $writer) { return $writer->write('echo $presenter->context->linker->createLink(%node.array);'); }
/** * <form n:name>, <input n:name>, <select n:name>, <textarea n:name> and <label n:name> */ public function macroNameAttr(MacroNode $node, PhpWriter $writer) { $words = $node->tokenizer->fetchWords(); if (!$words) { throw new CompileException("Missing name in n:{$node->name}."); } $name = array_shift($words); $tagName = strtolower($node->htmlNode->name); $node->isEmpty = !in_array($tagName, array('form', 'select', 'textarea')); if ($tagName === 'form') { return $writer->write('Nette\\Latte\\Macros\\FormMacros::renderFormBegin($form = $_form = ' . ($name[0] === '$' ? 'is_object(%0.word) ? %0.word : ' : '') . '$_control[%0.word], %1.var, FALSE)', $name, array_fill_keys(array_keys($node->htmlNode->attrs), NULL)); } else { return $writer->write('$_input = ' . ($name[0] === '$' ? 'is_object(%0.word) ? %0.word : ' : '') . '$_form[%0.word]; echo $_input' . ($tagName === 'label' ? '->getLabel(%1.raw)' : '->getControl(%1.raw)') . ($node->htmlNode->attrs ? '->addAttributes(%2.var)' : '') . '->attributes()', $name, implode(', ', array_map(array($writer, 'formatWord'), $words)), array_fill_keys(array_keys($node->htmlNode->attrs), NULL)); } }
macroContentType(MacroNode$node,PhpWriter$writer){if(Strings::contains($node->args,'xhtml')){$this->getCompiler()->setContentType(Latte\Compiler::CONTENT_XHTML);}elseif(Strings::contains($node->args,'html')){$this->getCompiler()->setContentType(Latte\Compiler::CONTENT_HTML);}elseif(Strings::contains($node->args,'xml')){$this->getCompiler()->setContentType(Latte\Compiler::CONTENT_XML);}elseif(Strings::contains($node->args,'javascript')){$this->getCompiler()->setContentType(Latte\Compiler::CONTENT_JS);}elseif(Strings::contains($node->args,'css')){$this->getCompiler()->setContentType(Latte\Compiler::CONTENT_CSS);}elseif(Strings::contains($node->args,'calendar')){$this->getCompiler()->setContentType(Latte\Compiler::CONTENT_ICAL);}else{$this->getCompiler()->setContentType(Latte\Compiler::CONTENT_TEXT);}if(Strings::contains($node->args,'/')){return$writer->write('$netteHttpResponse->setHeader("Content-Type", %var)',$node->args);}}function macroStatus(MacroNode$node,PhpWriter$writer){return$writer->write((substr($node->args,-1)==='?'?'if (!$netteHttpResponse->isSent()) ':'').'$netteHttpResponse->setCode(%var)',(int)$node->args);}static
/** * New node is found. * @return bool */ public function nodeOpened(Latte\MacroNode $node) { $this->used = TRUE; $node->isEmpty = FALSE; $node->openingCode = Latte\PhpWriter::using($node)->write('<?php if (Nette\\Latte\\Macros\\CacheMacro::createCache($netteCacheStorage, %var, $_g->caches, %node.array?)) { ?>', Nette\Utils\Strings::random()); }
public function iconw(MacroNode $node, PhpWriter $writer) { return $writer->write('$_el = Nette\\Utils\\Html::el("i"); $_el->class = array(%node.word ? "icon-".%node.word : null, "icon-white"); echo $_el;'); }