gpt4 book ai didi

php - 简单的 php 模板引擎

转载 作者:行者123 更新时间:2023-12-03 02:40:21 25 4
gpt4 key购买 nike

我的 friend 们,我正在研究简单的 php 模板引擎,这里有一些代码可以帮助我添加包括函数在内的文件像 %include.sidebar.html% 和循环函数,这样我就可以运行循环或告诉我一些其他简单的 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);
}
}

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

最佳答案

您可以使用Smarty ,但是php本身就是一种模板语言。为什么要更换它?

关于php - 简单的 php 模板引擎,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3170407/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com