Current location: Hot Scripts Forums » Programming Languages » PHP » [SOLVED] help with template class


[SOLVED] help with template class

Reply
  #1 (permalink)  
Old 05-20-08, 09:57 PM
darkcarnival's Avatar
darkcarnival darkcarnival is offline
PHP/MySQL coder
 
Join Date: Jun 2003
Location: Michigan
Posts: 939
Thanks: 0
Thanked 0 Times in 0 Posts
Hazard [SOLVED] help with template class

hi,

I'm trying to get a template class of mine to work correctly, but during the part of the code that should be outputting the parsed data, I get this error:

Quote:
Warning: Invalid argument supplied for foreach() on line 60
now I'm using a foreach as the template will be working with loops and I'm trying to make it all work on one template file(meaning using one tremplate for a loop action not separating it into 3) as currently my template file count is at 160 and I wish to cut that down a bit for ease

now here is the template class I'm using:

PHP Code:

class templateEngine implements templateCore{


    
#declare data members
    
private $output;
    private 
$tags = array();
    private 
$templateFile;
    
    
#function that will open template file.
    
public function __construct($file){
        
#see if template file exists.
        
if (!file_exists($file)){
            throw new 
Exception('Template file ('.$file.') was not found.');
        }else{
            
#get the contents of the template file
            
$contents file_get_contents($file);
            
            
#see if template file is empty.
            
if(empty($contents)){
                throw new 
Exception('Template file is empty.');
            }else{
                
#Add template contents into output variable.
                
$this->output $contents;                
            }             
        }        
    }
    
    
#function that will parse template tags.
    
public function parseTags($tags){
    
        if(
count($tags)<1){
            throw new 
Exception('No tags were found to be parsed.');
        }
        
        foreach(
$tags as $tag=>$data){
            
#if data is array, traverse recursive array of tags
            
if(is_array($data)){
                
$this->output preg_replace("/\{$tag/",'',$this->output);
            }
                      
            
$this->output str_replace('{'.$tag.'}'$data$this->output);
        }    
    }
    
    
#function that will output the final set.
    
public function outputHtml(){
     
    
$html '';
        foreach(
$this->output as $val){
            
$html .= $val;
        }
        return 
$html;
    
    }    

I have tried several things and so far I haven't gotten far. I hope something here knows why this doesn't work. If you need any thing else, please let me know.

much thanks in advanced
__________________
Elite Bulletin Board
http://elite-board.us
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 05-21-08, 10:25 AM
UnrealEd's Avatar
UnrealEd UnrealEd is offline
Community Liaison
 
Join Date: May 2005
Location: Antwerp, Belgium
Posts: 3,165
Thanks: 4
Thanked 25 Times in 25 Posts
what exactly does your class do? does it "compile" the template files to php, or does it just parse them?

What line is line 60? There are 2 foreach loops in your code snippet, so both might cause the problem....

As for the problem itself: the reason why PHP threw this error is because the variabe you've passed on into the foreach language construct isn't either an array or an object (in your case either $tags or $this->output isn't an array or object)
__________________
"Good judgement comes from experience, and experience comes from bad judgement." - Fred Brooks

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 05-21-08, 12:17 PM
Keith's Avatar
Keith Keith is offline
Community Liaison
 
Join Date: Feb 2004
Posts: 1,232
Thanks: 1
Thanked 11 Times in 11 Posts
It looks to me like $this->output is a string as opposed to an array.

You get the contents from a template file as $this->output, then modify its value in a foreach() loop. But never is $this->output converted to an array.
__________________
The toxic ZCE
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 05-21-08, 05:01 PM
darkcarnival's Avatar
darkcarnival darkcarnival is offline
PHP/MySQL coder
 
Join Date: Jun 2003
Location: Michigan
Posts: 939
Thanks: 0
Thanked 0 Times in 0 Posts
what the class is suppose to do is read a template file and parse the tags.

what I want to have it do is allow loops to run without having to make additional template files and I figured the foreach would resolve that.

if anyone has a better solution, please give me your advice.
btw line 60 is this line:

PHP Code:

foreach($this->output as $val){ 

__________________
Elite Bulletin Board
http://elite-board.us

Last edited by darkcarnival; 05-21-08 at 05:09 PM.
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 05-21-08, 06:36 PM
Keith's Avatar
Keith Keith is offline
Community Liaison
 
Join Date: Feb 2004
Posts: 1,232
Thanks: 1
Thanked 11 Times in 11 Posts
Read what I said just prior to your last reply. You cannot iterate a value which is not an array.
__________________
The toxic ZCE
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 05-21-08, 06:48 PM
darkcarnival's Avatar
darkcarnival darkcarnival is offline
PHP/MySQL coder
 
Join Date: Jun 2003
Location: Michigan
Posts: 939
Thanks: 0
Thanked 0 Times in 0 Posts
hmm ok, alright.

now it does work without the foreach loop, only issue is, the loops don't work now

I really want to make this work(and i don't want to do the library route as most contain too much fluff or requires regular updates and I plan to use this on a programming project I work on)

does anyone have a recommendation on how I should go with this?
__________________
Elite Bulletin Board
http://elite-board.us
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 05-21-08, 10:23 PM
darkcarnival's Avatar
darkcarnival darkcarnival is offline
PHP/MySQL coder
 
Join Date: Jun 2003
Location: Michigan
Posts: 939
Thanks: 0
Thanked 0 Times in 0 Posts
hi,

had a while to think more on this and after doing some looking around I realized what I want the class to do.

basically I want the class to handle block iterations.

an example of this(in case you want to see it):

Code:
{tag.DBVALUE}
I believe phpbb 3 uses it in their script and I'd like to try something similar.

the other alternative(which I wish to avoid) is to just place php code in the template file itself, though this would break the idea of "separating the code from the design" concept I'm going with.

so going with block iterations is my next step, any idea how I can do this?

I really appreciate everyone's help
__________________
Elite Bulletin Board
http://elite-board.us
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 05-22-08, 11:06 PM
darkcarnival's Avatar
darkcarnival darkcarnival is offline
PHP/MySQL coder
 
Join Date: Jun 2003
Location: Michigan
Posts: 939
Thanks: 0
Thanked 0 Times in 0 Posts
hi,

just wanted to say thanks to those who have helped.

doing some research, I found what I was looking for to help me with my class.
__________________
Elite Bulletin Board
http://elite-board.us
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
improving my template class darkcarnival PHP 4 05-23-07 01:11 PM
conceptual problem: abstract class, interface or class UnrealEd Everything Java 2 05-12-07 08:30 AM
I need to pass this class Please Help! Negative6 Everything Java 1 05-03-07 07:55 AM
how to loop mysql into template system??? how u guys make it??? Eric Hadson PHP 0 09-19-05 11:43 PM
Good template system [PL]Greg PHP 3 06-28-03 05:09 AM


All times are GMT -5. The time now is 07:14 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.