Skip to content

Commit

Permalink
Add config to allow disabling deprecated features.
Browse files Browse the repository at this point in the history
  • Loading branch information
ADmad committed Apr 5, 2020
1 parent c5359c8 commit 25eac28
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions src/View/TwigView.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,12 @@ class TwigView extends View
*
* Use ViewBuilder::setOption()/setOptions() in your controller to set these options.
*
* - `environment` - Array of config you would pass into \Twig\Environment to overwrite the default settings,
* - `environment`: Array of config you would pass into \Twig\Environment to overwrite the default settings,
* see: http://twig.sensiolabs.org/doc/api.html#environment-options
* - `markdown` - Config for MarkdownExtension. Array must contain `engine` key which is
* - `markdown`: Config for MarkdownExtension. Array must contain `engine` key which is
* an instance of Twig\Extra\Markdown\MarkdownInterface,
* see https://twig.symfony.com/doc/3.x/filters/markdown_to_html.html
* - `deprecatedFeatures`: Whether to enable deprecated features. Defaults to `true`.
*
* @var array
*/
Expand All @@ -77,6 +78,7 @@ class TwigView extends View
'markdown' => [
'engine' => null,
],
'deprecatedFeatures' => true,
];

/**
Expand Down Expand Up @@ -183,8 +185,10 @@ protected function createEnvironment(): Environment
*/
protected function initializeTokenParser(): void
{
$this->getTwig()->addTokenParser(new TokenParser\CellParser());
$this->getTwig()->addTokenParser(new TokenParser\ElementParser());
if ($this->getConfig('deprecatedFeatures')) {
$this->getTwig()->addTokenParser(new TokenParser\CellParser());
$this->getTwig()->addTokenParser(new TokenParser\ElementParser());
}
}

// phpcs:disable CakePHP.Commenting.FunctionComment.InvalidReturnVoid
Expand Down Expand Up @@ -276,10 +280,16 @@ protected function _render(string $templateFile, array $data = []): string
// Set _view for each render because Twig Environment is shared between views.
$this->getTwig()->addGlobal('_view', $this);

$data = array_merge(
empty($data) ? $this->viewVars : $data,
iterator_to_array($this->helpers()->getIterator())
);
if (empty($data)) {
$data = $this->viewVars;
}

if ($this->getConfig('deprecatedFeatures')) {
$data = array_merge(
$data,
iterator_to_array($this->helpers()->getIterator())
);
}

return $this->getTwig()->load($templateFile)->render($data);
}
Expand Down

0 comments on commit 25eac28

Please sign in to comment.