Current location: Hot Scripts Forums » Programming Languages » PHP » How to go about creating template system?


How to go about creating template system?

Reply
  #1 (permalink)  
Old 06-08-04, 10:02 PM
Alan Alan is offline
Newbie Coder
 
Join Date: Dec 2003
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
How to go about creating template system?

Hi.

I'm hoping that some of you may be able to offer me some advice regarding how to go about creating a template system.

To cut a very long story short, I'm in the process of rewriting my website, and rather than place html and php code in the same files again, I would quite like to add a template system instead. I'd like to store the templates in a mySQL database and output them when required. The only thing that is essential in this system, is that the templates will interpret any variables inside them.

To try and make this a little clearer, I may like to run a mySQL query on my index page, which counts how many users are registered on the site and assign it to a variable, maybe named $totalusers. Then, somewhere in that pages template, I could write "Total Users Registered: $totalusers" which would output the correct number of registered users.

Alternatively, let's say that my index.php file checks to see if there are any active announcements in the mySQL database. In the index template, I would put $announcement where any such announcement should be shown on the page. Then, if the script finds an announcement, the announcement template is output in place of $announcement, within the index template. If not, $announcement is empty and is simply ignored.

Anyhow, I've spent most of the evening working on this, and I can't seem to get anything other than a blank white page when I try to output the template(s). This makes me think that I am on the wrong track, and I could therefore use some advice as to how I should go about doing this.

I'm certain that this is possible, as I've seen it done before. I just can't remember much about it. :-/

I'm sorry I don't have any code to show you, but I've tried so much this evening, that I've completely lost track of what I tried, where I tried it and so on.

However, I feel the best way to learn is through experience, so I am determined not to give up untill I have this system made and working how I need it. So, any advice on this topic would be very much appreciated.

Thank you.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 06-09-04, 05:49 AM
NeverMind's Avatar
NeverMind NeverMind is offline
Community VIP
 
Join Date: Aug 2003
Location: K.S.A
Posts: 2,257
Thanks: 0
Thanked 2 Times in 1 Post
you can do that with functions-based-templates!
you will have to use globals and IF statments alot!
I use this method in my website, a function like this to build the header:
PHP Code:

function HTMLHeader($title '') {

  
//if no title was passed, we set the default title      
    
if (empty($title))
           
$title 'Default Title';

        echo 
'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html dir="ltr">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1;" />
    <meta name="description" content="'
.$title.'" />
    <meta name="keywords" content="PHP, SQLite, blog, free, open, source, SFSB, simple, simplicity" />
    <title>'
.$title.'</title>
    <link rel="stylesheet" type="text/css" href="default.css" />
    <script language="JavaScript" type="text/javascript" src="JavaScript.js"></script>
</head>'
."
    <body>\n"
;

this was very simple because it doesn't need much !

now let me try to achive what you want with a little function:
PHP Code:

//UP HERE WE FETCH announmnets and totalusers

//and set them to the variables

function body () {
  
//we first grap all announcments and users
   
global $announcement$totalusers;

 
//only if we find contents in announcments we show them  
   
if (!empty($announcment)) {
      
//PROCESS AND PRINT ANNOUCNMENTs
   
}

 
//and this function is defined by you to grap news and show them ..
   
ShowNews();

  
//now we look for total users
   
if (!empty($totalusers)) {
     echo 
'Total Users Registered: '.$totalusers;
   }

this is the method I user and fine it very easy to maintain and reuse..

if you don't like this method, look at Smarty template engine: http://smarty.php.net/
__________________
PHPSimplicity
We don't need a reason to help people - Zidane [FF9]

Last edited by NeverMind; 06-09-04 at 05:54 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 06-09-04, 07:02 AM
Alan Alan is offline
Newbie Coder
 
Join Date: Dec 2003
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Hi.

Thanks for the help.

I'm going to have another attempt this afternoon and see what I can come up with.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 06-09-04, 07:28 PM
Klesti Klesti is offline
Newbie Coder
 
Join Date: May 2004
Location: Albania
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
You may use the phplib template class, is the better solution.
there are many tutorials on the web for this:

Read this article --> http://www.phpbuilder.com/columns/david20000512.php3

And than this --> http://www.devshed.com/c/a/PHP/PHPLib-Templates
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 06-10-04, 03:15 AM
nd2 nd2 is offline
Wannabe Coder
 
Join Date: Jun 2003
Posts: 128
Thanks: 0
Thanked 0 Times in 0 Posts
heres a heavly striped down version of what i use in my template engine.

Code:
class Parse {
var varindex = array();

function Parse ($tagstart = '<!$', $tagend = '$>') {
/* Set our class world variables*/
$this->tagstart = $tagstart;
$this->tagend = $tagend;
}

function RenderSTR($template) {
	return preg_replace("/".preg_quote($this->tagstart)."(.*?)".preg_quote($this->tagend)."/ise", "(isset(\$this->varindex['$1']) ? \$this->varindex['$1'] : '')", $template);
}

function SetVar($var_name, $var_value) {
$this->varindex[$var_name] = $var_value;
}
}
first you need to init the class & set the start and end tags with something like
$Parse = new Parse('<!$','$>');
tags for this syntex will be like <!$tag$>

now you can set new vars like.
$Parse->SetVar('title','This is my title');

you can then render it in to a page by using

$Parse->RenderStr('your template to render goes here.');

hopfully that helps you out.
__________________
IonCMS (Coming Soon.)
http://ioncms.com
--
Ncaster (Free php/mysql cms)
http://ncaster.cjb.net
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #6 (permalink)  
Old 06-11-04, 11:14 AM
Alan Alan is offline
Newbie Coder
 
Join Date: Dec 2003
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Hi again.

Thanks to you all for your help.

I've managed to write a small system by myself, which stores the templates in the database and outputs them when called by my gettemplate() function. However, it has me slightly confused. I use addslashes() on all my templates before inserting them into the database, but then get nothing but a blank page when I use stripslashes() after getting out again and prepared for output. Strangely, I have to use addslashes() again for them to work, so in effect, I'm using addslashes twice on the templates. Is this...normal?

Thanks.

Last edited by Alan; 06-11-04 at 11:28 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #7 (permalink)  
Old 06-11-04, 12:21 PM
Alan Alan is offline
Newbie Coder
 
Join Date: Dec 2003
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
I'm sorry for double posting, but I wanted to rewrite my last post now that I've worked something out. However, for some reason there's a 60 minute editing limit and the time has passed, so I have to post again.

Anyhows, I realise why I need to use addslashes() rather than stripslashes() before outputting the template. It's because the slashes aren't actually stored in the database. Sorry I sound like I newbie, but that's exactly what I am.

Anyway, I've now got everything working, except for two templates out of about 27. On these two templates, the slashes added by the addslashes() statement are interpreted with the rest of the html code for some reason, rather than ignored. This means that where I have specified something such as <td class="head"> in my template, the "normal" templates recognise this, but the two "broken" templates still try and interpret it as <td class=\"head\"> meaning I get a standard "Times New Roman" font instead of my customised bold Arial.

As I said, this only happens on 2 templates and the other 25 are fine, which is why I am confused.

Does anyone know what may be causing this, I could, once again, really use some advice.

Thank you.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #8 (permalink)  
Old 06-12-04, 05:14 AM
nd2 nd2 is offline
Wannabe Coder
 
Join Date: Jun 2003
Posts: 128
Thanks: 0
Thanked 0 Times in 0 Posts
the database shoudent remove the slashes as far as i know, mysql defintly doesnt well at least from the versions i have tested.

to store you addslashes() once then stripslashes() to display.
__________________
IonCMS (Coming Soon.)
http://ioncms.com
--
Ncaster (Free php/mysql cms)
http://ncaster.cjb.net
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #9 (permalink)  
Old 06-12-04, 08:45 AM
Rapid Dr3am Rapid Dr3am is offline
Community VIP
 
Join Date: Jan 2004
Location: Liverpool, England
Posts: 752
Thanks: 0
Thanked 0 Times in 0 Posts
IF you decide to give up trying to make your own, you can use phpTemplater.
__________________
Placeholder, place signature here.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #10 (permalink)  
Old 06-12-04, 11:37 AM
Alan Alan is offline
Newbie Coder
 
Join Date: Dec 2003
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Thank you once again for your replies.

This is indeed a strange problem. The funny thing is that it works perfectly on localhost, just not on my actual site. The only difference is the PHP version, which is older on localhost. :-/

I would rather not give up on writing my own, I've enjoyed learning PHP so far and want to continue, and I think experiences such as this are the best way to learn. Even so, thank you for the suggestion Rapid Dr3am.

It seems I have a strange understanding of the functions addslashes() and stripslashes(), but I'm going to continue playing and see if I can eventually fix this problem.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Creating a METAR system tbig ASP 0 01-31-04 12:28 PM
Need Epinions-lite system in PHP & MYSQL wali001 Job Offers & Assistance 4 01-12-04 07:02 AM
High-Quality Template, .PSD +.PNG Fireworks, US$34.99, corporate/business template gwx General Advertisements 0 01-09-04 11:23 PM
Link system TAK PHP 3 08-17-03 12:17 PM
Good template system [PL]Greg PHP 3 06-28-03 05:09 AM


All times are GMT -5. The time now is 09:03 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.