Silvio's blog

[PHP] Simple template class

  • Terça-feira Jul 14,2009 03:49 PM
  • By admin
  • In PHP

Today I’ll share a simple template class in php. I did it a while ago and it works fairly good.

Objectives:

  • No HTML embebed on PHP code
  • Load a HTML template from file
  • Support loops
  • Replace vars
  • Publish the template

Result

Template.php

<?php
class Template{
    private $template, $vars;

    public function __construct($templatePathAndName){
        if(file_exists($templatePathAndName))
            $this->template = file_get_contents($templatePathAndName);
        else
            die("Template not found... aborting...");
    }

    public function getLoop($id){
        $tmp = explode("{LOOP[" . $id . "]}", $this->template);
        $tmp = explode("{/LOOP[" . $id . "]}",$tmp[1]);
        $tmp = $tmp[0];
        return $tmp;
    }
    public function getLoopLine($loop, $lineId){
        $tmp = explode("{LINE[" . $lineId . "]}", $loop);
        $tmp = explode("{/LINE[" . $lineId . "]}", $tmp[1]);
        $tmp = $tmp[0];
        return $tmp;
    }

    public function setVar($var, $content){
        $this->vars[$var] = $content;
    }

    public function replaceloop($id, $lines){
        $loopContent = $this->getLoop($id);

        for($i=1;$i<=$lines;$i++)
            if($this->getLoopLine($loopContent, $i))
                $tmp .= str_replace("}", "[" . $i . "]}", $this->getLoopLine($loopContent, $i));
            else
                $tmp .=  str_replace("}", "[" . $i . "]}", $this->getLoopLine($loopContent, 1));

        $this->template = str_replace("{LOOP[" . $id . "]}" . $loopContent . "{/LOOP[" . $id . "]}", $tmp,$this->template);
    }
    public function replaceAll(){
        foreach($this->vars as $var => $content)
            $this->template = str_replace("{" . strtoupper($var). "}", $content, $this->template);
    }

    public function publish(){
        $this->replaceAll();

        return $this->template;
    }
}
?>

Template.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>{SITE_NAME}</title>
<link rel="stylesheet" type="text/css" href="my.css">
</head>
<body>
<h1>{SITE_NAME}</h1>

<table>
        <tr bgcolor="#FFFF00">
            <td>Name</td>
            <td>State</td>
            <td>Country</td>
        </tr>
{LOOP[1]}
    {LINE[1]}
        <tr>
            <td>{NAME}</td>
            <td>{STATE}</td>
            <td>{COUNTRY}</td>
        </tr>
    {/LINE[1]}
{/LOOP[1]}
</table>

</body>
</html>

main.php

<?php
  function __autoload($class_name) {
    require_once $class_name . '.php';
}

// An array with data
$dataArray[0]["Name"] = "Silvio";
$dataArray[0]["State"] = "CO";
$dataArray[0]["Country"] = "PT";
$dataArray[1]["Name"] = "John";
$dataArray[1]["State"] = "CA";
$dataArray[1]["Country"] = "US";

// Load the template
$tmpl = new Template("template.html");

// Set var {SITE_NAME}
$tmpl->setVar("SITE_NAME", "Simple Template Class");
$count = 0;
// We have an array, so we need a loop
// 1 - Set the vars
foreach($dataArray as $key => $tmp){
    $count++;
    foreach($tmp as $var => $content)
        $tmpl->setVar($var . "[" . $count . "]", $content);
}
// 2 - Replace the original loop with the lines we need
$tmpl->replaceLoop(1,$count);

// Publish the template
echo $tmpl->publish();
?>

Conclusion

I got a clean and simple template system (a single class) without HTML embedded on php
Result

Download files

Your Ads Here
Promote your products