PHP in "fread" templates

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

Post Reply
Mr. Tech
Forum Contributor
Posts: 205
Joined: Tue Feb 11, 2003 4:18 pm
Location: Australia

PHP in "fread" templates

Post 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
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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.
mzfp2
Forum Contributor
Posts: 137
Joined: Mon Nov 11, 2002 9:44 am
Location: UK
Contact:

Post 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.
Jim
Forum Contributor
Posts: 238
Joined: Fri Apr 19, 2002 5:26 am
Location: Near Austin, Texas

Post 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 ;)
Mr. Tech
Forum Contributor
Posts: 205
Joined: Tue Feb 11, 2003 4:18 pm
Location: Australia

Post 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!
User avatar
Sevengraff
Forum Contributor
Posts: 232
Joined: Thu Apr 25, 2002 9:34 pm
Location: California USA
Contact:

Post by Sevengraff »

for working with templates, i've found ETS to work the best.
Mr. Tech
Forum Contributor
Posts: 205
Joined: Tue Feb 11, 2003 4:18 pm
Location: Australia

Post 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?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

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 »

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 »

no one?
Mr. Tech
Forum Contributor
Posts: 205
Joined: Tue Feb 11, 2003 4:18 pm
Location: Australia

Post by Mr. Tech »

volka wrote:fread reads the plain contents of a file.

either http://php.net/include or http://php.net/eval will explain what you want.
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!!! :P :D :) 8)
Post Reply