View Full Version : Template Problem - plz help

04-07-2004, 04:31 PM
hi everyone,

plz help me,
I am not able to figure out that that how can i use templates with php.
I dont want to use smarty templating system,

Is there any other way. I am not able to find tutorial on the net.

Plz help ASAP

gaurav Behl

04-07-2004, 05:05 PM
Why don't you want to use smarty? There's fasttemplate you could use, otherwise you're gonna have to script one yaself

04-07-2004, 07:07 PM
www.phpfreaks.com has some good tutorials on classes.

04-07-2004, 07:29 PM
:confused: What have classes got to do with template parsing?

04-07-2004, 09:27 PM
I thought by templates he meant classes :confused:

04-08-2004, 02:26 AM
simplest template setup ....


<div class="paragraph">
<div class="title">

$tpl = implode( '' , file( 'template.tpl' ) ) ;
$search=array( '{title}' , '{para1_title}' , '{para_1}' ) ;
/*replace could come from your database/wherever*/
'welcome to the template' ,
'All your templates' ,
'belong to PHP blah blah'
) ;
$tpl = str_replace( $search , $replace , $tpl ) ;
echo $tpl ;

there are a thousand ways & variations , but simplistically you take a template and within that template put placeholders (above I use {placeholder}) , then you parse the template replacing the placeholders with your content.

Now templates often require other forms of logic such as looping , e.g. you may have a variable number of paragraphs above and do not want to have to put all those placeholders in the template , rather loop through them.

There are several approaches to this from putting code/pseudo-code in your template (which I personally do not like (e.g. defeats the object)) or make your template parser smarter , or , I often have several templates for a single page anything that requires a loop or other logic is parsed seperately and then I stick all the bits together, this can be useful for standardising content over several pages and not having to have several templates that have large sections that essentially do the same thing .

so have a play with the above type of methodology for now as further expansion on templating methods is very subjective and the cause of many a fight ;)

04-08-2004, 07:09 AM
What about using ob_start() and ob_end_flush()?? I have my template, and then only in a special part of my template I wana have all my pages to appear. So, I print a PHP variable in that part of the template, and then in all my pages I use ob_start(callback) & ob_end_flush() to get all the output of my page and then I put it in that variable. After that I include that template, because the template outputs the variable, then everything will be printed in its place. How's this approach?

04-08-2004, 08:11 AM
Its fine , I played with that once after seeing a post in the manual notes , but I got bogged down when I had to iterate over some sections , so I gave up and went back with what I knew , I am sure that there are ways around that since PHP lets you work with several buffers at once .. me just too lazy ;)

I also tried eval() for templates as well (I think thats how VB works ?) but with similar issues and nested eval()'s scare me~

04-09-2004, 08:03 AM
Yes, eval is very scary and I don't think you need to put all your php commands inside a variable too.

About that ob_start() thing, one of my friends was telling me I shouldn't use it because it opens lots of streams and samples of my program and makes the server busy, but I couldn't see any down side, coz my script was running fairly fast. The only problem I have is that sometimes (just sometimes!) when I have some fatal errors in my page it can't show it. I mean I receive a blank page, it's because the ob_end_flush() doesn't get excecuted and it doesn't call my callback, only in that time I have to remove ob_start() and see my naked page (wihtout template!) and find out the error. Of course it doesn't happen that much. But it's really a great thing having templates! How easy the work becomes and how many copy & pasting you can save. I don't think I can ever code without templates!

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum