Page 1 of 1
PHP in "fread" templates
Posted: Thu May 22, 2003 12:31 am
by Mr. Tech
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
Posted: Thu May 22, 2003 3:48 am
by volka
fread reads the plain contents of a file.
either
http://php.net/include or
http://php.net/eval will explain what you want.
Posted: Thu May 22, 2003 5:55 am
by mzfp2
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.
Posted: Thu May 22, 2003 5:59 am
by Jim
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

Posted: Thu May 22, 2003 5:01 pm
by Mr. Tech
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!
Posted: Thu May 22, 2003 5:23 pm
by Sevengraff
for working with templates, i've found
ETS to work the best.
Posted: Thu May 22, 2003 6:36 pm
by Mr. Tech
It's to late now. I've written to many scripts the way i have.
Anyway, this script here uses the same template code as me and is able to use PHP in templates:
http://scripts.webmastersite.net/index.php?articleid=21
Any ideas?
Posted: Fri May 23, 2003 2:07 am
by twigletmac
Isn't the point of using templates to not have any PHP code in with the HTML?
Mac
Posted: Fri May 23, 2003 2:17 am
by Mr. Tech
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?
Posted: Sun May 25, 2003 9:07 pm
by Mr. Tech
no one?
Posted: Sun May 25, 2003 9:39 pm
by Mr. Tech
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!!!
