public static function initRuntime(Control $control, Template $template, stdClass $global) { self::$template = $template->getFile(); if (!$control instanceof Presenter && count(self::$parents)) { end(self::$parents)->dependencies[Cache::FILES][] = $template->getFile(); } }
/** * Register in base presenter. * When registered it will provide url for assets belonging to * compiler extension class which has implemented IAssetsProvider. * * @param Template $template * @param string $name Name of the macro used in templates. * @param string $extensionClass Compiler extension class */ public function register(Template $template, $name, $extensionClass) { $latte = $template->getLatte(); $template->_echo511_assetlocator = $this->extensionsData; $set = new MacroSet($latte->getCompiler()); $set->addMacro($name, function ($node, $writer) use($extensionClass) { return $writer->write('echo $_echo511_assetlocator->getFilesystem(\'' . $extensionClass . '\')->getUrl(\'' . trim($node->args, '"\'') . '\');'); }); }
public static function prepareTemplate(Template $template) { $texy = new \Texy(); $latte = $template->getLatte(); $latte->addFilter('texy', function ($text) use($texy) { return Html::el('')->setHtml($texy->process($text)); }); $latte->addFilter('time', function ($text) { return date('j.n.Y G:i:s', $text); }); }
public function register(Template $template) { foreach (['timeAgo', 'duration', 'hours', 'minutes', 'templateLink', 'lcFirst', 'subjectColor', 'subjectIcon', 'highlight', 'vlna'] as $filter) { $template->addFilter($filter, [$this, $filter]); } $cases = ['nominative' => 1, 'genitive' => 2, 'dative' => 3, 'accusative' => 4, 'vocative' => 5, 'locative' => 6, 'instrumental' => 7]; foreach ($cases as $name => $case) { $template->addFilter($name, function ($phrase) use($case) { return $this->inflection->inflect($phrase, $case); }); } }
/** * Gets html content * * @return string|Template */ public function getContent() { if ($this->content instanceof Template) { // if is it template return $this->content->render(); } else { if (file_exists($this->content)) { // if is it file $l = new Engine(); return $l->renderToString($this->content); } } return (string) $this->content; }
public function run() { error_reporting(E_ALL & ~E_NOTICE); if (!$this->skippedFiles) { $this->clearTemp(); } $latte = $this->template->getLatte(); /* @var $file SplFileInfo */ foreach ($this->prepareFiles() as $file) { try { echo $file->getPathname() . "\n"; $code = $latte->compile($file->getPathname()); file_put_contents($latte->getCacheFile($file->getPathname()), $code); } catch (RuntimeException $e) { if (substr($e->getMessage(), 0, 30) !== 'Cannot include undefined block') { throw $e; } } catch (CompileException $e) { $find = NULL; if (!preg_match('/Unknown macro \\{(.*)\\}/U', $e->getMessage(), $find)) { throw $e; } $macroName = $find[1]; $this->template->getLatte()->onCompile[] = function (Engine $engine) use($macroName) { $engine->addMacro($macroName, new EmptyMacro()); //goto checkFile; }; $this->skippedFiles[] = $file; } } if ($this->skippedFiles) { $this->run(); } }
/** * Render the templates * * @param \Nette\Forms\Form $form * @return void */ public function render(\Nette\Forms\Form $form) { if ($form === $this->form) { // don't forget for second run, @hosiplan! ;) foreach ($this->form->getControls() as $control) { $control->setOption('rendered', FALSE); } echo $this->template; return; } // Store the current form instance $this->form = $form; // Translator available? if ($translator = $form->getTranslator()) { // intentional = $this->template->setTranslator($translator); } // Pre-proccess form $errors = $form->getErrors(); foreach ($form->getControls() as $control) { $control->setOption('rendered', FALSE); if (!$control->getOption('blockname', FALSE)) { $ex = explode('\\', get_class($control)); $control->setOption('blockname', end($ex)); } if ($this->errorsAtInputs) { $errors = array_diff($errors, $control->errors); } } // Assign to template $this->template->form = $form; $this->template->errors = $errors; $this->template->renderer = $this; $this->template->errorsAtInputs = $this->errorsAtInputs; // And echo the output echo $this->template; }
/** * register helpers * * @param \Nette\Bridges\ApplicationLatte\Template * @return \Nette\Bridges\ApplicationLatte\Template */ public function register(Template $template) { $this->cache = new Cache($this->cacheStorage, self::CACHE_NAMESPACE); $methods = $this->cache->load('methods', function () { $methods = []; foreach ($this->getReflection()->getMethods() as $method) { if ($filterName = $method->getAnnotation('register')) { $methods[] = [$filterName, $method->name]; } } return $methods; }); foreach ($methods as $method) { $template->addFilter($method[0] === TRUE ? $method[1] : $method[0], $this->{$method[1]}); } if (isset($template->domainDir)) { $this->domainDir = $template->domainDir; } if (isset($template->domainDirUrl)) { $this->domainDirUrl = $template->domainDirUrl; } $this->template = $template; return $template; }
/** * @param Latte\Template|\Nette\Bridges\ApplicationLatte\Template|\Nette\Templating\Template $template * @param ITranslator $translator */ private static function overrideTemplateTranslator($template, ITranslator $translator) { if ($template instanceof Latte\Template) { $template->getEngine()->addFilter('translate', array(new TemplateHelpers($translator), 'translate')); } elseif ($template instanceof \Nette\Bridges\ApplicationLatte\Template) { $template->getLatte()->addFilter('translate', array(new TemplateHelpers($translator), 'translate')); } elseif ($template instanceof \Nette\Templating\Template) { $template->registerHelper('translate', array(new TemplateHelpers($translator), 'translate')); } return $translator; }
/** * Builds final pdf * * @return mPDF * @throws \Exception */ private function build() { if (empty($this->documentTitle)) { throw new \Exception("Var 'documentTitle' cannot be empty."); } if ($this->ignoreStylesInHTMLDocument) { if (!class_exists('Symfony\\Component\\DomCrawler\\Crawler')) { throw new MissingServiceException("Class 'Symfony\\Component\\DomCrawler\\Crawler' not found. Try composer-require 'symfony/dom-crawler'."); } if (!class_exists('Symfony\\Component\\CssSelector\\CssSelector')) { throw new MissingServiceException("Class 'Symfony\\Component\\CssSelector\\CssSelector' not found. Try composer-require 'symfony/css-selector'."); } } if ($this->generatedFile) { // singleton return $this->generatedFile; } if ($this->source instanceof ITemplate || $this->source instanceof Template) { $html = $this->source->__toString(); } else { $html = $this->source; } // Fix: $html can't be empty (mPDF generates Fatal error) if (empty($html)) { $html = '<html><body></body></html>'; } $mpdf = $this->getMPDF(); $mpdf->biDirectional = $this->multiLanguage; $mpdf->SetAuthor($this->documentAuthor); $mpdf->SetTitle($this->documentTitle); $mpdf->SetDisplayMode($this->displayZoom, $this->displayLayout); $mpdf->showImageErrors = true; // Add styles if (!empty($this->styles)) { $mpdf->WriteHTML($this->styles, 1); } // copied from mPDF -> removes comments $html = preg_replace('/<!--mpdf/i', '', $html); $html = preg_replace('/mpdf-->/i', '', $html); $html = preg_replace('/<\\!\\-\\-.*?\\-\\->/s', '', $html); // @see: http://mpdf1.com/manual/index.php?tid=121&searchstring=writeHTML if ($this->ignoreStylesInHTMLDocument) { // deletes all <style> tags $crawler = new Crawler($html); foreach ($crawler->filter('style') as $child) { $child->parentNode->removeChild($child); } $html = $crawler->html(); $mode = 2; // If <body> tags are found, all html outside these tags are discarded, and the rest is parsed as content for the document. If no <body> tags are found, all html is parsed as content. Prior to mPDF 4.2 the default CSS was not parsed when using mode #2 } else { $mode = 0; // Parse all: HTML + CSS } // Add content $mpdf->WriteHTML($html, $mode); $mpdf->page = count($mpdf->pages); //set pointer to last page to force render of all pages $this->onBeforeComplete($mpdf); $this->generatedFile = $mpdf; return $this->generatedFile; }
/** * @param Template $template */ public function templatePrepareFilters($template) { AngularMacro::install($template->getLatte()->getCompiler()); }