Quick Login   
 
Register AdminFusion Tutorials
 
Featured Sponsors


One.com Domain and Hosting


Register
Forum of the Month
Australian Webmaster
fotm

A webmaster forum specifically catering for Australian site owners. We discuss site development, marketing and management issues.

Tag Cloud
Latest Threads
Forum Stats
8,063 Members
165,817 Posts
25 Users Online

Please welcome our newest member, conorod!

Affiliates
Go Back AdminFusion » The Break Room » Off Topic » PHP Template Engine Help
Welcome to the AdminFusion. AdminFusion is the ultimate resource for forum administrators and moderators. With exclusive articles, interviews with the experts, free downloadable skins, and the revolutionary post exchange system - PostFusion, AdminFusion is the place to go for all of your forum needs.  By joining AdminFusion, you will become part of a thriving admin community and immediately gain access to all of these resources. Registration is fast, simple and absolutely free so please join us today!
Want more than our forums? Try these: Post Fusion Forum Matrix
Old 02-05-2006, 11:20 AM   #1

illusion's Avatar


Title: Apprentice

Points: 3,505, Level: 17Points: 3,505, Level: 17Points: 3,505, Level: 17
Level up: 18%, 345 Points neededLevel up: 18%, 345 Points neededLevel up: 18%, 345 Points needed
Activity: 0%Activity: 0%Activity: 0%

Join Date: Oct 2005

Posts: 481

illusion is on a distinguished road
Send a message via AIM to illusion Send a message via MSN to illusion Send a message via Yahoo to illusion  
 
PHP Template Engine Help

Hi,

I am coding my new site and I need to create a template engine because my coding gets really inefficient and looks real messy.

Anyway, my problem is that when I enter a variable to be substituted in the template, such as {TITLE} for Moozica, it will only do one. I can put in 50 variables with 50 data bits assigned to them and only the last one will get substituted.

I am trying to use OOP here as I think it is efficient, so don't comment on that. Just the code, which I have included below.

template.php

PHP Code:
<?php

class template
{

    var 
$ouput;    
    
    function 
parseFile$file )
    {
    
        if (
file_exists($file))
        {
        
            
$file $file;
        
        }
        else
        {
        
            
$file "error.tpl";
        
        }
    
        return 
file_get_contents($file);
    
    }
    
    function 
parseTemplate$templ$vars )
    {
    
        
$file $this->parseFile($templ);
        
        foreach (
$vars as $variable => $data)
        {
        
            
$variable '{' $variable '}';
            
$this->output str_replace($variable$data$file);
            print 
$this->output;
        
        }
        
    }
    
    function 
display()
    {
    
        
// return $this->output;
    
    
}
    
}

?>
index.php

PHP Code:
<?php

include ("includes/template.php");

include (
"includes/func.php");

$template = new template;

$vars = array('CONTENT' => 'Testing',
'TITLE' => 'Woot');

$file $template->parseTemplate('templates/index.tpl'$vars);

print 
$template->display();

?>
Now the files func.php only has a mysql_connect function in it. And index.tpl just has the html for a normal page with CONTENT and TITLE.

Can somebody help me?
__________________
Well I thought it was cool
Reply With Quote
Old 02-05-2006, 04:45 PM   #2

LarryB's Avatar

Title: AF Lead Developer

Points: 6,450, Level: 23Points: 6,450, Level: 23Points: 6,450, Level: 23
Level up: 24%, 100 Points neededLevel up: 24%, 100 Points neededLevel up: 24%, 100 Points needed
Activity: 0%Activity: 0%Activity: 0%

Join Date: Sep 2005

Posts: 1,186

Location: OHIO, US

LarryB has much to be proud ofLarryB has much to be proud ofLarryB has much to be proud ofLarryB has much to be proud ofLarryB has much to be proud ofLarryB has much to be proud ofLarryB has much to be proud ofLarryB has much to be proud of
Send a message via MSN to LarryB  
 
I'm sorry if this isn't constructive as to helping you write your own engine, but I have always been a fan of not re-inventing the wheel. There are several really good opensource engines out, some are bulky and complicated others are simpler. Below is a link to my personal favorite.

http://templatepower.codocad.com/
__________________
Do not post your PF private key in public.

Did I help you with this post? If so, pls click the rep button or send money.
Reply With Quote
Old 02-05-2006, 04:51 PM   #3

LarryB's Avatar

Title: AF Lead Developer

Points: 6,450, Level: 23Points: 6,450, Level: 23Points: 6,450, Level: 23
Level up: 24%, 100 Points neededLevel up: 24%, 100 Points neededLevel up: 24%, 100 Points needed
Activity: 0%Activity: 0%Activity: 0%

Join Date: Sep 2005

Posts: 1,186

Location: OHIO, US

LarryB has much to be proud ofLarryB has much to be proud ofLarryB has much to be proud ofLarryB has much to be proud ofLarryB has much to be proud ofLarryB has much to be proud ofLarryB has much to be proud ofLarryB has much to be proud of
Send a message via MSN to LarryB  
 
As for your actual problem. It is happening because the input variable $file is never being updated. So it is grabbing a fresh copy of the $file data every time, thus removing ur changes. Below is a copy with changes I would make to fix it.

PHP Code:
<?php 
 
class template 

 
var 
$ouput
 
function 
parseFile$file 

 
if (
file_exists($file)) 

 
$file $file
 

else 

 
$file "error.tpl"
 

 
return 
file_get_contents($file); 
 

 
function 
parseTemplate$templ$vars 

 
$file $this->parseFile($templ); 
 
foreach (
$vars as $variable => $data

 
$variable '{' $variable '}'
$file str_replace($variable$data$file); 
 

 
$this->output $file;
print 
$this->output

 
function 
display() 

 
// return $this->output; 
 

 

 
?>
__________________
Do not post your PF private key in public.

Did I help you with this post? If so, pls click the rep button or send money.
Reply With Quote
Old 02-06-2006, 06:50 AM   #4

illusion's Avatar


Title: Apprentice

Points: 3,505, Level: 17Points: 3,505, Level: 17Points: 3,505, Level: 17
Level up: 18%, 345 Points neededLevel up: 18%, 345 Points neededLevel up: 18%, 345 Points needed
Activity: 0%Activity: 0%Activity: 0%

Join Date: Oct 2005

Posts: 481

illusion is on a distinguished road
Send a message via AIM to illusion Send a message via MSN to illusion Send a message via Yahoo to illusion  
 
Thanks LarryB. I shall test it out tonight if I can
__________________
Well I thought it was cool
Reply With Quote
Old 02-06-2006, 07:46 AM   #5

LarryB's Avatar

Title: AF Lead Developer

Points: 6,450, Level: 23Points: 6,450, Level: 23Points: 6,450, Level: 23
Level up: 24%, 100 Points neededLevel up: 24%, 100 Points neededLevel up: 24%, 100 Points needed
Activity: 0%Activity: 0%Activity: 0%

Join Date: Sep 2005

Posts: 1,186

Location: OHIO, US

LarryB has much to be proud ofLarryB has much to be proud ofLarryB has much to be proud ofLarryB has much to be proud ofLarryB has much to be proud ofLarryB has much to be proud ofLarryB has much to be proud ofLarryB has much to be proud of
Send a message via MSN to LarryB  
 
Just realized I made a small mistake and have updated the file again.
__________________
Do not post your PF private key in public.

Did I help you with this post? If so, pls click the rep button or send money.
Reply With Quote
Old 02-06-2006, 07:50 AM   #6

illusion's Avatar


Title: Apprentice

Points: 3,505, Level: 17Points: 3,505, Level: 17Points: 3,505, Level: 17
Level up: 18%, 345 Points neededLevel up: 18%, 345 Points neededLevel up: 18%, 345 Points needed
Activity: 0%Activity: 0%Activity: 0%

Join Date: Oct 2005

Posts: 481

illusion is on a distinguished road
Send a message via AIM to illusion Send a message via MSN to illusion Send a message via Yahoo to illusion  
 
but I could just use display couldn't I? I could just use

PHP Code:
<?php 
 
class template 

 
var 
$ouput
 
function 
parseFile$file 

 
if (
file_exists($file)) 

 
$file $file
 

else 

 
$file "error.tpl"
 

 
return 
file_get_contents($file); 
 

 
function 
parseTemplate$templ$vars 

 
$file $this->parseFile($templ); 
 
foreach (
$vars as $variable => $data

 
$variable '{' $variable '}'
$file str_replace($variable$data$file); 
 

 
$this->output $file;
 

 
function 
display() 

 
print 
$this->output
 

 

 
?>
Just use the display function?
__________________
Well I thought it was cool
Reply With Quote
Old 02-06-2006, 03:45 PM   #7

LarryB's Avatar

Title: AF Lead Developer

Points: 6,450, Level: 23Points: 6,450, Level: 23Points: 6,450, Level: 23
Level up: 24%, 100 Points neededLevel up: 24%, 100 Points neededLevel up: 24%, 100 Points needed
Activity: 0%Activity: 0%Activity: 0%

Join Date: Sep 2005

Posts: 1,186

Location: OHIO, US

LarryB has much to be proud ofLarryB has much to be proud ofLarryB has much to be proud ofLarryB has much to be proud ofLarryB has much to be proud ofLarryB has much to be proud ofLarryB has much to be proud ofLarryB has much to be proud of
Send a message via MSN to LarryB  
 
yes. I was just using the code that you had in the original file. Wasn't looking further than that.
__________________
Do not post your PF private key in public.

Did I help you with this post? If so, pls click the rep button or send money.
Reply With Quote
Reply



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

 
Posting Rules

Similar Threads
Thread Thread Starter Forum Replies Last Post
Yahoo Lays Out Welcome Mat For PHP Devs shellspeare Graphics and Design 0 03-01-2006 09:08 AM
php Resource List gprime Graphics and Design 6 01-24-2006 05:01 PM

AdminFusion

All times are GMT +1. The time now is 05:06 AM. Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.1.0 © 2005-2008 AdminFusion - All Rights Reserved


From:
Title:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72