Forum Moderators: coopster

Message Too Old, No Replies

PHP template

I need to know how to configure a php template

         

ssskaya

6:10 pm on Mar 18, 2004 (gmt 0)

10+ Year Member



Hi.

I have a .php file that I want to use as a template.

The files that I will use with this template file will have very simple scripts such as:

<p>a paragraph of text</p>
<p>a paragraph of text</p>
<p>a paragraph of text</p>
<p>a paragraph of text</p>

I want to know how I do it.

IMPORTANT:

I am not looking for something like:

<?php include "header.php");?>
text
<?php include "footer.php");?>

In between is not gonna work for me.

I need a template file which I can use as a reference which will already have enough of php include's in it.

text will be just for one of the table cells.

thanks.

coopster

8:49 pm on Mar 18, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, ssskaya!

Table cells? Are you referring to a table-based html page layout or an actual table of data? Can you offer more details?

ssskaya

9:11 pm on Mar 18, 2004 (gmt 0)

10+ Year Member



Hi.

Thanks for the nice welcome!

What I meant was:

In my template file I also have other php files inserted by <?php include

I also have a couple of tables in the very same template file. (meaning table as in <table>)

And the "text" I mentioned will be put in a cell of one of these tables. (<td>)

The template file is actually is an html file with different php files "include"d in some certain cells.

Anyways, below is the actual template that I want to use.

All I want to add to it with the other file is {tpl_title} and {tpl_head_text}. you can see below where I want them.

thanks.

----------

<html>

<head>
<title>FiscalStudy.com - {tpl_title}</title>
<link rel="stylesheet" type="text/css" href="http://www.example.com/css1.css" />
<link rel="stylesheet" type="text/css" href="http://www.example.com/css2.css" />
</head>

<body>

<?php include("http://www.example.com/header.php");?>

<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td valign="top" width="15%"><font size="1"><br></font>
<?php include("http://www.example.com/menu.php");?>
</td>
<td width="1%" valign="top" class="vertical">&nbsp;</td>
<td valign="top" width="2%">&nbsp;</td>
<td valign="top" width="82%"><div align="center">
<p>&nbsp;</p>

<table width="62%" border="0" align="center" cellpadding="0" cellspacing="0" style="border: 2px solid #000000">
<tr>
<td bgcolor="#000000"><div align="right"><font color="#FFFFFF" size="3">News
Flash of the Week</font></div></td>
</tr>
<tr>
<td>&nbsp;</td>
</tr>
<tr>
<td> <div align="center"><strong>{tpl_title}</strong></div></td>
</tr>
<tr>
<td>&nbsp;</td>
</tr>
<tr>
<td><div align="justify">{tpl_head_text}</div></td>
</tr>
<tr>
<td>&nbsp;</td>
</tr>
<tr>
<td><hr color="#000000" size="1"></td>
</tr>
<tr>
<td><div align="center"><a href="http://www.example.com/flash/preflash.php"><font size="2"><u>previous
weeks' flashes!</u></font></a><font size="1"></font></div></td>
</tr>
<tr>
<td>&nbsp;</td>
</tr>
</table>
<p>&nbsp;</p>
</div>
</td>
</tr>
</table>

<?php include("http://www.example.com/footer.php");?>

</body>
</html>

[edited by: jatar_k at 6:45 pm (utc) on Mar. 19, 2004]
[edit reason] generalized urls [/edit]

coopster

10:37 pm on Mar 18, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Why not set variables at the beginning of your script and use those variables throughout by invoking the parser as you are with your includes? For example:
<?php 
$tpl_title = 'My Title';
$tpl_head_text = 'Heading Text';
?>
<html>
<head>
...
<title>FiscalStudy.com - <?php print $tpl_title;?></title>
...
<td><div align="center"><strong><?php print $tpl_title;?></strong></div></td>
...
<td><div align="justify"><?php print $tpl_head_text;?></div></td>
...

You can easily change the titles based on some type of condition by using an if statement or a switch [php.net] perhaps. Here is a similar discussion...Titles in php pages [webmasterworld.com].

ssskaya

11:33 pm on Mar 18, 2004 (gmt 0)

10+ Year Member



I understand that.

but once I set my template file this way, I would like to know how I should have the corresponding files?

can you please post a sample script?

ssskaya

10:24 am on Mar 22, 2004 (gmt 0)

10+ Year Member



For use with the template file, I am sent something like this:

<?php
$tpl = implode( '' , file( 'template.php' ) );
$search = array( '{tpl_head_text}' , '{tpl_title}' );
$replace = array( 'h1 title text' , 'this is the page title' );
$str = str_replace( $search, $replace , $tpl );
echo $str;
?>

But with that, I had trouble with the php inclusions in the template file. They just did not work.

Please help.

thanks.

nostra

2:21 pm on Mar 23, 2004 (gmt 0)

10+ Year Member



@first you have to read in this file:

if (file_exists($templatefile)) {
$newFile=fopen($templatefile,"r");
//read out the content
$fileContent=fread($newFile,filesize($templatefile));
//close the file
fclose($newfile);

$search = array( '{tpl_head_text}' , '{tpl_title}' );
$replace = array( 'h1 title text' , 'this is the page title' );
$str = str_replace( $search, $replace , $fileContent );
}
Try this one =)

But, pls note, this is a very simple example, and should not declared as a template System *G*

ssskaya

11:33 am on Mar 29, 2004 (gmt 0)

10+ Year Member



actually, I could not get yours to work.

can you tell me why the php include functions (inside the template file) do not work when you 'implode' the template file? They work all fine when you use 'include' or 'require'.

ssskaya

1:24 pm on Mar 29, 2004 (gmt 0)

10+ Year Member



to coopster:

I really like what you are suggesting.

It would be so easy to implement it as follows:

<?php
$title = "whatever title!";
$text = "whatever text";
?>

<?php include ("flashtemplate.php");?>

But my problems is, for the $text, I have to insert a text with html codes which is as long as 5 paragraphs. Something like:

<p>a paragraph of text</p>
<p>a paragrapg of text</p>
<p>a paragrapg of text</p>
<p>a paragrapg of text</p>
<p>a paragrapg of text</p>

How can I do it without making a text inclusion from a third file?

Nova Reticulis

2:34 pm on Mar 29, 2004 (gmt 0)

10+ Year Member



ssskaya, don't reinvent the wheel, seriously! Use Smarty [smarty.php.net]. You'll spend half a day playing with it and rewriting the templates but in the end, it will save you many a long hour.

ssskaya

3:20 pm on Mar 29, 2004 (gmt 0)

10+ Year Member



dear nova:

this is gonna be just for one section of the site which needs to have the same look as the other pages.

so I already have the template.php file.

All I need to know is how I can insert 5 paragraphs of text into $text without having to make a php inclusion for a third file.

ergophobe

7:37 pm on Mar 29, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You could read paragraph1.txt into a variable and then use that variable in your template as Coopster suggested.

Alternately, you could set up all your paragraphs and just include the template. That's usually my preference.

pg1.php
<?
$para1 = <<<PARA1
This is <span>paragraphs one</span> with html in it and everything
PARA1

$para23 = <<<PARA23
This is <span>paragraphs two and three</span> with html in it and everything
PARA23

include("T_default.php");
?>

file: T_default.php

<?
include("header.inc.php");

echo "<p>$para1</p>";
echo "<p>$para23</p>";

include("footer.inc.php");
?>

include

xeimm

4:50 am on Mar 30, 2004 (gmt 0)

10+ Year Member



HI.
Smarty template

HTMl file
<table width="760" border="0" align="center" cellpadding="4" cellspacing="1" bgcolor="#0066CC">
<tr>
<th height="25" colspan="2" bgcolor="#99CCFF" scope="col"><div align="left">{#$type#}</div></th>
</tr>
<tr>
<td width="150" rowspan="2" valign="top" bgcolor="#DAE9F5">{#include file=$search#} <p>&nbsp;</p></td>
<td valign="top" bgcolor="#DAE9F5"><div align="center">
<table width="100%" border="0" cellspacing="0" cellpadding="4">
<tr>
<th scope="col"><span class="style3">{#$title#}</span><br>
{#$date#} </th>
</tr>
</table>
</div></td>
</tr>
<tr>
<td valign="top" bgcolor="#DAE9F5">{#$content#}</td>
</tr>
</table>

php file

//开始smarty配置设置-------------------------------------------------------
include "class/Smarty.class.php";
define('__SITE_ROOT', 'd:\phpsite\adodbnews'); // 最後沒有斜線
$tpl = new Smarty();
$tpl->template_dir = __SITE_ROOT . "/page/";
$tpl->compile_dir = __SITE_ROOT . "/templates_c/";
$tpl->config_dir = __SITE_ROOT . "/configs/";
$tpl->cache_dir = __SITE_ROOT . "/cache/";
$tpl->left_delimiter = '{#';
$tpl->right_delimiter = '#}';
//结束smarty配置设置-------------------------------------------------------
$tpl->assign("title", $array_news["title"]);
$tpl->assign("content", $array_news["content"]);
$tpl->assign("date", $array_news["date"]);
$tpl->assign("typeid", $array_news["typeid"]);
$tpl->display('office/show_info.htm');