View Full Version : Writing a Template System in PHP
tomyknoker
03-26-2007, 08:30 AM
I don't want to use SMARTY would it be a stupid idea to write my own code from scratch? Are there any tutorials on this?
djr33
03-26-2007, 08:52 AM
<?php
echo "<html>.............";
echo $database_variables;
echo "......... ..</html>";
?>
There ya go.
Just expand on that.
Check out php.net for specific function reference, and http://php-mysql-tutorial.com for a general overview of using databases with PHP.
EDIT: Actually, two things I should add--
You don't need to use echo like that.
Here are two possibly better options:
<?php ... ?>
html here like normal
<?php
echo $vars;
?>
more html
...etcOR
<?php
echo <<<OUT
This is actually
a string
but even line breaks and " and '
are ok, since it's a special way to hold a string.
It starts with <<<NAME (then a line break)
then you keep going with it and end with:
NAME; (must be the only thing on the line, no tabs, etc.)
Also, you can use variables in this
(unlike plain html)
do this: {$var}, and it will replace that
with the value of $var, including the {}.
And... that's it for this method...
OUT;
?>
shachi
03-26-2007, 03:23 PM
Or, if you want a complex templating system, here are a few tutorials:
http://www.phpfreaks.com/tutorials/70/0.php
http://codewalkers.com/tutorials/58/1.html
djr33
03-27-2007, 02:31 AM
Sure. What I have above is very basic, but it IS really the basic framework of any system.
Also, something important to remember is that you could store template html (standard stuff on each page) in the database AND individual content for each page, so those database variables would cover the template and the content both, with anything specific to the page not in the database in the php itself.
Using ifs and such, you can create a one-php-files site with many "pages" based on GET variables (?page=3) then you can control how it is all output.
In that case, you might just store the system wide template on the page, such as CSS, etc., then just get stuff from the database and you're set.
That's how I've made some cool pages. Easy to modify later, too.
tomyknoker
03-27-2007, 04:06 AM
Heya dj well say I have this page, it's pretty simple but how would I make it so it works better, and I keep the php seperate to the html
<?php
/* connect to the mysql database and use different queries for the count of members */
include 'library/config.php';
include 'library/opendb.php';
//navigation
include("nav.php");
//approved
$a = mysql_query("SELECT * FROM `tblmembers` WHERE `MemberApproved`='A'");
$aCount = mysql_num_rows($a);
//deleted
$d = mysql_query("SELECT * FROM `tblmembers` WHERE `MemberApproved`='D'");
$dCount = mysql_num_rows($d);
//on hold
$h = mysql_query("SELECT * FROM `tblmembers` WHERE `MemberApproved`='H'");
$hCount = mysql_num_rows($h);
//pending
$p = mysql_query("SELECT * FROM `tblmembers` WHERE `MemberApproved`='P'");
$pCount = mysql_num_rows($p);
//not sure
$n = mysql_query("SELECT * FROM `tblmembers` WHERE `MemberApproved`='N'");
$nCount = mysql_num_rows($n);
//rejected
$r = mysql_query("SELECT * FROM `tblmembers` WHERE `MemberApproved`='R'");
$rCount = mysql_num_rows($r);
/* Make the layout */
?>
<link href="cs_style.css" rel="stylesheet" type="text/css" />
<table border="1" cellpadding="3" cellspacing="1">
<tr valign="top">
<td colspan="2">Membership Status</td>
</tr>
<tr valign="top">
<td>Status</td>
<td>Total</td>
</tr>
<tr valign="top">
<td>Approved</td>
<td><a href="showMembers.php?cat=a"><?php echo $aCount;?></a></td>
</tr>
<tr valign="top">
<td>Deleted</td>
<td><a href="showMembers.php?cat=d"><?php echo $dCount;?></a></td>
</tr>
<tr valign="top">
<td>On Hold</td>
<td><a href="showMembers.php?cat=h"><?php echo $hCount;?></a></td>
</tr>
<tr valign="top">
<td>Pending</td>
<td><a href="showMembers.php?cat=p"><?php echo $pCount;?></a></td>
</tr>
<tr valign="top">
<td>Not sure</td>
<td><a href="showMembers.php?cat=n"><?php echo $nCount;?></a></td>
</tr>
<tr valign="top">
<td>Rejected</td>
<td><a href="showMembers.php?cat=r"><?php echo $rCount;?></a></td>
</tr>
<tr valign="top">
<td>Total</td>
<td><?php echo $aCount + $dCount + $hCount + $pCount + $nCount + $rCount;?></a></td>
</tr>
</table>
djr33
03-27-2007, 05:23 AM
That looks just fine to me. Not really sure what you want. You can't seperate the php and html as the php must generate the html.
You could move the code around some, but that looks just fine.
Sparaker
03-27-2007, 05:28 AM
If you want to totally seperate the html and the php then you should create your html pages seperately and where ever you need php to insert values like for example in your code above your using <?php echo $aCount;?> since the html code is in a php file and php will replace that code with the string value. What you could do is that you could write something like {aCount} there instead of that code and then let php read the html file and replace the {} with the variables and then echo everything. This way you'll have php and html totally seperated from each other ;)
I'd give you a full detailed example once i get back from school. Just let me know that you want it.
tomyknoker
03-27-2007, 06:40 AM
Hi Sparaker, yes that would be great if you could do it thanks a million!
Heya dj... maybe I'm confusing myself I thought that with a php templating system the html and php were kept completely seperate...
djr33
03-27-2007, 07:23 AM
"templating" is just as vague/general as saying "using a database", really. I can only help how I think I would do it, though there are many ways.
You could try to seperate them, but I think that's not gonna help much.
The template you have is fine.
One thing you could try is to include the template html in the database too, if you think that would help.
mburt
03-27-2007, 10:24 AM
To put it in "real" terms, I use it for my site. I have the main page, and everything else is completely based on it. I use page includes for each page when the "p" variable is used accordingly.
TerrorKalle
03-27-2007, 12:54 PM
Heya dj well say I have this page, it's pretty simple but how would I make it so it works better, and I keep the php seperate to the html
<?php
/* connect to the mysql database and use different queries for the count of members */
include 'library/config.php';
include 'library/opendb.php';
//navigation
include("nav.php");
//approved
$a = mysql_query("SELECT * FROM `tblmembers` WHERE `MemberApproved`='A'");
$aCount = mysql_num_rows($a);
//deleted
$d = mysql_query("SELECT * FROM `tblmembers` WHERE `MemberApproved`='D'");
$dCount = mysql_num_rows($d);
//on hold
$h = mysql_query("SELECT * FROM `tblmembers` WHERE `MemberApproved`='H'");
$hCount = mysql_num_rows($h);
//pending
$p = mysql_query("SELECT * FROM `tblmembers` WHERE `MemberApproved`='P'");
$pCount = mysql_num_rows($p);
//not sure
$n = mysql_query("SELECT * FROM `tblmembers` WHERE `MemberApproved`='N'");
$nCount = mysql_num_rows($n);
//rejected
$r = mysql_query("SELECT * FROM `tblmembers` WHERE `MemberApproved`='R'");
$rCount = mysql_num_rows($r);
/* Make the layout */
?>
<link href="cs_style.css" rel="stylesheet" type="text/css" />
<table border="1" cellpadding="3" cellspacing="1">
<tr valign="top">
<td colspan="2">Membership Status</td>
</tr>
<tr valign="top">
<td>Status</td>
<td>Total</td>
</tr>
<tr valign="top">
<td>Approved</td>
<td><a href="showMembers.php?cat=a"><?php echo $aCount;?></a></td>
</tr>
<tr valign="top">
<td>Deleted</td>
<td><a href="showMembers.php?cat=d"><?php echo $dCount;?></a></td>
</tr>
<tr valign="top">
<td>On Hold</td>
<td><a href="showMembers.php?cat=h"><?php echo $hCount;?></a></td>
</tr>
<tr valign="top">
<td>Pending</td>
<td><a href="showMembers.php?cat=p"><?php echo $pCount;?></a></td>
</tr>
<tr valign="top">
<td>Not sure</td>
<td><a href="showMembers.php?cat=n"><?php echo $nCount;?></a></td>
</tr>
<tr valign="top">
<td>Rejected</td>
<td><a href="showMembers.php?cat=r"><?php echo $rCount;?></a></td>
</tr>
<tr valign="top">
<td>Total</td>
<td><?php echo $aCount + $dCount + $hCount + $pCount + $nCount + $rCount;?></a></td>
</tr>
</table>
Your code could take long time to execute on rather larger boards, so in your queries use COUNT(*), like:
<?php
function sql_error()
{
$errstr = mysql_error();
if(!empty($errstr)
{
trigger_error($errstr, E_USER_ERROR);
}
}
function count_table($table, $wherestr = '')
{
/** Send query */
$query = @mysql_query("SELECT COUNT(*) FROM `" . $table . "`" . (($wherestr) ? ' ' . ' WHERE ' . $wherestr)) or sql_error();
/** Get count */
$count = @mysql_fetch_array($query);
$count = $count[0];
/** Free query result */
@mysql_free_result($query);
$query = null;
/** Return count */
return($count);
}
$membersR= count_table('members', '`memberApproved` = \'R\'');
?>
boxxertrumps
03-27-2007, 12:59 PM
here's what i use...
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"><head>
<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=utf-8"/>
<title>BoxxerTrumps</title>
<link rel="shortcut icon" href="http://boxxer.mooo.com/img/favicon.ico" />
<link rel="stylesheet" type="text/css" href="http://boxxer.mooo.com/styles/css.css"/>
<?php include "includes/functions.php"; ?>
</head><body>
<div id="cont">
<div id="left"><div id="ltop"></div><div id="lmid">
<?php include "includes/wrapper.php"; ?>
</div><div id="lbot"></div></div>
<?php include "includes/top.php"; include "includes/contain.php"; ?>
<?php include "includes/extra.php"; ?>
</div>
<div id="ad">
footer section, static...(ads, ceative commons notice.)
</div></body></html>
Whenever i change layouts, its always easy because i seperate the layout and styles and information from eachother...
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.