PHP Templates

edited November 2004 in Software
Anyone know how to make PHP scripts that use templates?

Comments

  • you could look at phpsysinfo, that uses templates
  • Straight from the Tomchu.com source code. :P

    You need a class like this one:
    class TemplateInstance {
        var $templateCreation;
    
        // Implodes specified template file into variable
        function TemplateInstance($template) {
            $this->templateCreation = implode('', file("templates/$template"));
        }
    
        // Simple tag replacement function, also doubles as file includer if data
        //   given is a path to an existing file
        function replace($tag, $data) {
            $data = (file_exists($data)) ? implode('', file($data)) : $data;
            $this->templateCreation = str_replace('<!--'.$tag.'-->', $data, $this->templateCreation);
        }
    
        // Outputs the final template compilation
        function output() {
            print($this->templateCreation);
        }
    }
    

    You use the class like this:
    $template = new TemplateInstance('yourtemplate.tpl');
        $template->replace('TITLE', $somevariable);
        $template->output();
    

    "Tags" in your HTML template are <!--LIKETHIS-->. You would then use $template->replace('LIKETHIS', $somevariable), and the tag would be replaced with whatever $somevariable contains.
  • ah thanks.

    ill put that to use.
  • No problem.
Sign In or Register to comment.