Skip to content

Commit

Permalink
Fixed renderObjectTemplate() and removed $loaderClass & $options argu…
Browse files Browse the repository at this point in the history
…ments from View::getTwig()
  • Loading branch information
brandonkelly committed Jan 6, 2017
1 parent 84bbe8c commit 28d3995
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 45 deletions.
1 change: 1 addition & 0 deletions changelog-v3.md
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ Craft CMS 3.0 Working Changelog
- `craft\base\Element::onAfterMoveElementInStructure()` is no longer static, no longer has an `$element` argument, and has been renamed to `afterMoveInStructure()`.
- `craft\services\AssetIndexer::getIndexEntry()` now returns `null` if the index doesn’t exist, instead of `false`.
- `craft\services\Updates::getUnwritableFolders()` now returns folder paths without trailing slashes.
- `craft\web\View::getTwig()` no longer has `$loaderClass` or `$options` arguments.
- Renamed `craft.getAssets()` back to `craft.assets()`.
- Renamed `craft.getCategories()` back to `craft.categories()`.
- Renamed `craft.getEntries()` back to `craft.entries()`.
Expand Down
72 changes: 27 additions & 45 deletions src/web/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
/**
* @inheritdoc
*
* @property Environment $twig the Twig environment
*
* @author Pixel & Tonic, Inc. <[email protected]>
* @since 3.0
*/
Expand Down Expand Up @@ -56,9 +58,9 @@ class View extends \yii\web\View
private static $_elementThumbSizes = [30, 60, 100, 200];

/**
* @var
* @var Environment
*/
private $_twigs;
private $_twig;

/**
* @var
Expand Down Expand Up @@ -153,51 +155,31 @@ public function init()
}

/**
* Returns the Twig Environment instance for a given template loader class.
* Returns the Twig environment.
*
* @param string|null $loaderClass The name of the class that should be initialized as the Twig instance’s template
* loader. If no class is passed in, [[TemplateLoader]] will be used.
* @param array $options Options to instantiate Twig with
*
* @return Environment The Twig Environment instance.
* @return Environment
*/
public function getTwig($loaderClass = null, array $options = [])
public function getTwig()
{
if ($loaderClass === null) {
$loaderClass = TemplateLoader::class;
if ($this->_twig !== null) {
return $this->_twig;
}

$cacheKey = $loaderClass.':'.md5(serialize($options));

if (!isset($this->_twigs[$cacheKey])) {
/** @var $loader TemplateLoader */
if ($loaderClass === TemplateLoader::class) {
$loader = new $loaderClass($this);
} else {
$loader = new $loaderClass();
}

$options = array_merge($this->_getTwigOptions(), $options);
$this->_twig = new Environment(new TemplateLoader($this), $this->_getTwigOptions());

$twig = new Environment($loader, $options);
$this->_twig->addExtension(new \Twig_Extension_StringLoader());
$this->_twig->addExtension(new Extension($this, $this->_twig));

$twig->addExtension(new \Twig_Extension_StringLoader());
$twig->addExtension(new Extension($this, $twig));

if (Craft::$app->getConfig()->get('devMode')) {
$twig->addExtension(new \Twig_Extension_Debug());
}

// Set our timezone
/** @var \Twig_Extension_Core $core */
$core = $twig->getExtension(\Twig_Extension_Core::class);
$timezone = Craft::$app->getTimeZone();
$core->setTimezone($timezone);

$this->_twigs[$cacheKey] = $twig;
if (Craft::$app->getConfig()->get('devMode')) {
$this->_twig->addExtension(new \Twig_Extension_Debug());
}

return $this->_twigs[$cacheKey];
// Set our timezone
/** @var \Twig_Extension_Core $core */
$core = $this->_twig->getExtension(\Twig_Extension_Core::class);
$core->setTimezone(Craft::$app->getTimeZone());

return $this->_twig;
}

/**
Expand Down Expand Up @@ -348,15 +330,15 @@ public function renderObjectTemplate($template, $object)
return $template;
}

// Get a Twig instance with the String template loader
$twig = $this->getTwig('Twig_Loader_String');
$twig = $this->getTwig();

// Have we already parsed this template?
if (!isset($this->_objectTemplates[$template])) {
// Is this the first time we've parsed this template?
$cacheKey = md5($template);
if (!isset($this->_objectTemplates[$cacheKey])) {
// Replace shortcut "{var}"s with "{{object.var}}"s, without affecting normal Twig tags
$formattedTemplate = preg_replace('/(?<![\{\%])\{(?![\{\%])/', '{{object.', $template);
$formattedTemplate = preg_replace('/(?<![\}\%])\}(?![\}\%])/', '|raw}}', $formattedTemplate);
$this->_objectTemplates[$template] = $twig->loadTemplate($formattedTemplate);
$template = preg_replace('/(?<![\{\%])\{(?![\{\%])/', '{{object.', $template);
$template = preg_replace('/(?<![\}\%])\}(?![\}\%])/', '|raw}}', $template);
$this->_objectTemplates[$cacheKey] = $twig->createTemplate($template);
}

// Temporarily disable strict variables if it's enabled
Expand Down

0 comments on commit 28d3995

Please sign in to comment.