PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
Mr. Tech
Forum Contributor
Posts: 205 Joined: Tue Feb 11, 2003 4:18 pm
Location: Australia
Post
by Mr. Tech » Thu May 22, 2003 12:31 am
Hi again!
I was creating a script which has templates. This is the code I used:
Code: Select all
<?php
function fileread($filename)
{
$fd = @fopen($filename, 'r');
$filecontents = @fread ($fd, filesize($filename));
@fclose ($fd);
return $filecontents;
}
?>
Then to include the file I add:
Code: Select all
<?php
$header = fileread("templates/header.php");
$header = str_replace("{SITETITLE}", $sitetitle, $header);
?>
The problem is, it doesn't allow me to use PHP in the templates...... Does anyone have any idea how?
Thank you for any help!
Ben
mzfp2
Forum Contributor
Posts: 137 Joined: Mon Nov 11, 2002 9:44 am
Location: UK
Contact:
Post
by mzfp2 » Thu May 22, 2003 5:55 am
I think to output the template you need to use print $filecontents, your code it simply reads the contents into a array, doesnt actually create a new file for the template.
Jim
Forum Contributor
Posts: 238 Joined: Fri Apr 19, 2002 5:26 am
Location: Near Austin, Texas
Post
by Jim » Thu May 22, 2003 5:59 am
I've used templates like this several times before, and just like Volka said, you will have to use "eval" to get any PHP in those includes to work on your page.
Sorry to restate something someone else said, but I just mean to offer reasurrance that eval is the right way to go
Mr. Tech
Forum Contributor
Posts: 205 Joined: Tue Feb 11, 2003 4:18 pm
Location: Australia
Post
by Mr. Tech » Thu May 22, 2003 5:01 pm
Thanks guys!
I was looking come other code by someone else:
Code: Select all
<?php
function OutputPhpDocument($phpDocument)
{
$questMark = "?";
$phpOpenTag = "<${questMark}php";
$phpCloseTag = "${questMark}>";
return eval("$phpCloseTag" . stripslashes($phpDocument) . "$phpOpenTag "); // note the trailing space!
}
?>
I tried it but it didn't work. Do you know the exact code I need?
Where abouts do I need to put this code?
Thank you so much for your help!
Sevengraff
Forum Contributor
Posts: 232 Joined: Thu Apr 25, 2002 9:34 pm
Location: California USA
Contact:
Post
by Sevengraff » Thu May 22, 2003 5:23 pm
for working with templates, i've found
ETS to work the best.
twigletmac
Her Royal Site Adminness
Posts: 5371 Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK
Post
by twigletmac » Fri May 23, 2003 2:07 am
Isn't the point of using templates to not have any PHP code in with the HTML?
Mac
Mr. Tech
Forum Contributor
Posts: 205 Joined: Tue Feb 11, 2003 4:18 pm
Location: Australia
Post
by Mr. Tech » Fri May 23, 2003 2:17 am
twigletmac wrote: Isn't the point of using templates to not have any PHP code in with the HTML?
Mac
That is one reason but another reson is that they can customize it easily....
And some people have PHP code to include files and other things in there files.
Does anyone know?
Mr. Tech
Forum Contributor
Posts: 205 Joined: Tue Feb 11, 2003 4:18 pm
Location: Australia
Post
by Mr. Tech » Sun May 25, 2003 9:07 pm
no one?
Mr. Tech
Forum Contributor
Posts: 205 Joined: Tue Feb 11, 2003 4:18 pm
Location: Australia
Post
by Mr. Tech » Sun May 25, 2003 9:39 pm
aha!!! I worked it out!
Code: Select all
<?php
$qmark = "?";
$php_open = "<${qmark}php";
$php_close = "${qmark}>";
$main = fileread("template.php");
$main = eval("$php_close$main$php_open ");
print "$main";
?>
The:
$main = eval("$php_close$main$php_open ");
is what does it!
Thanks again volka!
yipppeeeee!!!