Jump to content

Template system


doddsey_65

Recommended Posts

For my forum I used the phpbb template engine, which I grew to love. But I wasn't happy with including another forums code within mine. I have started to create my own template engine which is based off theirs. I am not using any of their code and have deleted phpbb off my system so i don't feel like cheating.

 

The class below will take a file, replace the content with the variables you specify (including looped content) and then create a php cached file.

 

If anyone has any pointers or crits so far then please let me know. Thanks

 

/**
  * Asf Template
  * 
  * This file handles the setting of the main template
  * and the setting af template specific variables to 
  * be handled in the cached file
  */
  
class Asf_Template
{
    /**
      * Holds the name of the template file
      * to be included within current page
      */
    public $tpl_file = null;
    
    /**
      * this is an array of all the variables
      * that are used to replace the placeholders
      * in the html template file
      */
    public $tpl_data = array();
    
    /**
      * The name of the template we are using
      */
    public $template = null;
    
    /**
      * The extension of the template files
      */
    public $tpl_ext = '.html';
    
    /**
      * The directory in which cached files are stored
      */
    public $cache_dir = './cache';
    
    /**
      * Sets the template to be used
      *
      * @params $template string
      * @return void
      */
    public function set_template($template)
    {
        $this->template = $template;
    }
    
    /**
      * Sets the variables which are used
      * to replace the placeholders in the html file
      *
      * @params $vars array
      * @return void
      */
    public function set_vars(array $vars)
    {
        if(!is_array($vars))
        {
            return false;
        }
        
        foreach($vars as $key => $val)
        {
            $this->tpl_data[$key] = $val;
        }
    }
    
    /**
      * Sets the variables which are used
      * to replace the placeholders in the html file
      *
      * @params $loop string
      * @params $vars array
      * @return void
      */
    public function loop_vars($loop, array $vars)
    {
        if(!is_array($vars))
        {
            return false;
        }
        
        foreach($vars as $key => $val)
        {
            $this->tpl_data[$loop][$key][] = $val;
        }
    }
    
    /**
      * Sets the html file to be used
      *
      * @params $file string
      * @return void
      */
    public function set_file($file)
    {
        $this->tpl_file = $file;
    }
    
    /**
      * Replaces the placeholders in the html file
      * with the template variables and caches the
      * file
      *
      * @return $cached string
      */
    public function render()
    {
        $content = file_get_contents($this->tpl_file.$this->tpl_ext);
        
        foreach($this->tpl_data as $key => $val)
        {
            if(preg_match('|\{'.$key.'\}|i', $content))
            {
                $content = preg_replace('|\{'.$key.'\}|i', "<?php echo \$this->tpl_data['$key']; ?>", $content);
            }
            
            echo $key.'<br />';
            
            if(preg_match('|\<!-- BEGIN '.$key.' --\>|i', $content))
            {
                foreach($this->tpl_data[$key] as $l_key => $l_val)
                {
                    for($i=0; $i<count($this->tpl_data[$key][$l_key]); $i++)
                    {
                        $content = preg_replace('|\{'.$key.'.'.$l_key.'\}|i', "<?php foreach(\$this->tpl_data['$key']['$l_key'] as \$var){ echo \$var; } ?>", $content);
                    }
                }
            }
        }
        
        $handle = fopen($this->cache_dir.'/'.$this->tpl_file.'_tpl.php', 'w');
        
        fwrite($handle, $content);
        
        fclose($handle);
        
        include($this->cache_dir.'/'.$this->tpl_file.'_tpl.php');
        
        dump($this->tpl_data);
    }
}

// instantiate the class
$template = new Asf_Template;

// set the template to be used
$template->set_template('default');

// set the tpl file to be rendered
$template->set_file('test');

// set some template specific vars
$template->set_vars(array(
    'HELLO'    => 'Howdy!',
    'FRENCH'        => 'Bonjoúr',
    )
);

// set a template specific loop
for($i=0; $i<10; $i++)
{
    $template->loop_vars('loop', array(
        'NUMBER'    => $i,
        )
    );
}

// render the template
$template->render();

 

So the html file would look like this:

 

{HELLO}
{FRENCH}

<!-- BEGIN loop -->
{loop.NUMBER}
<!-- END loop -->

 

which would ouput a php cache file with this content

 

<?php echo $this->tpl_data['HELLO']; ?>
<?php echo $this->tpl_data['FRENCH']; ?>

<!-- BEGIN loop -->
<?php foreach($this->tpl_data['loop']['NUMBER'] as $var){ echo $var; } ?>
<!-- END loop -->

 

And the output to the browser is:

 

Howdy!Bonjoúr 0123456789

 

Any tips or suggestions then? I plan on extending this code to perform more functions.

 

Link to comment
https://forums.phpfreaks.com/topic/245658-template-system/
Share on other sites

You might want to make all the properties 'private' except $tpl_data, as that's the only property being accessed outside of the class?

 

You could also make a constructor to pass the other 'config' properties into the class and set them. Something like...

public function __construct($tpl_file=null, $template=null, $tpl_ext='.html', $cache_dir='./cache') {
  $this->set_file($tpl_file);
  $this->set_template($template);
  $this->tpl_ext = $tpl_ext;
  $this->cache_dir = $cache_dir;
}

// then to create an instance...
$template = new Asf_Template(null, null, '.html', './cache');

Link to comment
https://forums.phpfreaks.com/topic/245658-template-system/#findComment-1261721
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.