Forum Moderators: coopster

Message Too Old, No Replies

Including PHP in a .tpl file

Need help Incuding PHP in a .tpl file

         

GalaxiDesigns

8:28 pm on Nov 16, 2007 (gmt 0)

10+ Year Member



Hello everyone. I am having trouble getting a PHP include to show up in a .tpl file I am editing. It shows nothing on the page where the PHP include is suppose to be. I looked all over the internet for some forum post or article explaining how to do this, and found out some information that helped, but still need help figuring out how to implement it. I am guessing in the .tpl file I need to add something like {PHPINCLUDENAME} and in another file tell it that {PHPINCLUDENAME} should = <?php include.php?>, but I am not sure exactly the syntax that I need to use, and what file it should be placed in. Any ideas would be appreciated, thanks.

PHP_Chimp

8:32 pm on Nov 16, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



So I take it that php is not set up to parse .tpl files on your system?
If it isnt then you could add the following to .htaccess -

AddType application/x-httpd-php .tpl

As this will tell php to parse all .tpl files. Then you can include as much php code in those files as you want to.

GalaxiDesigns

8:41 pm on Nov 16, 2007 (gmt 0)

10+ Year Member



Thanks for the tip, but I tried it and it didn't work. I even changed it to AddHandler, since my PHP is cgi, and it still did not work. I am guessing because I also have a mod rewrite which changes the links to something like mydomain.com/article/article-title. So in the end it is no longer a .tpl file. I could be wrong though, but I did try it and the PHP include did not show up.

Any other ideas? I feel we are on the right track.

PHP_Chimp

9:06 pm on Nov 16, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Humm works for me...
If the .tpl files are getting changed into another type, say html, then have you tried putting .html in the list of files to be parsed by php?

I dont work with php as a cgi, very often...so it could be something specific to using it from there as opposed to an Apache module.

If you are not using an extension for the files then you may have to use -


DefaultType application/x-httpd-php

To get the php in...however that may not work with your system if you need those files parsed as another type.

[edited by: PHP_Chimp at 9:11 pm (utc) on Nov. 16, 2007]

GalaxiDesigns

9:24 pm on Nov 16, 2007 (gmt 0)

10+ Year Member



Unfortunately that did not work either. Also I guess I should of mentioned, the mod rewrite does not change my page to a .html, it just changes it to a directory. So is there any way of putting a line of code in the htaccess file that tells it to treat directories as a php file? Probably not, but I thought I would ask.

PHP_Chimp

11:18 pm on Nov 16, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Mod_rewrite works line by line. So you may well need to put the .tpl to php line in above the rewrite to make the file into a directory (dont know if you did that or just added it to the end of the file).

If that doesnt work then at the moment I dont have any more ideas...will let you know if I come up with anything else though.

GalaxiDesigns

12:26 am on Nov 17, 2007 (gmt 0)

10+ Year Member



Yeah I actually tried that too, and it didn't make a difference. You can stop trying to figure out how though, because I just figured it out. It wasn't easy, and involved me editing every page on my site manually, but I did firgure it out.

See each section that I wanted the PHP include to appear in, had a .tpl file and a main .php file. The .php file is where it starts, so basically all I did was make a copy of the .php file, and kept the name almost the same, I would just put ad_ in front of it. Then in the original .php file, the one that still has the original file name, I erased everything and added the code below:


<?php
function myfunction() {
include 'http://www.domain.com/php-include.php';
}
ob_start();
include 'ad_phppage.php';
$page = ob_get_contents();
ob_end_clean();

ob_start();
myfunction();
$mystuff = ob_get_contents();
ob_end_clean();

echo str_replace( "***phpinclude***", $mystuff, $page );
?>

Then in the actual .tpl file, where I wanted the PHP include to go, I just put ***phpinclude***

Basically it just finds and replaces that with my PHP include code. So I guess it isn't that hard, it just depends on how many pages you have :) Thanks for all your help too, I really appreciate it.

[edited by: GalaxiDesigns at 12:28 am (utc) on Nov. 17, 2007]

ashishp

5:42 am on Nov 18, 2007 (gmt 0)

10+ Year Member



Is this .tpl file a part of your own homegrown template system or is it part of Smarty or any other templating system?

GalaxiDesigns

9:39 am on Nov 18, 2007 (gmt 0)

10+ Year Member



It was apart of a system called article dashboard, which manages articles into a directory.

ashishp

5:22 pm on Nov 18, 2007 (gmt 0)

10+ Year Member



Ah! It is a pity when scripts do not use standard templating systems..

Good thing you figured it out.. :)

GalaxiDesigns

11:56 pm on Nov 18, 2007 (gmt 0)

10+ Year Member



Yeah, I never understood why they don't just have ONE template file that manages the entire layout. In fact, the people who made it could have made their jobs easier by having just one template. Oh well, gotta deal with what you have :)