View previous topic :: View next topic |
Author |
Message |
Conisant Smarty n00b
Joined: 08 Dec 2009 Posts: 3
|
Posted: Tue Dec 08, 2009 1:29 pm Post subject: Exception with whitespace in {if } |
|
|
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 " }"
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 |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Tue Dec 08, 2009 2:27 pm Post subject: |
|
|
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 |
|
douglassdavis Smarty Junkie
Joined: 21 Jan 2008 Posts: 541
|
Posted: Tue Dec 08, 2009 4:02 pm Post subject: |
|
|
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 |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Tue Dec 08, 2009 4:06 pm Post subject: |
|
|
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 |
|
douglassdavis Smarty Junkie
Joined: 21 Jan 2008 Posts: 541
|
Posted: Tue Dec 08, 2009 5:29 pm Post subject: |
|
|
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 |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Tue Dec 08, 2009 6:25 pm Post subject: |
|
|
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 |
|
douglassdavis Smarty Junkie
Joined: 21 Jan 2008 Posts: 541
|
Posted: Tue Dec 08, 2009 7:56 pm Post subject: |
|
|
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:
smarty
smarty
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. |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Tue Dec 08, 2009 8:29 pm Post subject: |
|
|
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 |
|
douglassdavis Smarty Junkie
Joined: 21 Jan 2008 Posts: 541
|
Posted: Tue Dec 08, 2009 8:32 pm Post subject: |
|
|
ok. makes sense |
|
Back to top |
|
Conisant Smarty n00b
Joined: 08 Dec 2009 Posts: 3
|
Posted: Wed Dec 09, 2009 7:54 am Post subject: |
|
|
Okay I totaly agree
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
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 |
|
douglassdavis Smarty Junkie
Joined: 21 Jan 2008 Posts: 541
|
Posted: Wed Dec 09, 2009 1:10 pm Post subject: |
|
|
Conisant wrote: | Okay I totaly agree
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
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
doesn't look much different than this:
|
|
Back to top |
|
|