Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Exception with whitespace in {if }

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Smarty 3
View previous topic :: View next topic  
Author Message
Conisant
Smarty n00b


Joined: 08 Dec 2009
Posts: 3

PostPosted: Tue Dec 08, 2009 1:29 pm    Post subject: Exception with whitespace in {if } Reply with quote

Hey,

I tried Smarty 3.0 Beta 5 today and the first thing I got was an exception about an {if} block.

I found out that it will always throw an exception, if there is a whitespace at the end of the {if} statement. So:

{if true} something {/if}
is totaly fine but

{if true } something {/if}
throws an exception that there is an unexpected " }" Smile

Is that an intended behaviour? Does I have to crawl all my .tpl for ifs with white spaces?

Greetings from Gernamy,
Coni
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Tue Dec 08, 2009 2:27 pm    Post subject: Reply with quote

You can disable $smarty->auto_literal = false. But, then you have to escape javascript and css code like Smarty 2 again. I'd recommend cleaning up whitespace.
Back to top
View user's profile Send private message Visit poster's website
douglassdavis
Smarty Junkie


Joined: 21 Jan 2008
Posts: 541

PostPosted: Tue Dec 08, 2009 4:02 pm    Post subject: Reply with quote

is it saying exception unexpected } or exception unexpected {/if}?

I would think {if true } should be ignored rather than cause an exception, and then the {/if} would cause the exception.
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Tue Dec 08, 2009 4:06 pm    Post subject: Reply with quote

Correct, it ignores braces surrounded by white space. So:

{if true FOO something {/if}

Would throw unexpected FOO. So " }" is also unexpected (brace preceded by a whitespace.)

This is a side effect of auto_literals.
Back to top
View user's profile Send private message Visit poster's website
douglassdavis
Smarty Junkie


Joined: 21 Jan 2008
Posts: 541

PostPosted: Tue Dec 08, 2009 5:29 pm    Post subject: Reply with quote

mohrt wrote:
Correct, it ignores braces surrounded by white space. So:

{if true FOO something {/if}

Would throw unexpected FOO. So " }" is also unexpected (brace preceded by a whitespace.)

This is a side effect of auto_literals.



So, anything that starts with "{something" the compiler will compile as smarty code, no matter how it ends. At first I had the impression that it had to start with "{something" and end with "something}," or else it was totally ignored by the compiler. But, I understand now.

Why isn't " }" allowed though? There's only one interpretation of what that can be, a user trying to end their smarty code. By the time you get to it, you've already determined that the code is meant to be Smarty.

Just asking Smile
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Tue Dec 08, 2009 6:25 pm    Post subject: Reply with quote

Quote:
So, anything that starts with "{something" the compiler will compile as smarty code, no matter how it ends.


Yes that is correct. Just as PHP compiles <?php as an opening php sequence, no matter how it ends (in error or not.) As it stands, the compiler does not recognize [space]} as a valid end delimiter, in order to allow for javascript/css friendliness. The compiler probably *could* allow the space because its in context, but that could lead to confusing parse errors when there really is a typo in the code.
Back to top
View user's profile Send private message Visit poster's website
douglassdavis
Smarty Junkie


Joined: 21 Jan 2008
Posts: 541

PostPosted: Tue Dec 08, 2009 7:56 pm    Post subject: Reply with quote

mohrt wrote:
Quote:
So, anything that starts with "{something" the compiler will compile as smarty code, no matter how it ends.


Yes that is correct. Just as PHP compiles <?php as an opening php sequence, no matter how it ends (in error or not.) As it stands, the compiler does not recognize [space]} as a valid end delimiter, in order to allow for javascript/css friendliness.


Ok, but, if auto_literal is false, you can add spaces if you want, got it.

I guess I never understood how [space]} is making the disambiguation between Javascript/CSS/Smarty any more difficult, or making Smarty less javascript/css friendly.

The way would I think it could be done:

Code:

{blah blah blah}

smarty


Code:

{blah blah blah }

smarty


Code:

{ blah blah blah }

not smarty

I know I often add a space for readability. And I'm just trying to understand what making [space]} an error accomplishes specifically. forgive me for bugging you, I guess I've just got a compulsion to try to understand things. Laughing
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Tue Dec 08, 2009 8:29 pm    Post subject: Reply with quote

It could be done technically, but I think its better to remove the space in the tag rather than allow it both ways. Because Smarty tags share the syntax of JS/CSS, debugging could get confusing when typos are made but no error is shown. Something like:

Code:
myfunc {
  var = {getvar
}


You can see there is a typo, but smarty won't throw an error because it will pick up the JS closing tag as its own. Then the developer thinks Smarty is eating its closing tag. This is just a simple example, but you can see how this could cause confusing debug situations.


Last edited by mohrt on Tue Dec 08, 2009 8:32 pm; edited 1 time in total
Back to top
View user's profile Send private message Visit poster's website
douglassdavis
Smarty Junkie


Joined: 21 Jan 2008
Posts: 541

PostPosted: Tue Dec 08, 2009 8:32 pm    Post subject: Reply with quote

ok. makes sense
Back to top
View user's profile Send private message
Conisant
Smarty n00b


Joined: 08 Dec 2009
Posts: 3

PostPosted: Wed Dec 09, 2009 7:54 am    Post subject: Reply with quote

Okay I totaly agree Smile
I wasn't aware of the fact that it comes from the java script support.

But couldn't the compiler allow one single whitespace? Because in my opinion the one whitespace in the {if}-tag makes it a lot better to read, because you see the expression a little bit better in contrast to the rest of the code.
But it's more important to have nice javascript support and I love that feature Smile
It's rare that there is need of putting scripts (also css) into the code, but there certainly are.

Oh and only to speak it out once: It's a great job you've done! 3.0 is great.

Greets, Coni
Back to top
View user's profile Send private message
douglassdavis
Smarty Junkie


Joined: 21 Jan 2008
Posts: 541

PostPosted: Wed Dec 09, 2009 1:10 pm    Post subject: Reply with quote

Conisant wrote:
Okay I totaly agree Smile
I wasn't aware of the fact that it comes from the java script support.

But couldn't the compiler allow one single whitespace? Because in my opinion the one whitespace in the {if}-tag makes it a lot better to read, because you see the expression a little bit better in contrast to the rest of the code.
But it's more important to have nice javascript support and I love that feature Smile
It's rare that there is need of putting scripts (also css) into the code, but there certainly are.

Oh and only to speak it out once: It's a great job you've done! 3.0 is great.

Greets, Coni


Maybe it could allow non-linebreak whitespace? IMO that would still be easy to debug.

visually this:
Code:
myfunc {
  var = {getvar
}


might be confusing, but this

Code:

{if $x==2}


doesn't look much different than this:


Code:

{if $x==2 }
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Smarty 3 All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP