SitePoint Sponsor

User Tag List

Results 1 to 9 of 9

Hybrid View

  1. #1
    webdesign Percept's Avatar
    Join Date
    Jan 2003
    Location
    Belgium
    Posts
    397
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    How do they do this ?

    I just ran into this code of vBulletin and I was wondering how it's done.
    PHP Code:
    <td background="{imagesfolder}/leftside.gif"
    How do they specify the value of {Imagefolder} ? It's probably not to hard and probably explained somewhere but I wouldn't know what to look for and do these things have a name ?


  2. #2
    SitePoint Enthusiast
    Join Date
    Apr 2003
    Location
    NJ
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think vb uses a include file or something when they do those things. That way every page is loaded with everything. I could be wrong though.

  3. #3
    ********* Wizard silver trophy Cam's Avatar
    Join Date
    Aug 2002
    Location
    Burpengary, Australia
    Posts
    4,495
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    vBulletin uses a template system to replace the variables in the template with values specified in the script. Smarty is a similar sort of system that is distributed as open-source.

  4. #4
    SitePoint Zealot Egghead's Avatar
    Join Date
    Feb 2002
    Posts
    197
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey there Percept,
    It's basically 'Object-Oriented Programming' (OOP) but without churning out a lot of guff about OOP and losing you, just consider if you had an array called $locations holding all the values of things like 'images', 'stylesheets', 'javascripts', etc.
    PHP Code:
    <?php
    $locations 
    = array(
    "images" => "/images",
    "stylesheets" => "/css",
    "javascript" => "/scripts/javascript"
    );
    ?>
    Then you could make a template for all your pages with any references to "/images" or "/scripts/javascript" written instead as {images} and {javascript} and so on. The advantage will become clear soon... Now you can create all your pages using your template. When you want to alter something on all the pages, just make a change in the array in the template and the whole site is instantly updated.

    The function for replacing the parameters in curly braces could use the str_replace function and look something like this ...
    PHP Code:
    <?php
    foreach ($locations as $key => $value)
    {
    $replacement '{' $key '}';
    str_replace($replacement$value$template);
    }
    ?>
    This runs a looping function on the $locations array which finds any specified key within curly braces and then replaces it with the value. In one cycle of the loop all instances of {images} will be replaced with "/images", the next all instances of {stylesheets} will be replaced with "/css" and so on.

    The $template variable will be your template page. Using OOP you can dynamically create instances of your template and have this function replace all the relevant details on the fly. It's really marvellous once you've sussed it and the advantages become obvious when you need to make changes to many pages on a site, such as the images directory being moved or whatever.
    Last edited by Egghead; Apr 19, 2003 at 17:39.

  5. #5
    SitePoint Zealot Egghead's Avatar
    Join Date
    Feb 2002
    Posts
    197
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I forgot to say what they are called...

    They are most likely to be called parameters and will be used in some kind of Object-Oriented Programming class to build pages from templates. I think they can be called arguements too. Basically they will be variables passed to some function or other that does something with them. - Does that answer your question?

    Of course every instance of one of the items in curly brackets could change from page to page so it can also be individually passed as a parameter to the page-building template rather than specified in an array like in the above post's example. Consider {page-title} for instance, or {keywords}.

    If you wanted to individually set the parameter each time you could use some sort of SetParameter function in your page building class, but we are starting to get deep now and I don't want to bore anybody. A good OOP tutorial and a good template tutorial will set you underway. I should think you could do all this without having to use classes and Object-Oriented Programming. But maybe not?

  6. #6
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Depending on what you want to do with your HTML though. If it's some simple stuff then Smarty would be way too powerful for you - you'd never use have the functionality.

    Plus Smarty may have too a high learning curve for you - for what you want to do.

    If you need a simple non too complicated method of using HTML templates I will be more than happy to post my OOP classes with some examples ?

  7. #7
    "Of" != "Have" bronze trophy Jeff Lange's Avatar
    Join Date
    Jan 2003
    Location
    Calgary, Canada
    Posts
    2,063
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    vBulletin calls them Template Replacements, and they are stored in the database and str_replace()ed on page display.

    They are not done using OOP, as vB only has one class, a db class.
    Who walks the stairs without a care
    It shoots so high in the sky.
    Bounce up and down just like a clown.
    Everyone knows its Slinky.

  8. #8
    webdesign Percept's Avatar
    Join Date
    Jan 2003
    Location
    Belgium
    Posts
    397
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the explanation guys, I had a look at smarty and it's a little to complicated for the moment.
    ( just started php ). Another things is I go crazy when I hear OOP I know the advantages of OOP but I just don't like it ( yet ) I guess I should improve my "normal" programming skills first.

    Dr Livingston, a few examples are allways welcome


  9. #9
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay, this is an OOP class though it's a simple one; not too long 8)

    Class Template
    PHP Code:
    class Template /** (c)2003 les quinn version 2.00 **/
     
      
    var $dir;
      var 
    $returns;
      var 
    $filename;
      var 
    $variables;
      var 
    $executables;
      
      
    /**
      * set directory pathname
      */
      
    function SetTempDirectory($path) {
       
    $this -> dir $path;
      }
      
      
    /**
      * get directory pathname
      */
      
    function GetTempDirectory() {
       return 
    $this -> dir
      }
      
      
    /**
      * set filename to parse
      */
      
    function SetTemplate($file) {
       
    $this -> filename $this -> dir $file;
      }
      
      
    /**
      * return a functions return value, if any
      */
      
    function GetFunctionReturns($num) {
       
    # get a return result from a function held within class variable(s)
       
    return $this -> returns[$num];
      }
      
      
    /**
      * set TAGS used in template
      */
      
    function SetTempVariables($x) {
       if(
    is_array($x)) {
        foreach(
    $x as $value) {
         
    # define class variables for every TAG to use
         
    $this -> variables[] = $value;
        }
       }
      }
      
      
    /**
      * set executable functions
      */
      
    function SetTempFunctions($x) {
       if(
    is_array($x)) {
        foreach(
    $x as $value) {
         
    # define class variables for every executable function to use
         
    $this -> executables[] = $value;
        }
       }
      }
      
      
    /**
      * output HTML template with parsed TAGS
      */
      
    function ParseTemplate() {
       
    $fp = @ file($this -> filename);
       
       if(
    $fp) {
        
    # no errors, so begin
        
    while(list($number$line) = each($fp)) {
         
    # remove whitespace from each line
         
    trim($line);
         
         
    $count 0;
         
    $state false;
         
         
    # for each TAG found
         
    foreach($this -> variables as $var) {
          if(
    ereg($var$line) > (int) 0) {
           
    # got a TAG, so execute specific function
           
    $this -> returns[] = $this -> executables[$count]();
           
           
    $state true;
           break;
          }
          
    $count++;
         }
         if(!
    $state) { 
          
    # output template once completed parsing TAGS
          
    echo($line); 
         }
        }
       }
      }
     } 
    Now you need an HTML template, for example something simple - note my tags beginning with TAG_...

    Code:
    <html>
    <head>
    <title><TAG_DOC_TITLE></title></head>
    <body>
    <p>Welcome back <TAG_USERNAME>.</p><br><br>
    <p>You are always welcome here.</p>
    </body></html>
    Okay, there is 2 tags there - I did say simple LoL.

    Now the way the class works, is first you set the template directory and a template file as such

    PHP Code:
    $tmp = new Template;
    $tmp -> SetTempDirectory('templates/');
    $tmp -> SetTemplate('home.html'); 
    Now for every tag that you have in a template, you need to set this within the class and also for every tag you need to set a PHP function to be called for each specific tag, seen below

    PHP Code:
    $tmp -> SetTempVariables('TAG_DOC_TITLE''TAG_USERNAME');
    $tmp -> SetTempFunctions('PrintTitle''PrintUsername'); 
    And then it's a simple case of parsing the template

    PHP Code:
    $tmp -> ParseTemplate(); 
    It's been a long time since I've used this class - I now use XML and XSL to transform XML templates - a lot more portable for other document formats and my classes allow for far easier and quicker web page developments, so I don't bother with using HTML anymore - it's great 8)

    The PHP functions must either reside along side the above pieces of script I described or you must INCLUDE them first before the above script.

    You can have as many TAGs as you want - so long as you remember to include the functions as well.

    If you have a function which returns a variable, then use the following to get the value back

    PHP Code:
    $var $tmp -> GetFunctionReturns(...); 
    Where you put the index number of which function you want - i.e. if you have 3 function calls within a template and you want the return value of the second function you'd use

    PHP Code:
    $var $tmp -> GetFunctionReturns(1);
    # for the 3rd function use
    $var $tmp -> GetFunctionReturns(2);
    # etc etc 
    Any problems (and I can't see any using my examples...) or you need more help then get back to this post 8)


Bookmarks

Posting Permissions

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