Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.

Smarty and <?xml
Goto page 1, 2  Next
 
Post new topic   Reply to topic    Smarty Forum Index -> Smarty 3
View previous topic :: View next topic  
Author Message
robert
Smarty Rookie


Joined: 04 Mar 2009
Posts: 13

PostPosted: Wed Mar 04, 2009 2:54 pm    Post subject: Smarty and <?xml Reply with quote

I just gave Smarty3 (r3014) a spin and noticed a few inconsistencies with Smarty 2.

A statement like this is just passes through the Smarty 3 parser
Code:
<?xml version="1.0" encoding="UTF-8" ?>

While Smarty2 will parse it like:
Code:
<?php echo '<?xml'; ?>
version="1.0" encoding="UTF-8"
<?php echo '?>'; ?>


Is this the desired behavior, and if so, how can one replicate Smarty 2's behavior?
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Wed Mar 04, 2009 3:14 pm    Post subject: Reply with quote

Smarty 3 compiles it into

Code:
<?php echo '<?xml version="1.0" encoding="UTF-8" ?>';?>


So what's wrong with that?
Back to top
View user's profile Send private message
robert
Smarty Rookie


Joined: 04 Mar 2009
Posts: 13

PostPosted: Wed Mar 04, 2009 3:26 pm    Post subject: Reply with quote

Nothing wrong with that, but thats not what i get.
Let me show you the output of the template file as an illustration.

This is the output smarty gives me:
Code:
<?php /* Smarty version Smarty3Alpha, created on 2009-03-01 15:25:01
         compiled from "./templates/includes/top.template" */ ?>
<?php if ($_smarty_tpl->getVariable('pageData')->value->mime!='text/html'): ?>
<?xml version="1.0" encoding="UTF-8"?>
<?php endif;?>


And that in turn produces the following PHP error:
Code:
Parse error: syntax error, unexpected T_STRING in /var/www/uonet/templates_c/2de955c712960e2cb26735b49527c588.top.template.php on line 4
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Wed Mar 04, 2009 3:35 pm    Post subject: Reply with quote

Okay, I found a problem in the parser.

I will let you know when the fix is in the SVN.
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Wed Mar 04, 2009 3:53 pm    Post subject: Reply with quote

The SVN has been updated with a fix.
Back to top
View user's profile Send private message
robert
Smarty Rookie


Joined: 04 Mar 2009
Posts: 13

PostPosted: Wed Mar 04, 2009 4:00 pm    Post subject: Reply with quote

The fix only does half the work, after the fix it now outputs:
Code:
<?xml version="1.0" encoding="UTF-8"<?php echo '?>';?>


Since the beginning of the XML tag isn't escaped the same error still occurs.
This needs one more look
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Wed Mar 04, 2009 4:15 pm    Post subject: Reply with quote

Can you try again?
Back to top
View user's profile Send private message
robert
Smarty Rookie


Joined: 04 Mar 2009
Posts: 13

PostPosted: Wed Mar 04, 2009 4:18 pm    Post subject: Reply with quote

Updated repository and tried again, but this time theres no change at all.
Still same output as last time.
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Wed Mar 04, 2009 4:26 pm    Post subject: Reply with quote

Strange.

Code:
<?xml version="1.0" encoding="UTF-8"?>


compiles on my place to
Code:
<?php echo '<?xml';?> version="1.0" encoding="UTF-8"<?php echo '?>';?>


Did you set force_compile or delete the old compiled template?
Back to top
View user's profile Send private message
robert
Smarty Rookie


Joined: 04 Mar 2009
Posts: 13

PostPosted: Wed Mar 04, 2009 4:36 pm    Post subject: Reply with quote

After some more exhaustive testing I came to the conclusion that this problem does not occur unless the <?xml statement in enclosed by a if-statement.

So, just having a template with the following works:
Code:
<?xml version="1.0" encoding="UTF-8"?>


While this does not:
Code:
{if $foo}
<?xml version="1.0" encoding="UTF-8"?>
{/if}
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Wed Mar 04, 2009 5:00 pm    Post subject: Reply with quote

I think i finally found it.

Please try once again.
Back to top
View user's profile Send private message
robert
Smarty Rookie


Joined: 04 Mar 2009
Posts: 13

PostPosted: Wed Mar 04, 2009 5:11 pm    Post subject: Reply with quote

I think you got it this time, it gives me the correct output.

Theres only one difference now when having the xml declaration inside a if statement, and that is that it forces a newline before the xml statement, and this is not allowed according to the XHTML standards.

This is the output I get:
Code:
<?php echo '
<?xml';?> version="1.0" encoding="UTF-8"<?php echo '?>';?>


Nice and properly escaped, just the damned newline that shouldn't be there.
Now I think Smarty 2 had the same behavior which I circumvented by writing the code like this:
Code:
{if $foo
}<?xml ... ?>{
/if}


That is, however now allowed in Smarty 3, so could you remove the output of the newline?
Back to top
View user's profile Send private message
robert
Smarty Rookie


Joined: 04 Mar 2009
Posts: 13

PostPosted: Wed Mar 04, 2009 5:15 pm    Post subject: Reply with quote

The newline should instead be after the XML statement.
This would be correct in this case:
Code:
?php echo '<?xml';?> version="1.0" encoding="UTF-8"<?php echo '?>
';?>


That should be the anticipated output and makes the DOCTYPE begin on a line of its own.
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Wed Mar 04, 2009 5:23 pm    Post subject: Reply with quote

I don't think it's a bug. When you do this:

Code:
{if ...}
<?xml ... ?>
{/if}


The newline is coming from the template, because you have a newline after the {if} tag. The template is behaving exactly as you have it. You can get around this problem by removing the newline:

Code:
{if ...}<?xml ... ?>{/if}


Or use {strip} tags to remove whitespace. Or, trim() the template output.
Back to top
View user's profile Send private message Visit poster's website
robert
Smarty Rookie


Joined: 04 Mar 2009
Posts: 13

PostPosted: Wed Mar 04, 2009 5:42 pm    Post subject: Reply with quote

Ah, I see.

Thanks for the fast reply, and I hope next time I might be able to help out a bit more and supply a patch or two
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> Smarty 3 All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
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