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

\r\n after {/if} eaten

 
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 -> Bugs
View previous topic :: View next topic  
Author Message
ilyalyu
Smarty Regular


Joined: 03 Nov 2009
Posts: 72

PostPosted: Wed Jan 25, 2012 7:41 pm    Post subject: \r\n after {/if} eaten Reply with quote

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
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Wed Jan 25, 2012 9:22 pm    Post subject: Reply with quote

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
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Wed Jan 25, 2012 9:36 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
ilyalyu
Smarty Regular


Joined: 03 Nov 2009
Posts: 72

PostPosted: Thu Jan 26, 2012 10:22 am    Post subject: Reply with quote

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
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Thu Jan 26, 2012 2:43 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
ilyalyu
Smarty Regular


Joined: 03 Nov 2009
Posts: 72

PostPosted: Fri Jan 27, 2012 12:46 pm    Post subject: Reply with quote

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
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Fri Jan 27, 2012 1:00 pm    Post subject: Reply with quote

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
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 -> Bugs 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