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

Including pure PHP subtemplates compile incorrectly

 
Post new topic   Reply to topic    Smarty Forum Index -> Smarty 3
View previous topic :: View next topic  
Author Message
Michael White
Smarty Rookie


Joined: 08 Oct 2009
Posts: 20

PostPosted: Mon Oct 26, 2009 4:48 pm    Post subject: Including pure PHP subtemplates compile incorrectly Reply with quote

I've had some problems with mixing .tpl and .php template includes. There is a temporary hack/fix below.

Note: Assumes $smarty->allow_php_templates = true;

Let's say I have a template named "main.tpl":
Code:

<html>
About to include a .php file.<br />
{include file="php:some_template.php"}
<br />Just included a .php file.
</html>


and another template named "some_template.php":
Code:

<?php
echo "I am a PHP template.<br />";
?>


We would expect the raw HTML source output to be this:
Code:

<html>
About to include a .php file.<br />
I am a PHP template.<br />
<br />Just included a .php file.
</html>


Instead I have been getting this:
Code:

<html>
About to include a .php file.<br />
<br />Just included a .php file.
</html>


I looked at the compiled version of "main.tpl" and found that the compiler is misbehaving. For some reason it thinks that "main.tpl" has a compiled template and is therefore not "echoing" anything but rather it does several tasks that include adding it to the $_tpl_stack array.

To solve this problem temporarily (without knowing what side-effects it may have) I have added the following line just below the current line 146 in "internal.compile_include.php". I am using revision 3280 (the latest revision as of a few hours ago).

Code:

$_output .= "<?php echo \$_template->fetch(); ?>";


This line simply tells it to still echo/print the actual template contents even when the parent template ("main.tpl") is found to have compiled templates.

I could also see this being a problem when both .tpl and .php files are included into the same .tpl file with the {include} plugin.[/code]

[edit]
The fix mentioned above does NOT work properly. You will end up with two renderings of each non-php include.

I came up with this fix just now to hopefully resolve the problem more accurately:

Place this code starting at about line 33 of the "internal.compile_include.php" file (just before the if($compiler... ) line)
Code:

      $include_path = str_replace(array('"', "'"), "", $include_file);
      $is_php_include = false;
      if(strtolower(substr($include_path, 0, 4)) == 'php:') {
         $is_php_include = true;
      }


Then, insert this line at about line 153 (line number assumes code above has already been inserted).
Code:

            if($is_php_include) {
               $_output .= "<?php echo \$_template->fetch(); ?>";
            }


[/edit]
Back to top
View user's profile Send private message Visit poster's website
U.Tews
Administrator


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

PostPosted: Mon Oct 26, 2009 8:39 pm    Post subject: Reply with quote

Michael

The offical fix looks different. It's in the SVN now.

Anyway thanks for bringing this up.
Back to top
View user's profile Send private message
Michael White
Smarty Rookie


Joined: 08 Oct 2009
Posts: 20

PostPosted: Mon Oct 26, 2009 8:51 pm    Post subject: Reply with quote

Awesome. Thanks for fixing it officially.

Also, I figured the official fix should look different as there were probably several things I failed to take into account in my temporary hack. Very Happy
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    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