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

White space in Smarty code

 
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
drak
Smarty Rookie


Joined: 25 Nov 2009
Posts: 11

PostPosted: Wed Nov 25, 2009 6:56 am    Post subject: White space in Smarty code Reply with quote

I have noticed a LOT of whitespace in the Smarty PHP files

Code:

?>\n
\n


It's generally better to just omit the ending ?> from PHP files to avoid this problem - the issue is that PHP interprets the code inside the php delimiters and passes whatever is not to the output buffer which can cause all manner of problems when it's not expected.

For this reason, I would like to request you remove the trailing ?> from all the Smarty PHP files.

Drak
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Thu Nov 26, 2009 1:33 am    Post subject: Reply with quote

where are you seeing white space? particular file examples?
Back to top
View user's profile Send private message Visit poster's website
drak
Smarty Rookie


Joined: 25 Nov 2009
Posts: 11

PostPosted: Thu Nov 26, 2009 11:28 am    Post subject: Reply with quote

Every single. Just remove the trailing ?> it's really not required and saves white trailing space from creeping again.

Drak
Back to top
View user's profile Send private message
SLX
Smarty Rookie


Joined: 19 Nov 2009
Posts: 11
Location: Wroclaw, Poland

PostPosted: Thu Nov 26, 2009 12:41 pm    Post subject: Reply with quote

Yes, I also noticed that. In every file from /libs directory on the end there are one new line character 0x0A. Its not big problem if output buffering is enabled, but in some environments it may give the error
Quote:
Warning: Cannot modify header information - headers already sent

and it may add unnecessary whitespaces to the output.
Back to top
View user's profile Send private message
sadness203
Smarty Rookie


Joined: 12 Nov 2009
Posts: 11

PostPosted: Thu Nov 26, 2009 1:05 pm    Post subject: Reply with quote

see http://www.smarty.net/forums/viewtopic.php?t=16316 for this exact problem
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Thu Nov 26, 2009 1:57 pm    Post subject: Reply with quote

Is it the FTP upload that is adding the newline? There should not be a newline on any of them. Even if there was ONE newline, the ?> of PHP should eat it. Do you have this same issue with Smarty 2?
Back to top
View user's profile Send private message Visit poster's website
drak
Smarty Rookie


Joined: 25 Nov 2009
Posts: 11

PostPosted: Thu Nov 26, 2009 2:01 pm    Post subject: Reply with quote

I am using a direct SVN checkout.

The point is you simply do not need to have a ?> in a pure PHP file at all. PHP just reads code until the end of the file. You only need to have ?> when you are mixing HTML and PHP code together. If you look around, most modern projects so not have ?> in any of their PHP code. Just remove it and save a whole headache. Try it, you will be surprised Wink

Drak
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Fri Nov 27, 2009 4:05 am    Post subject: Reply with quote

The point is, we've never had this issue with Smarty 2, why would we see it in Smarty 3? I don't see this at all, so I'm puzzled about it.

I like to fix the problem, not the symptom. removing ?> does not address the question why an extra newline is showing up. This is not in the distribution. Are you using a distro, or SVN checkout?
Back to top
View user's profile Send private message Visit poster's website
drak
Smarty Rookie


Joined: 25 Nov 2009
Posts: 11

PostPosted: Fri Nov 27, 2009 6:40 am    Post subject: Reply with quote

In any case, just look at your source code, there is a blank line after every close tag, that is just plain wrong as you are instructing PHP to pass a blank line to the output buffer when you are not intending it. Just because you see any issues before doesnt mean it's not a bug. Look at any modern php application these days, none use php close tags.

It's even right there in PHP's own documentation!
http://php.net/manual/en/language.basic-syntax.instruction-separation.php

Quote:
Note: The closing tag of a PHP block at the end of a file is optional, and in some cases omitting it is helpful when using include() or require(), so unwanted whitespace will not occur at the end of files, and you will still be able to add headers to the response later. It is also handy if you use output buffering, and would not like to see added unwanted whitespace at the end of the parts generated by the included files.


Drak
Back to top
View user's profile Send private message
drak
Smarty Rookie


Joined: 25 Nov 2009
Posts: 11

PostPosted: Fri Nov 27, 2009 7:12 am    Post subject: Reply with quote

And... I the Zend Framework makes this a requirement in their reference guide

http://framework.zend.com/manual/en/coding-standard.php-file-formatting.html

Quote:
B.2.1. General
For files that contain only PHP code, the closing tag ("?>") is never permitted. It is not required by PHP, and omitting itī prevents the accidental injection of trailing white space into the response.


Drak
Back to top
View user's profile Send private message
smarto
Smarty n00b


Joined: 27 Nov 2009
Posts: 1

PostPosted: Fri Nov 27, 2009 3:07 pm    Post subject: Reply with quote

This is an server issue.

It occurred to me in my several projects, not specifically in smarty.

This happens in two cases.

1. FTP Software append trailing slash at the end of the file while uploading.

2. If you upload an zip/rar/tar file and then try to unzip it on server using the exec function in php with zip command or use an decompress utility provided in hosting panel.
(this issue mostly occurred to me on plaxo panel)
Solution:

If case 1 change FTP software.
If case 2 don't upload the zip file. Upload the php files without zipping it.
Back to top
View user's profile Send private message
SLX
Smarty Rookie


Joined: 19 Nov 2009
Posts: 11
Location: Wroclaw, Poland

PostPosted: Tue Dec 01, 2009 5:04 pm    Post subject: Reply with quote

It occour in files downloaded directly from smarty site ex. http://smarty.net/do_download.php?download_file=Smarty-3.0b5.zip just after unpacking.
Back to top
View user's profile Send private message
ctlajoie
Smarty Rookie


Joined: 20 Nov 2009
Posts: 29

PostPosted: Wed Dec 02, 2009 1:11 am    Post subject: Reply with quote

I too see what the OP is talking about. In Smarty.class.php of the beta 5 dist, the ?> is on line 540, and there is a trailing \n at the end of the file, making the file a total of 541 lines. PHP will eat the final newline, but if FTP software were to add another \n to the end of it, there would be problems. PHP does not eat multiple newlines following the ?>.

This issue is not limited to Smarty 3 -- it would happen in Smarty 2 also. Maybe it is just rare enough that nobody has complained about it happening in Smarty 2.

To be honest, I don't see a reason *not* to simply remove the ?> from all of the Smarty source files. You could make solve a rare issue experienced by at least a couple people with no negative effect.
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