Results 1 to 10 of 10

Thread: Template Class:::Help ME

  1. #1

    Thread Starter
    Hyperactive Member PlaGuE's Avatar
    Join Date
    Jun 2005
    Location
    in ur mind.
    Posts
    445

    Template Class:::Help ME

    okay i have this template class.
    PHP Code:
    <?php
    class Page
    {
      var 
    $page;

      function 
    Page($tpl_file "testy.php") {
      
    $template "$tpl_file";
        if (
    file_exists($template))
          
    $this->page join(""file($template));
        else
          die(
    "Template file $template 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) {
            
    $data = (file_exists($data)) ? $this->parse($data) : $data;
            
    $this->page eregi_replace("{" $tag "}"$data,
                          
    $this->page);
            }
        else
          die(
    "No tags designated for replacement.");
      }

      function 
    output() {
        echo 
    $this->page;
      }
    }
    ?>
    PHP Code:
    require_once("lib/parser.php");
    $page = new Page("templates/test/index.body.php");

    $page->replace_tags(array(
      
    "title" => "HOME",
      
    "added_by" => "$row[author]",
      
    "added_on" => "$row[date]",
    "begin_cat_row" => "?????",
    "end_cat_row" => "????"
    ));

    $page->output(); 
    PHP Code:
    <table width="100%"  border="0" cellspacing="0" cellpadding="0">
      <
    tr>
        <
    th scope="col">Title</th>
        <
    th scope="col"Added on</th>
        <
    th scope="col">Added by</th>
      </
    tr>
    <!--- {
    begin_cat_row}-->
      <
    tr>
        <
    th scope="col">{title}</th>
        <
    th scope="col">{added_on}</th>
        <
    th scope="col">{added_by}</th>
      </
    tr>
    <!--- {
    end_cat_row}-->
    </
    table
    how. would i be able to 'loop' JUST
    PHP Code:
    <!--- {begin_cat_row}-->
      <
    tr>
        <
    th scope="col">{title}</th>
        <
    th scope="col">{added_on}</th>
        <
    th scope="col">{added_by}</th>
      </
    tr>
    <!--- {
    end_cat_row}--> 
    This would save a hell of a lot of time .... n skinning and in coding.

    Tho if anyone found a better example for this... plz tell me.
    Last edited by PlaGuE; Sep 16th, 2005 at 04:11 PM.
    Without balance, there could only be chaos.
    Without chaos, there could be no balance.
    I live with karma. Eat with destiny. Dream of life without shackles....
    Yet. If life had no consequences, life could not exist, nor could it flourish.


    If at first you dont succeed.You're screwed.

    C++/Java NOOB.

    I aint a professional at PHP, but if i can help i will.

  2. #2
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Template Class:::Help ME

    I use a templating system on two of the sites I've made. One site is my home page and I actually use PHP for the templates. I do this by having a template class:
    PHP Code:
    <?php
    // template.php
    class Template
    {
        var 
    $path;

        function 
    Template($path null)
        {
            
    $this->path $path;
        }

        function 
    display()
        {
            @include(
    $this->path);
        }
    }
    ?>
    The templates themselves are just PHP files containing HTML and PHP, with the difference being that they use PHP's alternate templating like syntax for control structures such as loops. A simple template may look like this:
    PHP Code:
    <?php // template.tpl.php ?>
    <html>
      <head>
        <title><?php echo($this->title?></title>
      </head>
      <body>
        <h1>My Friends</h1>
        <?php foreach($this->friends as $friend): ?>
          <p><?php echo($friend?></p>
        <?php endforeach; ?>
      </body>
    </html>
    A simple script which uses this template looks like this.
    PHP Code:
    <?php
    // index.php

    require_once 'template.php';

    $template = new Template('template.tpl.php');

    /* assign some variables */
    $template->title 'Custom Title';
    $template->friends = Array('me''graham''mr burns''bart');

    /* now display the template */
    $template->display();
    ?>
    It is worth noting that this method does require some discipline and consistency in approach to the templates. It is ideal and very elegant for projects where there are only a small number of people, all who know the language.

    However, there are situations where it may be necessary to allow other people access to the output layer of your web site. These people are not necessarily PHP developers and allowing them to modify PHP files directly could interfere with other layers of your application. Fortunately the Smarty project addresses this problem. It is a very powerful templating system written in PHP, for PHP and uses the same syntax as you are using in the templates, in addition to supporting looping and logical control structures. The template above written for Smarty would look like this:
    Code:
    {* template.tpl *}
    <html>
      <head>
        <title>{$title}</title>
      </head>
      <body>
        <h1>My Friends</h1>
        {foreach from="$friends item="friend"}
          <p>{$friend}</p>
        {/foreach}
      </body>
    </html>
    The PHP code to display the template is also very similar. Note that the SmartyTemplate class is an inherited version of the standard smarty class, this is described here.
    PHP Code:
    <?php
    // index.php

    require_once 'template.php';

    // a special class for smarty
    $template = new SmartyTemplate;

    /* assign some variables */
    $template->assign('title',  'Custom Title');
    $template->assign('friends', Array('me''graham''mr burns''bart'));

    /* now display the template */
    $template->display('template.tpl';);
    ?>
    Smarty compiles all templates into HTML and as such it is very fast, it separates the output layer of your application completely and securely from the PHP code.

    I did once try to write my own templating language, but it is very complicated, especially when you want to start matching nested statements of varying depths. So, I abandoned the idea and used smarty for one of my sites - why reinvent the wheel?
    Last edited by visualAd; Sep 16th, 2005 at 04:30 PM.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  3. #3

    Thread Starter
    Hyperactive Member PlaGuE's Avatar
    Join Date
    Jun 2005
    Location
    in ur mind.
    Posts
    445

    Re: Template Class:::Help ME

    uhh....sorry... me no understandy...lol...

    This is confusing... maybe a lil more lamens terms should help.. lol.
    Without balance, there could only be chaos.
    Without chaos, there could be no balance.
    I live with karma. Eat with destiny. Dream of life without shackles....
    Yet. If life had no consequences, life could not exist, nor could it flourish.


    If at first you dont succeed.You're screwed.

    C++/Java NOOB.

    I aint a professional at PHP, but if i can help i will.

  4. #4
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Template Class:::Help ME

    What don't you understand?
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  5. #5

    Thread Starter
    Hyperactive Member PlaGuE's Avatar
    Join Date
    Jun 2005
    Location
    in ur mind.
    Posts
    445

    Re: Template Class:::Help ME

    well.. it all really confuses me...


    all i am trying to do is... make a part loop... i guess once i figure out how to make {begin_cat_row} = while($row=mysql_fetch_array($result)):


    i may be okay...


    I work better with an example i can destroy n put back together...

    taht example kinda left me in the dark.

    sorry to say.
    Without balance, there could only be chaos.
    Without chaos, there could be no balance.
    I live with karma. Eat with destiny. Dream of life without shackles....
    Yet. If life had no consequences, life could not exist, nor could it flourish.


    If at first you dont succeed.You're screwed.

    C++/Java NOOB.

    I aint a professional at PHP, but if i can help i will.

  6. #6

    Thread Starter
    Hyperactive Member PlaGuE's Avatar
    Join Date
    Jun 2005
    Location
    in ur mind.
    Posts
    445

    Re: Template Class:::Help ME

    meh... ive decided... to use(AND LEARN) PHPBB's template class.

    theres even sum topics on it.
    Without balance, there could only be chaos.
    Without chaos, there could be no balance.
    I live with karma. Eat with destiny. Dream of life without shackles....
    Yet. If life had no consequences, life could not exist, nor could it flourish.


    If at first you dont succeed.You're screwed.

    C++/Java NOOB.

    I aint a professional at PHP, but if i can help i will.

  7. #7
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Template Class:::Help ME

    What you want is to introduce the ability to traverse a an array in your template right?

    Unfortunately the the system you are using only substitutes variables using a simple search and replace on the template file. Therefore you will have to rewrite the substitution function to enable looping through an array. Doing this would be quite a complex task however, one that you need not do because it has already been done

    In my reply I went on to explain two alternatives to the system you are already using. One alternative is to use PHP templates. If you save the following class in a file called tmeplate.php then include it in your script.
    PHP Code:
    <?php
    // template.php
    class Template
    {
        var 
    $path;

        function 
    Template($path null)
        {
            
    $this->path $path;
        }

        function 
    display()
        {
            @include(
    $this->path);
        }
    }
    ?>
    Now change the code in you script:
    PHP Code:
    require_once("template.php");
    $page = new Template("templates/test/index.body.php");


    $page->title "HOME";
    $page->added_by $row[author];
    $page->added_on => $row[date],

    $page->display(); 
    Now change your template file to this:
    PHP Code:
    <?php // templates/test/index.body.php ?>
    <table width="100%"  border="0" cellspacing="0" cellpadding="0">
      <tr>
        <th scope="col">Title</th>
        <th scope="col"> Added on</th>
        <th scope="col">Added by</th>
      </tr>
    <!--- {begin_cat_row}-->
      <tr>
        <th scope="col"><?php echo($this->title?></th>
        <th scope="col"><?php echo($this->added_on?></th>
        <th scope="col"><?php echo($this->added_by?></th>
      </tr>
    <!--- {end_cat_row}-->
    </table>
    OK - so that is you template system changed to PHP. Now you can add a loop. The easiest way of doing this is to load the rows you want into an array and add this array to the template.

    Replace your original code which assigns the variables with this loop which loads the result set into an array.
    PHP Code:
    $cat_array = array() // initialize the array as empty
    while($row=mysql_fetch_array($result) {
        
    $cat_array[] = $row// add the row to the array
    }

    $page->cat_array $cat_array// assign the array to the template 
    Now all you have to do is modify the template:
    PHP Code:
    <?php foreach($this->cat_array as $category): ?>
    <!--- {begin_cat_row}-->
      <tr>
        <th scope="col"><?php echo($category['title']) ?></th>
        <th scope="col"><?php echo($category['added_on']) ?></th>
        <th scope="col"><?php echo($category['added_by']) ?></th>
      </tr>
    <!--- {end_cat_row}-->
    <?php endforeach; ?>
    This should now display each row in the array.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  8. #8

    Thread Starter
    Hyperactive Member PlaGuE's Avatar
    Join Date
    Jun 2005
    Location
    in ur mind.
    Posts
    445

    Re: Template Class:::Help ME

    the thing is... im trying to stray away from the PHP... hences why the template files are .tpl.. cuz when a user wants to customize the look... I dont want them seeing all this php...

    Will give the stuff a try tho.
    Without balance, there could only be chaos.
    Without chaos, there could be no balance.
    I live with karma. Eat with destiny. Dream of life without shackles....
    Yet. If life had no consequences, life could not exist, nor could it flourish.


    If at first you dont succeed.You're screwed.

    C++/Java NOOB.

    I aint a professional at PHP, but if i can help i will.

  9. #9
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Template Class:::Help ME

    This is why I also suggested Smarty. It does exactly what you are asking for and there is no risk of the person editing the templates messing up the PHP code.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  10. #10

    Thread Starter
    Hyperactive Member PlaGuE's Avatar
    Join Date
    Jun 2005
    Location
    in ur mind.
    Posts
    445

    Re: Template Class:::Help ME

    well... im either gunna use smarty...

    or PHPBB's template system(class).

    I mean why not sue phpbb's. ... i might be making my script intergratable with PHPBB...

    idk yet... i havent discussed this with my friend yet.

    Thanks for your help.
    I appreciate your help.
    Without balance, there could only be chaos.
    Without chaos, there could be no balance.
    I live with karma. Eat with destiny. Dream of life without shackles....
    Yet. If life had no consequences, life could not exist, nor could it flourish.


    If at first you dont succeed.You're screwed.

    C++/Java NOOB.

    I aint a professional at PHP, but if i can help i will.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width