Twig_Environment::loadTemplate PHP Method

loadTemplate() public method

Loads a template by name.
public loadTemplate ( string $name ) : Twig_TemplateInterface
$name string The template name
return Twig_TemplateInterface A template instance representing the given template name
    public function loadTemplate($name)
    {
        $cls = $this->getTemplateClass($name);

        if (isset($this->loadedTemplates[$cls])) {
            return $this->loadedTemplates[$cls];
        }

        if (!class_exists($cls, false)) {
            if (false === $cache = $this->getCacheFilename($name)) {
                eval('?>'.$this->compileSource($this->loader->getSource($name), $name));
            } else {
                if (!file_exists($cache) || ($this->isAutoReload() && !$this->loader->isFresh($name, filemtime($cache)))) {
                    $this->writeCacheFile($cache, $this->compileSource($this->loader->getSource($name), $name));
                }

                require_once $cache;
            }
        }

        if (!$this->runtimeInitialized) {
            $this->initRuntime();
        }

        return $this->loadedTemplates[$cls] = new $cls($this);
    }

Usage Example

 /**
  * {@inheritdoc}
  */
 public function create($templateName, array $context = array())
 {
     if (!isset($this->templateConfigs[$templateName])) {
         throw new TemplateNotExistsException($templateName);
     }
     $templateConfig = $this->templateConfigs[$templateName];
     if (isset($context['utm'])) {
         $templateConfig['utm'] = array_merge($templateConfig['utm'], $context['utm']);
     }
     $context = $this->twig->mergeGlobals($context);
     $template = $this->twig->loadTemplate($templateConfig['template']);
     $subject = $template->renderBlock('subject', $context);
     $htmlBody = $template->renderBlock('body_html', $context);
     $textBody = $template->renderBlock('body_text', $context);
     if (empty($subject)) {
         throw new TemplatePartRequiredException('subject', $templateConfig['template']);
     }
     if (empty($htmlBody)) {
         throw new TemplatePartRequiredException('body_html', $templateConfig['template']);
     }
     $htmlBody = $this->cssToInline($htmlBody);
     $htmlBody = $this->addUtmParams($htmlBody, $templateConfig['host'], $templateConfig['utm']);
     if (empty($textBody) && $templateConfig['generate_text_version']) {
         $textBody = $this->htmlToText($htmlBody);
     }
     $message = \Swift_Message::newInstance()->setSubject($subject)->setBody($htmlBody, 'text/html');
     if (!empty($textBody)) {
         $message->addPart($textBody, 'text/plain');
     }
     return $message;
 }
All Usage Examples Of Twig_Environment::loadTemplate