CodeIgniter Forums
Parser function (parseConditionals) error - JS in HTML with condition IF - Printable Version

+- CodeIgniter Forums (
+-- Forum: CodeIgniter 4 (
+--- Forum: CodeIgniter 4 Support (
+--- Thread: Parser function (parseConditionals) error - JS in HTML with condition IF (/thread-76597.html)

Parser function (parseConditionals) error - JS in HTML with condition IF - MarcinKabiesz - 05-30-2020

I think I found a view parser problem. The problem occurs according to the error code when substituting the phrase "{if" in the HTML code that contains the JS code. There is no '{if' tag used to be replaced by the parser, but it tries to do it.

My JS code in HTML:
                success: function (data) {
                  if (data.code == true) {
                    clock_time = clock_time_session;

As you can see, this is a piece of AJAX code written before the end of BODY in HTML.

The parser lets go when I do this trick:
                success: function (data) {
// -> Added Comment (fix), finaly pattern not matched
                  if (data.code == true) {
                    clock_time = clock_time_session;

After analyzing the regex that is to replace the {if} {elseif} {else} etc ... it looks like a syntax error in the saved regex.

File system/View/Parser.php:
Pattern line 524/868 in the parseConditionals function:
$pattern = '/\{\s*(if|elseif)\s*((?:\()?(.*?)(?:\))?)\s*\}/ms';

My Fix:
$pattern = '/\{(if|elseif)\s*((?:\()?(.*?)(?:\))?)\s*\}/ms';

CUT: "\s*" -> ""

It seems to me that the use of spaces between "{" and "if" should not be allowed, which also translates into a parser crash through your own JS code in BODY HTML.

Please check the Framework again for using the view parser.

I'm using the latest stable version 4.0.3.

A little note.

I noticed that you also allow {   else  } or {  endif} tags. Since you write in the documentation specifically {else} or {endif} instead of what I gave above, I think there is no point in allowing additional whitespace or new lines - no sense ...

I didn't notice that after my change, of course, the debugger toolbar stopped working. In the documentation, examples show that there is no space between the parenthesis and if, and yet they themselves had to use it with the debugger toolbar. ehh ...

My Fix #2:
$pattern = '/\{\s*(if|elseif)\s*((?:\()?(.*?)(?:\))?)\s*\}/m';

CUT: "/ms" -> "/m"

The debugger strip and JS code work then.