Activity Stream
48,167 MEMBERS
6771 ONLINE
besthostingforums On YouTube Subscribe to our Newsletter besthostingforums On Twitter besthostingforums On Facebook besthostingforums On facebook groups

Results 1 to 8 of 8
  1.     
    #1
    Member
    Website's:
    litewarez.net litewarez.com triniwarez.com

    Default Creating a Fast PHP Template System

    Well, I really dont see the point in a template engine that uses repalcements/regex so i have made this tutorial to show you another way.

    PHP Is already a template engine, when you write <?php echo $var?> its just like doing <{$var}> or {$var}

    Think of it this way, PHP Already translates <?php echo '<b>hello</b>'?> into <b>hello</b> by its engine, so why make it do everything 2 times over.

    The way you would implement a template engine is like so

    ------

    Firstly create a template class

    PHP Code: 
        class Template
        
    {
           var 
    $vars = array();
        
           function 
    __set($key,$val)
           {
              
    $this->vars[$key] = $val;
           }
        
           function 
    __get($key)
           {
             return isset(
    $this->vars[$key]) ? $this->vars[$key] : false;
           }
        
           function 
    output($tpl false)
           {
              if(
    $tpl === false)
              {
                 die(
    'No template file selected in Template::output(...)');
              }
        
              if(!
    file_exists(($dir 'templates/' $tpl '.php')))
              {
                 die(
    sprintf('Tpl file does not exists (%s)',$dir));
              }
        
              new 
    TemplateLoader($dir,$this->vars);
              return 
    true;
           }
        } 
    This is what you use in your logic such as index.php, you will set data just like an **stdClass** just google it if your unsure. and when you run the output command it sends the data and tpl to the next class below.

    ----------

    And then create a standalone class to compile the tpl file within.

    PHP Code: 
        class TemplateLoader
        
    {
            private 
    $vars = array();
            private 
    $_vars = array(); //hold vars set within the tpl file
            
    function __construct($file,$variables)
            {
                
    $this->vars $variables;
                
    //Start the capture;
                
    ob_start();
                   include 
    $file;
                   
    $contents ob_get_contents();
                
    ob_end_clean(); //Clean it

               //Return here if you wish
               
    echo $contents;
            }
        
            function 
    __get($key)
            {
                return isset(
    $this->vars[$key]) ? $this->vars[$key] : (isset($this->_vars[$key]) ? $this->_vars[$key] : false) : false;
            }
        
            function 
    __set($key,$val)
            {
               
    $this->_vars[$key] = $val;
               return 
    true;
            }

           function 
    bold($key)
           {
              return 
    '<strong>' $this->$key '</string>';
           }
        } 
    The reason we keep this seperate is so it has its own space to run in, you just load your tpl file as an include in your constructor so it only can be loaded once, then when the file is included it has access to all the data and methods within TemplateLoader.

    ----------

    #Index.php
    PHP Code: 
     <?php
           
    require_once 'includes/Template.php';
           require_once 
    'includes/TemplateLoader.php';
        
        
           
    $Template = new Template();
           
           
    $Template->foo 'somestring';
           
    $Template->bar = array('some' => 'array');
        
           
    $Template->zed = new stdClass(); // Showing Objects
        
           
    $Template->output('index'); // loads templates/index.php
        
    ?>
    Now here we dont really want to mix html with this page because by seperating the php and the view / templates you making sure all your php has completed because when you send html or use html it stops certain aspects of your script from running.

    ----------

    templates/index.php

    PHP Code: 
    header
          
            <h1><?php $this->foo;?></h1>
            <ul>
                <?php foreach($this->bar as $this->_foo):?>
                    <li><?php echo $this->_foo?></li>
                <?php endforeach; ?>
            </ul>
             <p>Testing Objects</p>
             <?php $this->sidebar $this->foo->show_sidebar $this->foo->show_sidebar false;?>
             <?php if($this->sidebar):?>
                Showing my sidebar.
             <?php endif;?>
        footer
    Now here we can see that were mixing html with php but this is ok because in ehre you should only use basic stuff such as Foreach,For etc. and Variables.

    ----------

    NOTE: IN the TemplateLoader Class you can add a function like..

    PHP Code: 
    function bold($key)
    {
        return 
    '<strong>' $this->$key '</string>';

    This will allow you to increase your actions in your templates so bold,italic,atuoloop,css_secure,stripslashs..

    You still have all the normal tools such as stripslashes/htmlentites etc.

    Heres a small example of the bold.

    PHP Code: 
    $this->bold('foo'); //Returns <strong>somestring</string> 
    ---------

    You can add lots of tools into the TempalteLoader class such as inc() to load other tpl files, you can develop a helper system so you can go $this->helpers->jquery->googleSource

    If you have any more questions feel free to ask me.
    litewarez Reviewed by litewarez on . Creating a Fast PHP Template System Well, I really dont see the point in a template engine that uses repalcements/regex so i have made this tutorial to show you another way. PHP Is already a template engine, when you write <?php echo $var?> its just like doing <{$var}> or {$var} Think of it this way, PHP Already translates <?php echo '<b>hello</b>'?> into <b>hello</b> by its engine, so why make it do everything 2 times over. The way you would implement a template engine is like so ------ Rating: 5
    Join Litewarez.net today and become apart of the community.
    Unique | Clean | Advanced (All with you in mind)
    Downloads | Webmasters


    Notifications,Forum,Chat,Community all at Litewarez Webmasters


  2.   Sponsored Links

  3.     
    #2
    Member
    Website's:
    litewarez.net litewarez.com triniwarez.com
    Also here are the files that i just put together, there all working and i think all of you should start using such thing in your scripts.

    http://rapidshare.com/files/40432365...ngine.zip.html
    Join Litewarez.net today and become apart of the community.
    Unique | Clean | Advanced (All with you in mind)
    Downloads | Webmasters


    Notifications,Forum,Chat,Community all at Litewarez Webmasters


  4.     
    #3
    Respected Developer
    Website's:
    wrzc.org
    This is pretty cool. simple but does exactly what you want. I'm not using this right now but I have a project I'm going to give this a go in soon.
    Tutorial How to SEO your Warez Site a guide to help you increase your organic traffic

    Huge list of Warez Sites and free Multiposter Templates

  5.     
    #4
    Member
    its good, but did u check Smarty

    Good luck./

  6.     
    #5
    Member
    Website's:
    mirrormaker.org
    I'd still prefer smarty over writing my own, mainly because it's pre-written, well tested, easy to use and prolly more efficient than something I would write myself. If you have a huge project, imo, there's no sense re-inventing the wheel by writing your own template engine, when there is more important stuff to do. Plus if for whatever reason smarty doesn't do what you want it to do, you can just write a plugin for it. Just my opinion though

  7.     
    #6
    Member
    Website's:
    CodeSociety.net
    Smarty is a template engine with everything u can possibly think of doing.

    Example:
    In Smarty {$variable} is the same as <?php echo $variable; ?> Smarty basically makes it a lot easier to type and Shorter in length

    Plus allows us to use MVC pattern, there a lot of benefits to why template engine (smarty), as u have said php itself is a template engine



  8.     
    #7
    Member
    Website's:
    litewarez.net litewarez.com triniwarez.com
    yea but with smarty

    your getting php to tun {$$var} into <?php echo $var;?> and then php turns that into "some value"

    Example:

    index.php --> index.tpl -> Smarty engine -> PHP's Engine -> Output

    There THE SAME, They convert what you write into what is sent to the browaser, you just cangee the tags and make php work a hell of a lot hard

    Why do you enable asp style tags and shortags.

    <%=$var%> or <?=$var?> instead of <?php echo $var ?> if you cant be arsed to write :/

    Also Immortal, Smarty has nothing to do with the MVC Pattern, Fuck Template systems in general have nothing to do with the MVC Pattern :/
    Join Litewarez.net today and become apart of the community.
    Unique | Clean | Advanced (All with you in mind)
    Downloads | Webmasters


    Notifications,Forum,Chat,Community all at Litewarez Webmasters


  9.     
    #8
    Member
    Quote Originally Posted by litewarez View Post
    Also here are the files that i just put together, there all working and i think all of you should start using such thing in your scripts.

    http://rapidshare.com/files/40432365...ngine.zip.html
    link deleted pls reupload
    Can Do :-
    PSD TO ANY CMS | PHP(Dynamic Site) | MySql | Script Customization | cUrl Related Work
    Pm me for more info ;)

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 3
    Last Post: 29th Aug 2012, 02:33 AM
  2. Replies: 6
    Last Post: 30th Nov 2011, 03:00 AM
  3. Need fast DLE template
    By John in forum Webmaster Discussion
    Replies: 6
    Last Post: 22nd Nov 2011, 04:16 PM
  4. Creating a simple Object Config system in PHP
    By litewarez in forum Web Development Area
    Replies: 0
    Last Post: 9th Dec 2010, 09:59 PM
  5. Replies: 6
    Last Post: 28th Sep 2009, 08:46 PM

Tags for this Thread

BE SOCIAL