Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

php template

Status
Not open for further replies.

kharddie

Instructor
Feb 7, 2006
23
AU
Hey guys
how do i customise this class so that the Creatpage() function ( page send to the browser) is processed as a php page instead of a html page . my html template has php include codes in the tables so it has to be processed as .php ?????
tried to save the template.php as template.inc and template.php.inc????? but doesnt work

pure html code runs well but once i include php code it it then the template.inc doent open ..pls help it has to work

thanks


<?php

class HtmlTemplate{

var $template;
var $html;
var $parameters= array();

function HtmlTemplate ($template) { //sets which template to be used

$this->template = $template;
$this->html = implode("",(file($this->template))); //read the template into an array and then creat
}


function SetParameter ($variable,$value){ //sets tha particular values

$this->parameters[$variable] = $value;
}


function CreatePage (){ //does the bulk of the work


foreach ($this->parameters as $key => $value){ //loop thro all the parameters and set the variables to values.
$template_name='{'.$key.'}';

$this->html =str_replace($template_name,$value,$this->html);
}
echo $this->html;



}
}




?>
 
You need to save the file with a .php file extension and then save the file in a directory (most likely on a server) that is managed using Apache server. If you are saving this file locally on you hard disk and then attempting to open the file using a web browser you will not get any php to be parsed - unless you are running an Apache server on your local compute (and request the file using a full http request:
Cheers,
Jeff

[tt]Jeff's Page @ Code Couch
[/tt]

What is Javascript? FAQ216-6094
 
you don't need to worry about it by the looks of your code. when you are echoing it (echo $this->html) you are doing so as part of a php script and this should parse the tags on the fly.
 
am running apache on my local server but still nothing ...below is the template.php and the php page that calls the template using the class above (HtmlTemplate.class)

template.php>>>>>>>>>>>>>>>>>>>
<html>
<head>
<title> Page 1 </title>
<link rel="stylesheet" type="text/css" href="{CSS}"/>
</head>

<body>
<?php
include ("{TOPHTM}");
?>
<table width="90%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td><?php
include ("TOPMIDHTM}");
?></td>
</tr>
</table>
<!-- Table for Main Body -->
<table width="81%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="111" align="right" valign="top">
<?php
include ("{MENU}");
?>
</td>
<td width="1" bgcolor="#336601" valign="top"> </td>
<td valign="top" bgcolor="#FFFFFF">

<?php
include ("{BODY}");
?>
<br>
<br></td>
</tr></table>

<table width="90%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td><?php
include ("{FOOTER}");
?></td>
</tr>
</table>
</body></html>


index.php>>>>>>>>>>>>>>>>
<?php

//use htmlTemplateclass
//sa

require_once "classes/HtmlTemplate.class"; //include the class

$page = new HtmlTemplate("template.inc"); //create an instance of the class---object





$page->SetParameter("TOPHTM","top.htm");
$page->SetParameter("TOPMIDHTM","mid.htm");
$page->SetParameter("MENU","menu.htm");
$page->SetParameter("BODY","body.htm");
$page->SetParameter("FOOTER","footer.htm");


$page->CreatePage(); //SEND THE PAGE TO THE BROWSER.



?>
 
this looks fine.

is php working at all? do you get proper output if you try <? phpinfo(); ?>?

if php is working then have you enabled read access to the apache user for the directories in which the templates are stored?

what error messages are you receiving - if none are the screen AND the source code blank? have you ensured that error checking is on in php.ini (or put error_reporting(E_ALL); as the first line of your index.php script).
 
php is working , bacause when i run the template.php with the files directory it works fine.(befor i put the {}to turn it into a template).

index.php runs well and just shows blank tables (i changed the table borders to "1" in the template.php above).

no error messages either

is it because i saved the template.php as template.inc which is used by the index.php???????the php scripts are not being read in the template.i have used it before with only html and all works fine.

please help...thanks for ur contributions
 
what you call a file that you are "including" is irrelevant.

but you do need to make sure that you are calling the right file.

it strikes me that you should be calling template.php if that is what you have named the file.

Code:
$page = new HtmlTemplate("template.php");
 
Looking at the code in the HtmlTemplate class, this line:
Code:
echo $this->html;
I know there is a lot more stored in $this->html, but this simple example will do. In essence you are saying:
Code:
echo '<html><head></head><body><?php include ("menu.htm"); ?></body></html>';
PHP will not evaluate the string contained in $this->html, for addition PHP blocks to execute. To execute a command like 'include' you need to exit the string; something like:
Code:
echo '<html><head></head><body>'.include ("menu.htm").'</body></html>';
With the file() call in the constructor of the HtmlTemplate class you have taken the contents of your template and brought it into your PHP code, so there is no need wrap additional PHP code in opening and closing tags.

That said, I think the easiest way to get the code working like you envisioned is to define an include tag used by your templating system, so when doing the str_replace() in the CreatePage() method, you can look for the include tag and have you code act accordingly.
 
hey Itshim, jpadie the problem am still having is that this template is echoed as a .htm to the browser so the php part wont run and the results of the index.php is just the tables showing. the same thing happens when i tried to save and run the template on its own as template.htm only the tables show and not the include files.can the creatpage() echo it as a .php
 
i don't understand what you mean by
echoed as a .htm
. i'm assuming your aim is to echo/print html (and perhaps css + javascript) to the browser. that's what most web applications end up doing after processing some data.

are you saying that the html language is not itself being displayed within the webpage but is being downloaded as an attachment?
 
i ment the template is echoed and processed as template.htm when called by the index.php instead of template.php which should be tha case for the php "includes" to work
 
echoed and processed as template.htm

this is what i don't understand. do you mean:

1. your code is looking for a file called template.htm rather than template.php? and as it cannot find the file the code fails?

2. your code is not outputting all the php tags that are in template.php instead of parsing them as php (have you read Itshim's post above?)?

3. something else.

the code you have posted looks ok but you have not posted your complete code set so we are just having to guess at what you mean. can you point us to a url where the problem is exhibited, perhaps?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top