HiveBrain v1.2.0
Get Started
← Back to all entries
patternphpMinor

PHP Template Engine

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
enginephptemplate

Problem

I have been working on a php driven template engine. This is fairly light weight, and fast from all of my testing, but I was hoping to get some feed back on it. First I would like to show you an example usages before I show the actual library. For full documentation and more examples: http://plater.phpsnips.com/

This first part is a example users homepage

templates/user.tpl


    
        User Home Page
    
    
        Welcome, $session.first.ucfirst();
        
            Here is where you will find the last 5 images that you have uploaded.
        
        $each("myimages"):
            
                
                    
                
                
                    $description;
                
            
        $endeach;
    


This next section is the php part for the above template.

user.php

 $row["filename"], "description" => $row["descr"]];
}

// replacemet
$tpl->assign("myimages", $images);

// show
$tpl->show("templates/user.tpl");


That was just a little taste of the template system. Here are some of the current features that it has so far:

  • Import templates within the template



  • Run PHP functions



  • Run Custom functions



  • Tidy



  • Import css (decreases http requests)



  • Loops



  • globals



  • $get



  • $post



  • $session



  • $cookie



  • $server



  • Template comments (Won't display in html output)



  • Multi line: /$ Multiline comment $/



  • Single line: $$ Single line comment



  • Remove empty tags after all replacements are done



And finally, here is the main library:

```

* @copyright (c) 2012, php Snips
* @version 0.0.1
* @see http://plater.phpsnips.com/docs/
*/
class Plater{

protected
$template = "",
$replacements = array(),
$cssFiles = array(),
$disableTidy = false;

public function __construct(){

}

public function show($filename){
try{
$this->template = $this->import($filename);
$this->format();
echo $t

Solution

Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

PHP itself already is a templating language, so my first thought would be - this is completely pointless. And no, "designers do not know PHP" is not an argument for making such templates, since those same developer would have to learn how to use your miracle instead.
About the code ..

-
There is only one way to render you template:

public function show($filename){
    try{
        $this->template = $this->import($filename);
        $this->format();
        echo $this->template;
    }catch(Exception $e){
        echo $e->getMessage();
    }
}


The problem is that this approach severely limits the ability to cache the rendered templates (you could use ob_*, that would be extremely hack'ish).

-
The public function attachCSS($filename = null) should be refactored. It's basically two different methods.

-
You have replaces the PHP built in templating functionality with preg_*, which seems quite silly.

-
These kind of templates will most likely conflict with JS libraries

Code Snippets

public function show($filename){
    try{
        $this->template = $this->import($filename);
        $this->format();
        echo $this->template;
    }catch(Exception $e){
        echo $e->getMessage();
    }
}

Context

StackExchange Code Review Q#20281, answer score: 4

Revisions (0)

No revisions yet.