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

Caching a Smarty XML template causes newlines to be removed

 
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
cbj4074
Smarty Regular


Joined: 10 Nov 2011
Posts: 49

PostPosted: Thu Aug 08, 2013 9:11 pm    Post subject: Caching a Smarty XML template causes newlines to be removed Reply with quote

It's possible that the behavior I'm observing is related to an old bug ( http://www.smarty.net/forums/viewtopic.php?p=75440 ).

Basically, I am generating an XML document and rendering it using a Smarty template. This works as expected -- until I attempt to cache the document using Smarty's caching feature.

When caching is disabled, the first two lines of the document look like this:

Code:

<?xml version="1.0" encoding="UTF-8"?>
     <rss version="2.0">


But as soon as I enable caching, the first two lines look like this:

Code:

<?xml version="1.0" encoding="UTF-8"?><rss version="2.0">


It seems as though Smarty removes newlines arbitrarily in the process of caching. I don't know what else to make of this behavior.

I noticed the problem in Smarty 3.1.11, and I just upgraded to Smarty 3.1.14 (I cleared my compile and cache directories after upgrading), yet the behavior persists.

Thanks in advance for any assistance.
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Thu Aug 08, 2013 9:39 pm    Post subject: Reply with quote

I can't reproduce your problem.
If I view the browsers source I see the 2 two lines as expected.
Back to top
View user's profile Send private message
cbj4074
Smarty Regular


Joined: 10 Nov 2011
Posts: 49

PostPosted: Fri Aug 09, 2013 1:54 pm    Post subject: Reply with quote

Thanks, U.Tews. I really appreciate you taking the time to try to reproduce the behavior.

Here's the cached template source that is generated:

Code:

<?php /*%%SmartyHeaderCode:57835204f28bba7de9-34870367%%*/if(!defined('SMARTY_DIR')) exit('no direct access allowed');
$_valid = $_smarty_tpl->decodeProperties(array (
  'file_dependency' =>
  array (
    '2341220d3f721c726f8c4a0461bc40ed5fad2148' =>
    array (
      0 => 'C:\\Users\\User\\Documents\\Apache\\project\\trunk\\protected\\templates\\common\\include\\blog\\rss.tpl',
      1 => 1375994598,
      2 => 'file',
    ),
  ),
  'nocache_hash' => '57835204f28bba7de9-34870367',
  'variables' =>
  array (
    'xml' => 0,
  ),
  'has_nocache_code' => false,
  'version' => 'Smarty-3.1.14',
  'unifunc' => 'content_5204f28bbff501_17561670',
  'cache_lifetime' => 3600,
),true); /*/%%SmartyHeaderCode%%*/?>
<?php if ($_valid && !is_callable('content_5204f28bbff501_17561670')) {function content_5204f28bbff501_17561670($_smarty_tpl) {?><?php echo '<?'; ?>xml version="1.0" encoding="UTF-8"<?php echo '?>'; ?>
<rss version="2.0">
    <channel>
        <title>localhost/sq RSS Feed</title>
      [...]


It seems that Smarty includes a separate call to echo() for the DTD, "<?xml version="1.0" encoding="UTF-8"?>". When you attempted to reproduce this, did you include an XML DTD, as in my example?

Is it possible that Smarty thinks the "<?" and "?>" are PHP parsing delimiters, and handles them in a "special" way as a result, thus eliminating a newline that was present in the source?
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Fri Aug 09, 2013 2:32 pm    Post subject: Reply with quote

I think I found it. I assume you use the trimwhitespace output filter.
The code in the cache file should look like this


Code:
<?php if ($_valid && !is_callable('content_5204f28bbff501_17561670')) {function content_5204f28bbff501_17561670($_smarty_tpl) {?><?php echo '<?'; ?>xml version="1.0" encoding="UTF-8"<?php echo '?>'; ?>
 
<rss version="2.0">
    <channel>


The filter does remove the required additional line feed.
I must have a closer look what we can do about it.
I will try to fix it ASAP
Back to top
View user's profile Send private message
cbj4074
Smarty Regular


Joined: 10 Nov 2011
Posts: 49

PostPosted: Fri Aug 09, 2013 2:55 pm    Post subject: Reply with quote

Thanks for the quick follow-up, and your willingness to try to implement a fix. I appreciate both.

I'm not using the trimwhitespace output filter (unless it is applied by default).
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Fri Aug 09, 2013 3:01 pm    Post subject: Reply with quote

Are you shure you don't use trimwhitespace?
Then it's getting strange....
Because I see the problem here when load the filer.
The filter is not loaded by default.

Can you then please post the corresponding lines of the compiled template file.
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Fri Aug 09, 2013 3:17 pm    Post subject: Reply with quote

Correction of my last post. trimwhitespace is not causing this.

the line breake must be lost somewhere else.

which OS and PHP version do you use?
Back to top
View user's profile Send private message
cbj4074
Smarty Regular


Joined: 10 Nov 2011
Posts: 49

PostPosted: Fri Aug 09, 2013 7:00 pm    Post subject: Reply with quote

Thanks, U.Tews.

I'm on Windows 7 x64 SP1 with PHP 5.4.8 (API20100525,TS,VC9).

If necessary, I can test this on my other development box (which runs Ubuntu 12.04 with PHP 5.4.3).

I sent the template files to you, too. (U.Tew... hehehe.)
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Sat Aug 10, 2013 10:06 am    Post subject: Reply with quote

With the code you provided I found out that
Code:
<?xml version="1.0" encoding="UTF-8"?>

was not in the template source but passed as a variable.

If caching is enabled <? and ?> in variable output needs special treatment to avoid possible code injection. In this case the filter did eat the newline.

The fix is now in the SVN trunk and will later be included in 3.1.15
Back to top
View user's profile Send private message
cbj4074
Smarty Regular


Joined: 10 Nov 2011
Posts: 49

PostPosted: Mon Aug 12, 2013 3:05 pm    Post subject: Reply with quote

Great! Thank you for working to establish the cause and for implementing a fix so quickly. I really appreciate it! Very Happy
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