mojolicious / mojo

:sparkles: Mojolicious - Perl real-time web framework
https://mojolicious.org
Artistic License 2.0
2.66k stars 576 forks source link

surprising syntax error due to control flow syntax in template #1933

Open bkerin opened 2 years ago

bkerin commented 2 years ago

Steps to reproduce the behavior

It's probably known and unavoidable but this syntax in a template:

      % if ( $error ) {

        <%= $error %>

      % }

      % else {

      }

Causes a syntax error:

syntax error at template sign_in.html.ep line 27, near "; else" syntax error at template sign_in.html.ep line 82, near "} $_O " Global symbol "$_O" requires explicit package name (did you forget to declare "my $_O"?) at template sign_in.html.ep line 82. 22
23
<%= $error %> 24
25
% } 26
27
% else { 28

Just removing the blank line between % } and % else { fixes it:

      % }
      % else {

Some note about this behavior in https://docs.mojolicious.org/Mojolicious/Guides/Rendering or something might be good

Expected behavior

works like perl

Actual behavior

EXPLAIN WHAT HAPPENED INSTEAD HERE

daleif commented 2 years ago

Interesting error. BTW might want to use % } at the end in the example as that is also an error, but not the interesting one here.

All the examples use % } else { so might never been tested, or might not he recommended syntax.

guest20 commented 2 years ago

@bkerin. Your example has a blank line between % } and else { and that blank line turns into an output "a new line" in the transpiled(?) template.

If you stick a % at the front of that blank line, or omit it it'll work fine:


% if (...) { 
yes.
% }
% else {
nope.
% }

~~
A non-cuddled-else-enjoyer 
kraih commented 2 years ago

Indeed, a blank line in between code lines won't work. This is expected behaviour.