View previous topic :: View next topic |
Author |
Message |
ilyalyu Smarty Regular
Joined: 03 Nov 2009 Posts: 72
|
Posted: Wed Jan 25, 2012 7:41 pm Post subject: \r\n after {/if} eaten |
|
|
Code: |
$smarty = new Smarty;
echo $smarty->fetch("string:{if true}aaa{/if}\r\n{if true}bbb{/if}\r\n{if true}ccc{/if}");
|
outputs "aaabbbccc" instead of "aaa bbb ccc" |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Wed Jan 25, 2012 9:22 pm Post subject: |
|
|
PHP does eat a signle lineend after it's ?> closing tag.
There is no general solution for this as in other cases this behaviour is wanted.
Just modify your code to something like this
Code: | echo $smarty->fetch("string:{if true}aaa{/if}\r\n {if true}bbb{/if}\r\n {if true}ccc{/if}"); |
|
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Wed Jan 25, 2012 9:36 pm Post subject: |
|
|
what tews said, however if you put a real space character in the string, what is the point of using /r/n? just eliminate it:
Code: | echo $smarty->fetch("string:{if true}aaa{/if} {if true}bbb{/if} {if true}ccc{/if}"); |
Or if you really like \r\n, double it up:
Code: | echo $smarty->fetch("string:{if true}aaa{/if}\r\n\r\n{if true}bbb{/if}\r\n\r\n{if true}ccc{/if}"); |
\r\n isn't really ideal for use in templates, so when you quote template source in-line with PHP code and you want to use them, you just have to compensate a bit. |
|
Back to top |
|
ilyalyu Smarty Regular
Joined: 03 Nov 2009 Posts: 72
|
Posted: Thu Jan 26, 2012 10:22 am Post subject: |
|
|
Quote: | PHP does eat a signle lineend after it's ?> closing tag. |
In PHP this is official feature. It is described in their official guide.
http://www.php.net/manual/en/language.basic-syntax.phpmode.php: Quote: | When PHP hits the ?> closing tags, it simply starts outputting whatever it finds (except for an immediately following newline - see instruction separation ) until it hits another opening tag | http://www.php.net/manual/en/language.basic-syntax.instruction-separation.php: Quote: | The closing tag for the block will include the immediately trailing newline if one is present |
Is it official feature for Smarty to eat immediately trailing newline after closing curly bracket? If it is so, then here are two examples when Smarty does NOT eat immediately trailing newline: Code: | echo $smarty->fetch('string:{$x = 1}
{$x}
{$x}'); |
Code: | echo $smarty->fetch('string:
{cycle values="odd,even"}
{cycle values="odd,even"}'); |
I suggest that the treatment of the newline after curly bracket should be an official feature described in official Smarty guide. This way developers can be sure that it works in the same way for all smarty constructs and that it does not voluntarily change from version to version. Currently, I see that in some cases Smarty eats trailing newline and in some cases it does not. |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Thu Jan 26, 2012 2:43 pm Post subject: |
|
|
This topic comes up quite often. The problem is, for templates newline eating is situational. What it boils down to... if the tag has output, don't eat the newline. If it doesn't, eat it. So:
Code: | This is a {$foo}
test. |
Here you would expect the newline after $foo to stay, however:
Code: | {if $foo}
bar
{/if} |
Here you would not expect the IF tags to create newlines in the template output.
The output rule not always a constant, sometimes it is contextual:
Code: | This is a {$x=1}
test. |
Now you WANT the newline after the tag to stay, even without output from the tag.
So the tricky part is figuring out at compile time, should we eat the newline. This can tough to determine, as this is situational at best. We have tried to address this the best we could, but we know it is not perfect, nor can every situation be handled perfectly. We try to cover the common situations, but sometimes you have to add/remove a newline in the tpl to get the desired output. |
|
Back to top |
|
ilyalyu Smarty Regular
Joined: 03 Nov 2009 Posts: 72
|
Posted: Fri Jan 27, 2012 12:46 pm Post subject: |
|
|
In my view the problem is that newline treatment is an undocumented feature. This means that it may change from version to version and that the same template may produce different output depending on Smarty version. In my view, it is unsafe to rely on undocumented features. |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Fri Jan 27, 2012 1:00 pm Post subject: |
|
|
This topic is not new and the behaviour did not change between Smarty versions.
A Smarty2 template of
Code: | {if true}aaa{/if}
{if true}bbb{/if}
{if true}ccc{/if} |
does also output aaabbbccc. |
|
Back to top |
|
|