开发者

simple php Template engine [closed]

开发者 https://www.devze.com 2023-01-05 19:27 出处:网络
Closed. This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post.
Closed. This question needs details or clarity. It is not currently accepting answers.

Want to improve this question? Add details and clarify the problem by editing this post.

Closed 7 years ago.

Improve this question

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

<?

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("{" .开发者_Python百科 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);
  }
}

$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>


You could use Smarty, but php itself is a templating language. Why replace it?


TinyButStrong is ar rather good but still small engine.


I use Smarty.


While slightly less lightweight, Twig is a powerful template engine with extremely simple syntax.


idea, not really formated for your class:

<?php
    $glob1 = glob("templates/$template/*");
    for($1=0;$i<=count($glob1)-1;$i++) {
        $file = $glob1[$i];
        $file = str_replace('templates/', '', $file);
        $template = str_replace('%include.$file%', (include("templates/$file")), $template();
    }
?>

really hope that helps.

0

精彩评论

暂无评论...
验证码 换一张
取 消