|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
Michael White Smarty Rookie
Joined: 08 Oct 2009 Posts: 20
|
Posted: Mon Oct 26, 2009 4:48 pm Post subject: Including pure PHP subtemplates compile incorrectly |
|
|
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 |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Mon Oct 26, 2009 8:39 pm Post subject: |
|
|
Michael
The offical fix looks different. It's in the SVN now.
Anyway thanks for bringing this up. |
|
Back to top |
|
Michael White Smarty Rookie
Joined: 08 Oct 2009 Posts: 20
|
Posted: Mon Oct 26, 2009 8:51 pm Post subject: |
|
|
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. |
|
Back to top |
|
|
|
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
|