-
Notifications
You must be signed in to change notification settings - Fork 641
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed renderObjectTemplate() and removed $loaderClass & $options argu…
…ments from View::getTwig()
- Loading branch information
1 parent
84bbe8c
commit 28d3995
Showing
2 changed files
with
28 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,8 @@ | |
/** | ||
* @inheritdoc | ||
* | ||
* @property Environment $twig the Twig environment | ||
* | ||
* @author Pixel & Tonic, Inc. <[email protected]> | ||
* @since 3.0 | ||
*/ | ||
|
@@ -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 | ||
|
@@ -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; | ||
} | ||
|
||
/** | ||
|
@@ -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 | ||
|