diff --git a/app/Config/View.php b/app/Config/View.php index 78cd547e3b8f..340e266b09d7 100644 --- a/app/Config/View.php +++ b/app/Config/View.php @@ -43,6 +43,16 @@ class View extends BaseView */ public $plugins = []; + /** + * Whether to use Parser conditionals (if, else, and elseif). + * + * This setting is for compatibility with templates that works on CodeIgniter3. + * If you have JavaScript code in templates, Parser may raise error + * when there are strings that can be interpreted as conditionals. + * In that case, set this false. + */ + public bool $enableConditionals = true; + /** * View Decorators are class methods that will be run in sequence to * have a chance to alter the generated output just prior to caching diff --git a/system/View/Parser.php b/system/View/Parser.php index 6834c003e500..820030839f69 100644 --- a/system/View/Parser.php +++ b/system/View/Parser.php @@ -59,6 +59,16 @@ class Parser extends View */ protected $dataContexts = []; + /** + * Whether to use Parser conditionals (if, else, and elseif). + * + * This setting is for compatibility with templates that works on CodeIgniter3. + * If you have JavaScript code in templates, Parser may raise error + * when there are strings that can be interpreted as conditionals. + * In that case, set this false. + */ + protected bool $enableConditionals = true; + /** * Constructor * @@ -72,6 +82,8 @@ public function __construct(ViewConfig $config, ?string $viewPath = null, $loade // Ensure user plugins override core plugins. $this->plugins = $config->plugins ?? []; + $this->enableConditionals = $config->enableConditionals ?? $this->enableConditionals; + parent::__construct($config, $viewPath, $loader, $debug, $logger); } @@ -223,8 +235,10 @@ protected function parse(string $template, array $data = [], ?array $options = n $template = $this->parseComments($template); $template = $this->extractNoparse($template); - // Replace any conditional code here so we don't have to parse as much - $template = $this->parseConditionals($template); + if ($this->enableConditionals) { + // Replace any conditional code here so we don't have to parse as much + $template = $this->parseConditionals($template); + } // Handle any plugins before normal data, so that // it can potentially modify any template between its tags. diff --git a/tests/system/View/ParserTest.php b/tests/system/View/ParserTest.php index 3462b0cc2a84..b2076a0791dd 100644 --- a/tests/system/View/ParserTest.php +++ b/tests/system/View/ParserTest.php @@ -938,4 +938,38 @@ public function testRenderFindsOtherView() $expected = '

Hello World

'; $this->assertSame($expected, $this->parser->render('Simpler.html')); } + + /** + * @see https://github.com/codeigniter4/CodeIgniter4/issues/5831 + */ + public function testEnableConditionalsFalse() + { + $this->config->enableConditionals = false; + $this->parser = new Parser($this->config, $this->viewsDir, $this->loader); + + $data = [ + 'message' => 'Warning!', + ]; + $this->parser->setData($data); + + $template = <<<'EOL' + + EOL; + $expected = <<<'EOL' + + EOL; + $this->assertSame($expected, $this->parser->renderString($template)); + } }