Jump to content

simple php Template engine


rami103

Recommended Posts

i friends i am working on simple php template engine here is some code can someboady help me to add files including function

like %include.sidebar.html%  and loop functions so i can run loops  or tell me some other simple php template engine

 

thanks

 

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

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

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

   
    public function replaceAll() {
        foreach($this->vars as $var => $content)
            $this->template = str_replace("{" . strtoupper($var). "}", $content, $this->template);
    }

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

        echo $this->template;
    }

    public function includeFile(){
         foreach($this->vars as $var => $content)
            $this->template = str_replace("<-" . strtoupper($var). "->", file_get_contents($content), $this->template);
    }
}
?>
<?php
$tpl = new Template;
$tpl->SetTemplete(base_path."home.html");
        $tpl->setVar("SITE_NAME", "Simple Template Class2");
        $tpl->publish();
        
?>
///home.html 
<html>
    <head>
        
    </head>
    <body>
        {SITE_NAME}
    </body>
</html>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.