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

postfilter.glue_php
Goto page 1, 2  Next
 
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 -> Plugins
View previous topic :: View next topic  
Author Message
aloner
Smarty Rookie


Joined: 24 Apr 2003
Posts: 24

PostPosted: Fri Apr 25, 2003 9:35 am    Post subject: postfilter.glue_php Reply with quote

There are a lot of code like "?> <?php" in compiled templates. I suppose that removing them will speed up parsing a bit.

Here's the code:

[php:1:b5aadcfdee]
<?php
/*
* Smarty plugin
* -------------------------------------------------------------
* File: postfilter.glue_php.php
* Type: postfilter
* Name: glue_php
* Purpose: Glues PHP blocks together. Should speed up parsing a little (probably).
* -------------------------------------------------------------
*/
function smarty_postfilter_glue_php($compiled, &$smarty)
{
return preg_replace('!\?\>\s*\<\?php!', "\n", $compiled);
}
?>
[/php:1:b5aadcfdee]
_________________
Your ad here.
Back to top
View user's profile Send private message
andre
Smarty Pro


Joined: 23 Apr 2003
Posts: 164
Location: Karlsruhe, Germany

PostPosted: Fri Apr 25, 2003 10:15 am    Post subject: Reply with quote

Simple but nice Very Happy
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Fri Apr 25, 2003 2:45 pm    Post subject: Reply with quote

You wouldn't want to clean these up if there are spaces between them, you'd lose some (possibly intentional) formatting.

now, removing:

?><?php

and:

?>\n<?php


would be feasible, since this doesn't alter the template output. I'll look into this, maybe it's something Smarty should do by default.

Monte
Back to top
View user's profile Send private message Visit poster's website
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Fri Apr 25, 2003 5:09 pm    Post subject: Reply with quote

removing [php:1:dcb336d7d8]?>\n<?php[/php:1:dcb336d7d8] is only acceptable for HTML-only output. I think the core engine should leave non- smarty portions of templates completely as-is allowing filters to do the rest.

Last edited by boots on Sat May 24, 2003 6:53 am; edited 1 time in total
Back to top
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Fri Apr 25, 2003 5:23 pm    Post subject: Reply with quote

maybe

[php:1:af9f8e61ee]return preg_replace('!\?\>\n?\<\?php!s', "", $compiled);[/php:1:af9f8e61ee]

???

it's not heavily tested, but shouldn't have any impact on the output.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
mohrt
Administrator


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

PostPosted: Fri Apr 25, 2003 10:01 pm    Post subject: Reply with quote

boots wrote:
removing [php:1:fdec785836]?>\n<?php[/php:1:fdec785836] is only acceptable for HTML-only output. I think the core engine should leave non- smarty portions of templates completely as-is allowing filters to do the rest.


That should be acceptible because PHP eats the newlines after ?> tags anyways.

Monte
Back to top
View user's profile Send private message Visit poster's website
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Fri Apr 25, 2003 11:40 pm    Post subject: Reply with quote

Quote:
PHP eats the newlines after ?> tags anyways.


I forgot. That also explains a behaviour I had noticed earler but couldn't figure out. Sad
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Fri Apr 25, 2003 11:51 pm    Post subject: Reply with quote

HEY! I just checked and PHP DOES NOT eat the newlines between ?> .. <?
but DOES eat them inside (natch).

Code:
1


4
<?php


?>

5

6


Point your browser to that and you get:

Code:
1 4 5 6


BUT do a view source and you get:

Code:
1


4

5

6


All the interspersed \n are preserved!
Back to top
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Sat Apr 26, 2003 12:02 am    Post subject: Reply with quote

of course they are preserved. php is a templating system Wink
but a newline directly after "?>" is slurped by php.
(this is what my preg-exp mactches)

do this:

Code:

<echo "foo"; ?>

<echo "bar"; ?>


it leads to

foo
bar

(one newline).

it does not lead to (as one might think)
Code:

foo

bar

(two newlines as in the php-source)
Back to top
View user's profile Send private message Send e-mail Visit poster's website
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Sat Apr 26, 2003 12:22 am    Post subject: Reply with quote

That makes it clearer. Good stuff.

I haven't tested your reg-exp. If I got it right, you are optionally matching one newline. So it won't make a difference because it would never survive PHP anyway. Right. But if that is the only case that is going on, I wonder if it justifies an extra regexp in the core for each fetch as opposed to using a filter plugin (for those who want it).

I really don't think it will speed up PHP parsing while at the same time it adds a runtime cost.
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Sat Apr 26, 2003 3:11 am    Post subject: Reply with quote

If this was implemented it would definately be done at compile time. After the template is compiled, strip out unnecessary close/open tags right then.

Monte
Back to top
View user's profile Send private message Visit poster's website
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Sat Apr 26, 2003 9:01 am    Post subject: Reply with quote

Does the compiler insert a \n after it inserts a <?php ?> block?

If not, perhaps a \n should be ADDED between ?> <? during output to preserve \n's that get consumed but were part of the original stream. Or, perhaps a \n should be emitted after <?php ?> blocks to eliminate the need for the replacement in the output.

update:

scratch that idea.
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Sat Apr 26, 2003 3:58 pm    Post subject: Reply with quote

The removal of unnecessary close/open tags has been committed to CVS.

Quote:

Does the compiler insert a \n after it inserts a <?php ?> block?


Smarty does everything it can to make the output match the template. The only thing I know as an exception is Smarty comments. A {* comment *} on a line by itself (or multiline) will leave an extra newline in the output (the one after the comment.)

Monte


Last edited by mohrt on Sat Apr 26, 2003 4:03 pm; edited 1 time in total
Back to top
View user's profile Send private message Visit poster's website
Tom Sommer
Administrator


Joined: 16 Apr 2003
Posts: 47
Location: Denmark

PostPosted: Sat Apr 26, 2003 3:59 pm    Post subject: Reply with quote

looks good
Back to top
View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger MSN Messenger
Budda
Smarty Regular


Joined: 19 Apr 2003
Posts: 53
Location: Lymm, Cheshire. UK

PostPosted: Sun Aug 17, 2003 5:21 pm    Post subject: Reply with quote

Exactly how much faster does removing these extra ?> <?php tags make Smarty?
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger
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 -> Plugins 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