Current location: Hot Scripts Forums » Programming Languages » PHP » improving my template class


improving my template class

Reply
  #1 (permalink)  
Old 05-22-07, 09:04 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
improving my template class

hello,

i need some assistance in improving my template class to allow the use of loops.

currently it does not and i am stuck on getting it to work with loops.

an example of what i want to try:

Code:
the following users have been here for a year or longer: {users}
now thats a very basic example, but i think you get the point.

here is the template class i use, if you need anything else please let me know

PHP Code:

class template

{
  var 
$page;

  function 
template($template) {
    if (
file_exists($template))
      
$this->page join(""file($template));
    else
      die(
"Template file $template was not found.");
  }

   function 
parse($file) {
    
ob_start();
    include(
$file);
    
$buffer ob_get_contents();
    
ob_end_clean();
    return 
$buffer;
  }

  function 
replace_tags($tags = array()) {
    if (
sizeof($tags) > 0){
      foreach (
$tags as $tag => $data) {
        
$this->page eregi_replace("{" $tag "}"$data,
                      
$this->page);
        }
    }else{
      die(
"No tags designated for replacement.");
    }
  }

  function 
output() {
    echo 
$this->page;
  }

much thanks in advance
__________________
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-23-07, 03:08 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
do you mean loops, or arrays? your example suggests using arrays.
If you use loops (smarty-loops), they look something like this:
smarty Code:
  1. {section loop=$users name=i}
  2. <b>{$users[i]}</b><br/>
now the output will be something like this:
Code:
<b>UnrealEd</b><br/>
<b>darkcarnival</b><br/>
What exactly do you want the output to be of the example you gave? it might be easier just to check if it's an array and implode the array.
Creating template-loops isn't easy, it took me about 3 days to write a for-loop. There's a lot of regex parsing, and nested for-loops to get the actual php result.

How does your template class works? as far as i can see, you read the file into the $page field, and then replace the tags, everytime you visit the page, right? This is gonna work pefectly untill your templates become very large, then it's better to switch to a template-engine, such as smarty, that will store a php version of the template file

hope i helped you a little further
__________________
"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-23-07, 08:51 AM
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,

i should explain this a bit better.

it can handle loops somewhat, but i'd like it to perform it like the way you described.

a real good example of what i want done is having a table be dynamic.

currently in order for me to do that, I'd have to create 3 separate templates file(header,data,footer) which if i have to, im willing to go that way but if i could have it all done in 1 template file, that'd be best.

what would you suggest?

should i try to make it all fit in 1 template file or just make 3 files do the work?
__________________
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
  #4 (permalink)  
Old 05-23-07, 09:10 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
Do it all in one page, that's ways faster and cleaner than using 3 different files.
I'll try to explain how i did it:

To place a loop in the template, i used the {for} keyword, like this:
smarty Code:
  1. {for loop=$var name=i}
  2. <!-- content here -->
  3. {/for}
where $var was the array to loop over. I is the name of the variable which will hold the current array-index. I had some additional parameters, such as "start" and "end", but i'll leave them out atm.
I used a regex to get the {for ....} declaration, and grab the value of the "loop" and "name" parameter (note: the regex below is not the one i'm using in my engine):
PHP Code:

$regex '#\{for\s+loop\s*=\s*([^\s\n]+)\s+name\s*=\s*([^\s\n]+)\s*\}#i'
I used preg_match_all to get all for-loop declarations in the template, and then looped over them to convert them to php. So in the end you get something like this:
PHP Code:

<?php 
$start 
0// or something else if the "start" param is defined
$end sizeof($var); // or something else if the "end" param is defined
for($i=$start$i<$end$i++): ?>
<!-- content here -->
<?php endfor; ?>
Next i store the file in a different directory, but as php file. This way i only have to compile the template once in order to get it working for everyone. You will have to change your script so it will check if the template is allready compiled first, if so, you jsut have to load the php page, and set a value to the $var variable. Or: instead of replacing the tags in the template document, you just declare the tags in your script, and run the compiled template (hope you can follow, i'm not very good at explaining sometimes ).

Here's an example of what a compiled template looks like after running it through my engine:
PHP Code:

<?php for ($this->TPL_VARS["-loop-"]["i"]=0$this->TPL_VARS["-loop-"]["i"] < sizeof($this->TPL_VARS["var"]); $this->TPL_VARS["-loop-"]["i"]+=1): ?>
<b><?=$this->TPL_VARS["var"][$this->TPL_VARS["-loop-"]["i"]];?></b><br/>
<?php endfor; ?>
i'm using classes as well, so that's where the $this->TPL_VARS comes from. That field holds all data that needs to be displayed within the template.
and this is what it looked like before compiling:
smarty Code:
  1. {for loop=$var name=i}
  2. <b>{$var[i]}</b><br/>
  3. {/for}
You see that you will have to convert the variable you're using within the for loop as well. Don't forget this, or it will never work (i forgot, and it took me about a day to find out what exactly wasn't working)

Hope this helps you more than it confuses you
cheers
__________________
"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
  #5 (permalink)  
Old 05-23-07, 01:11 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
no i understand.

I will try this out and see what works

thanks.
__________________
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
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
How to go about creating template system? Alan PHP 9 06-12-04 11:37 AM
Good template system [PL]Greg PHP 3 06-28-03 05:09 AM


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