From 678916854e17eeab3beb920bc0d3139c25fae9ee Mon Sep 17 00:00:00 2001 From: Matteo Date: Fri, 26 Jan 2018 17:59:45 +0100 Subject: [PATCH] Add config to enable Twig strict_variables (#3370) Adds the cms.enableTwigStrictVariables config option to enable strict_variables in Twig for debugging purposes. See https://twig.symfony.com/doc/2.x/api.html#environment-options. Credit to @matteotrubini --- config/cms.php | 15 +++++++++++++++ modules/cms/classes/Controller.php | 3 +++ 2 files changed, 18 insertions(+) diff --git a/config/cms.php b/config/cms.php index 5c35ca4f3..088323b50 100644 --- a/config/cms.php +++ b/config/cms.php @@ -346,5 +346,20 @@ return [ */ 'forceBytecodeInvalidation' => true, + + /* + |-------------------------------------------------------------------------- + | Twig Strict Variables + |-------------------------------------------------------------------------- + | + | If strict_variables is disabled, Twig will silently ignore invalid + | variables (variables and or attributes/methods that do not exist) and + | replace them with a null value. When enabled, Twig throws an exception + | instead. If set to null, it is enabled when debug mode (app.debug) is + | enabled. + | + */ + + 'enableTwigStrictVariables' => false, ]; diff --git a/modules/cms/classes/Controller.php b/modules/cms/classes/Controller.php index dc67dce66..f3bdb2d91 100644 --- a/modules/cms/classes/Controller.php +++ b/modules/cms/classes/Controller.php @@ -488,11 +488,14 @@ class Controller $useCache = !Config::get('cms.twigNoCache'); $isDebugMode = Config::get('app.debug', false); + $strictVariables = Config::get('cms.enableTwigStrictVariables', false); + $strictVariables = is_null($strictVariables) ? $isDebugMode : $strictVariables; $forceBytecode = Config::get('cms.forceBytecodeInvalidation', false); $options = [ 'auto_reload' => true, 'debug' => $isDebugMode, + 'strict_variables' => $strictVariables, ]; if ($useCache) {