Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add Config\View::$enableConditionals to disable Parser conditionals #5841

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions app/Config/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 16 additions & 2 deletions system/View/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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);
}

Expand Down Expand Up @@ -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.
Expand Down
34 changes: 34 additions & 0 deletions tests/system/View/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -938,4 +938,38 @@ public function testRenderFindsOtherView()
$expected = '<h1>Hello World</h1>';
$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'
<script type="text/javascript">
var f = function() {
if (true) {
alert('{message}');
}
}
</script>
EOL;
$expected = <<<'EOL'
<script type="text/javascript">
var f = function() {
if (true) {
alert('Warning!');
}
}
</script>
EOL;
$this->assertSame($expected, $this->parser->renderString($template));
}
}