Click to See Complete Forum and Search --> : Template Class:::Help ME
PlaGuE
Sep 15th, 2005, 01:53 PM
okay i have this template class.
<?php
class Page
{
var $page;
function Page($tpl_file = "testy.php") {
$template = "$tpl_file";
if (file_exists($template))
$this->page = join("", file($template));
else
die("Template file $template not found.");
}
function parse($file) {
ob_start();
include($file);
$buffer = ob_get_contents();
ob_end_clean();
return $buffer;
}
function replace_tags($tags = array()) {
if (sizeof($tags) > 0)
foreach ($tags as $tag => $data) {
$data = (file_exists($data)) ? $this->parse($data) : $data;
$this->page = eregi_replace("{" . $tag . "}", $data,
$this->page);
}
else
die("No tags designated for replacement.");
}
function output() {
echo $this->page;
}
}
?>
require_once("lib/parser.php");
$page = new Page("templates/test/index.body.php");
$page->replace_tags(array(
"title" => "HOME",
"added_by" => "$row[author]",
"added_on" => "$row[date]",
"begin_cat_row" => "?????",
"end_cat_row" => "????"
));
$page->output();
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<th scope="col">Title</th>
<th scope="col"> Added on</th>
<th scope="col">Added by</th>
</tr>
<!--- {begin_cat_row}-->
<tr>
<th scope="col">{title}</th>
<th scope="col">{added_on}</th>
<th scope="col">{added_by}</th>
</tr>
<!--- {end_cat_row}-->
</table>
how. would i be able to 'loop' JUST
<!--- {begin_cat_row}-->
<tr>
<th scope="col">{title}</th>
<th scope="col">{added_on}</th>
<th scope="col">{added_by}</th>
</tr>
<!--- {end_cat_row}-->
This would save a hell of a lot of time .... n skinning and in coding.
Tho if anyone found a better example for this... plz tell me.
visualAd
Sep 16th, 2005, 02:41 AM
I use a templating system on two of the sites I've made. One site is my home page and I actually use PHP for the templates. I do this by having a template class:
<?php
// template.php
class Template
{
var $path;
function Template($path = null)
{
$this->path = $path;
}
function display()
{
@include($this->path);
}
}
?>
The templates themselves are just PHP files containing HTML and PHP, with the difference being that they use PHP's alternate templating (http://uk.php.net/manual/en/control-structures.alternative-syntax.php) like syntax for control structures such as loops. A simple template may look like this:
<?php // template.tpl.php ?>
<html>
<head>
<title><?php echo($this->title) ?></title>
</head>
<body>
<h1>My Friends</h1>
<?php foreach($this->friends as $friend): ?>
<p><?php echo($friend) ?></p>
<?php endforeach; ?>
</body>
</html>
A simple script which uses this template looks like this.
<?php
// index.php
require_once 'template.php';
$template = new Template('template.tpl.php');
/* assign some variables */
$template->title = 'Custom Title';
$template->friends = Array('me', 'graham', 'mr burns', 'bart');
/* now display the template */
$template->display();
?>
It is worth noting that this method does require some discipline and consistency in approach to the templates. It is ideal and very elegant for projects where there are only a small number of people, all who know the language.
However, there are situations where it may be necessary to allow other people access to the output layer of your web site. These people are not necessarily PHP developers and allowing them to modify PHP files directly could interfere with other layers of your application. Fortunately the Smarty (http://smarty.php.net/) project addresses this problem. It is a very powerful templating system written in PHP, for PHP and uses the same syntax as you are using in the templates, in addition to supporting looping and logical control structures. The template above written for Smarty would look like this:
{* template.tpl *}
<html>
<head>
<title>{$title}</title>
</head>
<body>
<h1>My Friends</h1>
{foreach from="$friends item="friend"}
<p>{$friend}</p>
{/foreach}
</body>
</html>
The PHP code to display the template is also very similar. Note that the SmartyTemplate class is an inherited version of the standard smarty class, this is described here (http://smarty.php.net/manual/en/installing.smarty.extended.php).
<?php
// index.php
require_once 'template.php';
// a special class for smarty
$template = new SmartyTemplate;
/* assign some variables */
$template->assign('title', 'Custom Title');
$template->assign('friends', Array('me', 'graham', 'mr burns', 'bart'));
/* now display the template */
$template->display('template.tpl';);
?>
Smarty compiles all templates into HTML and as such it is very fast, it separates the output layer of your application completely and securely from the PHP code.
I did once try to write my own templating language, but it is very complicated, especially when you want to start matching nested statements of varying depths. So, I abandoned the idea and used smarty for one of my sites - why reinvent the wheel? ;)
PlaGuE
Sep 16th, 2005, 04:27 PM
uhh....sorry... me no understandy...lol...
This is confusing... maybe a lil more lamens terms should help.. lol.
visualAd
Sep 16th, 2005, 05:16 PM
What don't you understand?
PlaGuE
Sep 16th, 2005, 09:08 PM
well.. it all really confuses me...
all i am trying to do is... make a part loop... i guess once i figure out how to make {begin_cat_row} = while($row=mysql_fetch_array($result)):
i may be okay...
I work better with an example i can destroy n put back together...
taht example kinda left me in the dark.
sorry to say.
PlaGuE
Sep 17th, 2005, 02:08 AM
meh... ive decided... to use(AND LEARN) PHPBB's template class.
theres even sum topics on it.
visualAd
Sep 17th, 2005, 03:00 AM
What you want is to introduce the ability to traverse a an array in your template right?
Unfortunately the the system you are using only substitutes variables using a simple search and replace on the template file. Therefore you will have to rewrite the substitution function to enable looping through an array. Doing this would be quite a complex task however, one that you need not do because it has already been done :)
In my reply I went on to explain two alternatives to the system you are already using. One alternative is to use PHP templates. If you save the following class in a file called tmeplate.php then include it in your script.
<?php
// template.php
class Template
{
var $path;
function Template($path = null)
{
$this->path = $path;
}
function display()
{
@include($this->path);
}
}
?>
Now change the code in you script:
require_once("template.php");
$page = new Template("templates/test/index.body.php");
$page->title = "HOME";
$page->added_by $row[author];
$page->added_on => $row[date],
$page->display();
Now change your template file to this:
<?php // templates/test/index.body.php ?>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<th scope="col">Title</th>
<th scope="col"> Added on</th>
<th scope="col">Added by</th>
</tr>
<!--- {begin_cat_row}-->
<tr>
<th scope="col"><?php echo($this->title) ?></th>
<th scope="col"><?php echo($this->added_on) ?></th>
<th scope="col"><?php echo($this->added_by) ?></th>
</tr>
<!--- {end_cat_row}-->
</table>
OK - so that is you template system changed to PHP. Now you can add a loop. The easiest way of doing this is to load the rows you want into an array and add this array to the template.
Replace your original code which assigns the variables with this loop which loads the result set into an array.
$cat_array = array() // initialize the array as empty
while($row=mysql_fetch_array($result) {
$cat_array[] = $row; // add the row to the array
}
$page->cat_array = $cat_array; // assign the array to the template
Now all you have to do is modify the template:
<?php foreach($this->cat_array as $category): ?>
<!--- {begin_cat_row}-->
<tr>
<th scope="col"><?php echo($category['title']) ?></th>
<th scope="col"><?php echo($category['added_on']) ?></th>
<th scope="col"><?php echo($category['added_by']) ?></th>
</tr>
<!--- {end_cat_row}-->
<?php endforeach; ?>
This should now display each row in the array.
PlaGuE
Sep 17th, 2005, 04:25 AM
the thing is... im trying to stray away from the PHP... hences why the template files are .tpl.. cuz when a user wants to customize the look... I dont want them seeing all this php...
Will give the stuff a try tho.
visualAd
Sep 17th, 2005, 04:27 AM
This is why I also suggested Smarty. It does exactly what you are asking for and there is no risk of the person editing the templates messing up the PHP code.
PlaGuE
Sep 17th, 2005, 06:24 AM
well... im either gunna use smarty...
or PHPBB's template system(class).
I mean why not sue phpbb's. ... i might be making my script intergratable with PHPBB...
idk yet... i havent discussed this with my friend yet.
Thanks for your help.
I appreciate your help.
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.