View previous topic :: View next topic |
Author |
Message |
drak Smarty Rookie
Joined: 25 Nov 2009 Posts: 11
|
Posted: Wed Nov 25, 2009 6:56 am Post subject: White space in Smarty code |
|
|
I have noticed a LOT of whitespace in the Smarty PHP files
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 |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Thu Nov 26, 2009 1:33 am Post subject: |
|
|
where are you seeing white space? particular file examples? |
|
Back to top |
|
drak Smarty Rookie
Joined: 25 Nov 2009 Posts: 11
|
Posted: Thu Nov 26, 2009 11:28 am Post subject: |
|
|
Every single. Just remove the trailing ?> it's really not required and saves white trailing space from creeping again.
Drak |
|
Back to top |
|
SLX Smarty Rookie
Joined: 19 Nov 2009 Posts: 11 Location: Wroclaw, Poland
|
Posted: Thu Nov 26, 2009 12:41 pm Post subject: |
|
|
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 |
|
sadness203 Smarty Rookie
Joined: 12 Nov 2009 Posts: 11
|
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Thu Nov 26, 2009 1:57 pm Post subject: |
|
|
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 |
|
drak Smarty Rookie
Joined: 25 Nov 2009 Posts: 11
|
Posted: Thu Nov 26, 2009 2:01 pm Post subject: |
|
|
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
Drak |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Fri Nov 27, 2009 4:05 am Post subject: |
|
|
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 |
|
drak Smarty Rookie
Joined: 25 Nov 2009 Posts: 11
|
Posted: Fri Nov 27, 2009 6:40 am Post subject: |
|
|
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 |
|
drak Smarty Rookie
Joined: 25 Nov 2009 Posts: 11
|
Posted: Fri Nov 27, 2009 7:12 am Post subject: |
|
|
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 |
|
smarto Smarty n00b
Joined: 27 Nov 2009 Posts: 1
|
Posted: Fri Nov 27, 2009 3:07 pm Post subject: |
|
|
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 |
|
SLX Smarty Rookie
Joined: 19 Nov 2009 Posts: 11 Location: Wroclaw, Poland
|
|
Back to top |
|
ctlajoie Smarty Rookie
Joined: 20 Nov 2009 Posts: 29
|
Posted: Wed Dec 02, 2009 1:11 am Post subject: |
|
|
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 |
|
|