コード例 #1
0
ファイル: Twig.php プロジェクト: frnks/meister
 public function render($v, $data)
 {
     $view = str_replace('{module}', $this->app['Module'], $v);
     if (!$this->twig->resolveTemplate($view)) {
         $view = str_replace('@{module}/', '', $v);
     }
     $data = array_merge($data, ["logged" => $this->app['auth']->isLogged(), "User" => $this->app['auth']->getUser(), "module" => $this->app['Module']]);
     foreach ($this->app->keys() as $key) {
         $data[$key] = $this->app[$key];
     }
     return $this->twig->render($view, $data);
 }
コード例 #2
0
 /**
  * Loads the template.
  *
  * @param string $template The template relative path
  * @param array  $vars     The template variables
  *
  * @throws TemplateNotFoundException    When template does not exist
  * @throws UnsupportedTemplateException When template format is not supported
  *
  * @return TemplateInterface
  */
 public function loadTemplate($template, array $vars = [])
 {
     if (!$this->supports($template)) {
         throw new UnsupportedTemplateException(sprintf('Template %s is not supported by this engine.', $template));
     }
     try {
         $reference = $this->twig->resolveTemplate($template);
     } catch (\Twig_Error_Loader $exception) {
         throw new TemplateNotFoundException(sprintf('Template %s does not exist.', $template), $exception);
     } catch (\Exception $exception) {
         throw new UnsupportedTemplateException(sprintf('Invalid template %s provided.', $template), $exception);
     }
     return new Template($reference->getTemplateName(), $this->twig->mergeGlobals($vars));
 }
コード例 #3
0
ファイル: TwigResponse.php プロジェクト: gmo/common
 public function render(Environment $twig)
 {
     $templates = ArrayCollection::create($this->template)->map(function ($template) {
         return Str::endsWith($template, '.twig') ? $template : $template . '.twig';
     });
     $this->setContent($twig->resolveTemplate($templates->toArray())->render($this->variables->toArray()));
     $this->rendered = true;
 }
コード例 #4
0
ファイル: ElfChatExtension.php プロジェクト: elfchat/elfchat
 public function view(\Twig_Environment $env, $path, $variables = array())
 {
     $name = preg_replace("/\\.[^.\\s]{3,4}\$/", "", $path);
     // Remove ext.
     $name = str_replace('/', '_', $name);
     $html = $env->resolveTemplate($path)->render($variables);
     $html = "<script type=\"text/html\" id=\"view_{$name}\">{$html}</script>";
     return $html;
 }
コード例 #5
0
 /**
  * Add a new namespace to the loader.
  *
  * @param  \TwigBridge\StringView\StringView  $view
  * @return \TwigBridge\Twig\Template
  */
 public function resolveTemplate(StringView $view)
 {
     $currentLoader = $this->twig->getLoader();
     $loader = new Loader($view);
     $this->twig->setLoader($loader);
     $template = $this->twig->resolveTemplate($view->getName());
     $this->twig->setLoader($currentLoader);
     return $template;
 }
コード例 #6
0
 private function render(\Twig_Environment $env, $context, TwigInjectEvent $event)
 {
     $content = '';
     if (sizeof($injections = $event->getInjections())) {
         foreach ($injections as $item) {
             if ($item instanceof TwigInjectInclude) {
                 $content .= $env->resolveTemplate($item->getTemplate())->render(array_merge($context, $item->getParameters()));
                 continue;
             }
             if ($item instanceof TwigInjectRender) {
                 $content .= $this->fragment->render(new ControllerReference($item->getController(), $item->getAttributes(), $item->getQuery()), $item->getStrategy());
                 continue;
             }
         }
     }
     return $content;
 }
コード例 #7
0
 /**
  * @param \Twig_Environment $env
  * @param $context
  * @param TwigTemplateEvent $event
  *
  * @return string
  */
 protected function render(\Twig_Environment $env, $context, TwigTemplateEvent $event)
 {
     $codes = $event->getCodes();
     $compiled = '';
     if (count($codes)) {
         foreach ($codes as $code) {
             if ($code instanceof TwigEventInclude) {
                 $compiled .= $env->resolveTemplate($code->getTemplate())->render(array_replace_recursive($context, $code->getParameters()));
                 continue;
             }
             if ($code instanceof TwigEventString) {
                 $compiled .= $env->render($code->getTemplateString(), array_replace_recursive($context, $code->getParameters()));
                 continue;
             }
             if ($code instanceof TwigEventRender) {
                 $reference = new ControllerReference($code->getController(), $code->getAttributes(), $code->getQuery());
                 $compiled .= $this->fragment->render($reference, $code->getStrategy());
                 continue;
             }
         }
     }
     return $compiled;
 }
コード例 #8
0
ファイル: Core.php プロジェクト: stler/NMFrame
/**
 * Renders a template.
 *
 * @param Twig_Environment $env
 * @param array            $context
 * @param string|array     $template      The template to render or an array of templates to try consecutively
 * @param array            $variables     The variables to pass to the template
 * @param bool             $withContext
 * @param bool             $ignoreMissing Whether to ignore missing templates or not
 * @param bool             $sandboxed     Whether to sandbox the template or not
 *
 * @return string The rendered template
 */
function twig_include(Twig_Environment $env, $context, $template, $variables = array(), $withContext = true, $ignoreMissing = false, $sandboxed = false)
{
    $alreadySandboxed = false;
    $sandbox = null;
    if ($withContext) {
        $variables = array_merge($context, $variables);
    }
    if ($isSandboxed = $sandboxed && $env->hasExtension('sandbox')) {
        $sandbox = $env->getExtension('sandbox');
        if (!($alreadySandboxed = $sandbox->isSandboxed())) {
            $sandbox->enableSandbox();
        }
    }
    try {
        return $env->resolveTemplate($template)->render($variables);
    } catch (Twig_Error_Loader $e) {
        if (!$ignoreMissing) {
            throw $e;
        }
    }
    if ($isSandboxed && !$alreadySandboxed) {
        $sandbox->disableSandbox();
    }
}
コード例 #9
0
 public function showSourceCode(\Twig_Environment $twig, $template)
 {
     return $twig->render('@CodeExplorer/source_code.html.twig', ['controller' => $this->getController(), 'template' => $this->getTemplateSource($twig->resolveTemplate($template))]);
 }