关于我的PHP模板类的帮助
我想将HTML的输出与项目中的程序代码分开,所以我编写了一个非常简单的数据库类关于我的PHP模板类的帮助,php,class,templates,Php,Class,Templates,我想将HTML的输出与项目中的程序代码分开,所以我编写了一个非常简单的数据库类 <?php class Template { private $template; function load($filePath) { if(!$this->template = file_get_contents($filePath)) $this->error('Error: Failed to open <strong&
<?php
class Template
{
private $template;
function load($filePath)
{
if(!$this->template = file_get_contents($filePath))
$this->error('Error: Failed to open <strong>' . $filePath . '</strong>');
}
function replace($var, $content)
{
$this->template = str_replace("{$var}", $content, $this->template);
}
function display()
{
echo $this->template;
}
function error($errorMessage)
{
die('die() by template class: <strong>' . $errorMessage . '</strong>');
}
}
?>
index.php
文件如下:
<html>
<head>
<title>{TITLE}</title>
</head>
<body>
<h1>{TITLE}</h1>
<?php
if($something) {
echo '$something is true';
} else {
echo '$something is false';
}
?>
</body>
</html>
{TITLE}
{TITLE}
我只是想知道那里的PHP代码是否会运行?或者它只是以明文形式发送到浏览器?我在模板类中使用了eval()
,但我讨厌这个函数:P
谢谢。不。它将以纯文本形式显示 “echo”输出不解析PHP代码
您不需要依赖于使用eval。还有其他方法,例如使用输出缓冲 为什么不试着运行它呢?我还想收集一些建议,告诉大家应该使用什么方法来运行包含的文件中的PHP。
<html>
<head>
<title>{TITLE}</title>
</head>
<body>
<h1>{TITLE}</h1>
<?php
if($something) {
echo '$something is true';
} else {
echo '$something is false';
}
?>
</body>
</html>