/** * @param Template $template */ public function addTemplate(Template $template) { $this->templates[$template->getTemplateName()] = $template; /** @var Template $parent */ if ($parent = $template->getParent([])) { $this->addTemplate($parent); } }
protected function getTemplates(\Twig_Template $template) { $names = []; $names[] = $template->getTemplateName(); if (false !== ($parent = $template->getParent([]))) { $names = array_merge($names, $this->getTemplates($parent)); } return $names; }
private function getTemplateSource(\Twig_Template $template) { // Twig templates are not always stored in files, and so there is no // API to get the filename from a template name in a generic way. // The logic used here works only for templates stored in app/Resources/views // and referenced via the "filename.html.twig" notation, not via the "::filename.html.twig" // one or stored in bundles. This is enough for the needs of the demo app. $filePath = $this->kernelRootDir . '/Resources/views/' . $template->getTemplateName(); $sourceCode = $template->getSource(); // Temporary workaround for https://github.com/twigphp/Twig/issues/2011 if (null === $sourceCode) { $sourceCode = @file_get_contents($filePath); } return ['file_path' => $filePath, 'starting_line' => 1, 'source_code' => $sourceCode]; }
/** * Gets the code from the template * @param Twig_Template $template * @return HTML to display */ public function getCode($template) { $controllerClass = get_class($this->controller[0]); if (class_exists('CG\\Core\\ClassUtils')) { $controllerClass = ClassUtils::getUserClass($controllerClass); } $methodName = $this->controller[1]; $controllerLink = $this->githubLocator->getMethodClassLink($controllerClass, $methodName); $templateLink = $this->githubLocator->getTemplateLink($template->getTemplateName()); $controller = $this->getControllerCode($controllerClass, $methodName); $template = $this->getTemplateCode($template); // remove the code block $template = str_replace('{% set code = code(_self) %}', '', $template); return <<<EOF <h4><strong>Controller Code - <a href="{$controllerLink}">Github</a></strong></h4> <pre class="prettyprint">{$controller}</pre> <h4><strong>Template Code - <a href="{$templateLink}">Github</a></strong></h4> <pre class="prettyprint">{$template}</pre> EOF; }
/** * Gets the name of the main loaded template. * * @return string */ public function getTemplateName() { return $this->template->getTemplateName(); }
private function getTemplateSource(\Twig_Template $template) { return array('file_path' => $this->kernelRootDir . '/Resources/views/' . $template->getTemplateName(), 'starting_line' => 1, 'source_code' => $template->getSource()); }
/** * Calls macro in a template. * * @param Twig_Template $template The template * @param string $macro The name of macro * @param array $arguments The arguments of macro * @param array $namedNames An array of names of arguments as keys * @param integer $namedCount The count of named arguments * @param integer $positionalCount The count of positional arguments * * @return string The content of a macro * * @throws Twig_Error_Runtime if the macro is not defined * @throws Twig_Error_Runtime if the argument is defined twice * @throws Twig_Error_Runtime if the argument is unknown */ protected function callMacro(Twig_Template $template, $macro, array $arguments, array $namedNames = array(), $namedCount = 0, $positionalCount = -1) { if (!isset($template->macros[$macro]['reflection'])) { if (!isset($template->macros[$macro])) { throw new Twig_Error_Runtime(sprintf('Macro "%s" is not defined in the template "%s".', $macro, $template->getTemplateName())); } $template->macros[$macro]['reflection'] = new ReflectionMethod($template, $template->macros[$macro]['method']); } if ($namedCount < 1) { return $template->macros[$macro]['reflection']->invokeArgs($template, $arguments); } $i = 0; $args = array(); foreach ($template->macros[$macro]['arguments'] as $name => $value) { if (isset($namedNames[$name])) { if ($i < $positionalCount) { throw new Twig_Error_Runtime(sprintf('Argument "%s" is defined twice for macro "%s" defined in the template "%s".', $name, $macro, $template->getTemplateName())); } $args[] = $arguments[$name]; if (--$namedCount < 1) { break; } } elseif ($i < $positionalCount) { $args[] = $arguments[$i]; } else { $args[] = $value; } $i++; } if ($namedCount > 0) { $parameters = array_keys(array_diff_key($namedNames, $template->macros[$macro]['arguments'])); throw new Twig_Error_Runtime(sprintf('Unknown argument%s "%s" for macro "%s" defined in the template "%s".', count($parameters) > 1 ? 's' : '', implode('", "', $parameters), $macro, $template->getTemplateName())); } return $template->macros[$macro]['reflection']->invokeArgs($template, $args); }
/** * @param FieldDescriptionInterface $fieldDescription * @param \Twig_Template $template * @param array $parameters * * @return string */ public function output(FieldDescriptionInterface $fieldDescription, \Twig_Template $template, array $parameters, \Twig_Environment $environment) { $content = $template->render($parameters); if ($environment->isDebug()) { $commentTemplate = <<<EOT <!-- START fieldName: %s template: %s compiled template: %s --> %s <!-- END - fieldName: %s --> EOT; return sprintf($commentTemplate, $fieldDescription->getFieldName(), $fieldDescription->getTemplate(), $template->getTemplateName(), $content, $fieldDescription->getFieldName()); } return $content; }
/** * @param FieldDescriptionInterface $fieldDescription * @param \Twig_Template $template * @param array $parameters * * @return string */ public function output(FieldDescriptionInterface $fieldDescription, \Twig_Template $template, array $parameters = array()) { $content = $template->render($parameters); if ($this->environment->isDebug()) { return sprintf("\n<!-- START \n fieldName: %s\n template: %s\n compiled template: %s\n -->\n%s\n<!-- END - fieldName: %s -->", $fieldDescription->getFieldName(), $fieldDescription->getTemplate(), $template->getTemplateName(), $content, $fieldDescription->getFieldName()); } return $content; }
protected function enrichMetdata(\Twig_Template $template) { $this->context['metadata']['uri'] = $template->getTemplateName(); $this->context['metadata']['lastmod'] = new \DateTime(sprintf('@%d', filemtime($this->app['twig']->getCacheFilename($template->getTemplateName())))); }
/** * @param \Twig_Template $scope * @param string $name * * @return mixed */ public function getUiPageSelf(\Twig_Template $scope, $name) { $file = $scope->getTemplateName(); $pattern = '/ui\\:(.*):(.*)\\.html\\.twig/'; return preg_replace($pattern, sprintf('ui:$1:%s.html.twig', $name), $file); }