我将我的模板存储为文件,并希望有机会将它们存储在MySql数据库中。从数据库/字符串获取模板
我的模板系统
//function of Template class, where $file is a path to a file
function fetch() {
ob_start();
if (is_array($this->vars)) extract($this->vars);
include($file);
$contents = ob_get_contents();
ob_end_clean();
return $contents;
}
function set($name, $value) {
$this->vars[$name] = is_object($value) ? $value->fetch() : $value;
}
用法:
$tpl = & new Template('path/to/template');
$tpl->set('titel', $titel);
模板例如:
<h1><?=titel?></h1>
<p>Lorem ipsum...</p>
我的方法
- 从数据库中选择模板作为一个String
- 我得到的是像$第三方物流=“< H1> <?= $ Titel的? > ...“
- 现在我想将它传递给模板系统,所以我伸出我的构造函数和取功能:
函数取(){
if (is_array($this->vars)) extract($this->vars);
ob_start();
if(is_file($file)){
include($file);
}else{
//first idea: eval ($file);
//second idea: print $file;
}
$contents = ob_get_contents();
ob_end_clean();
return $contents;
}
” eval'给了我一个解析异常,因为它把整个字符串解释为php,而不仅仅是php部分。'print'真的很奇怪:它不打印介于两者之间的工作人员,但我可以在源代码中看到它该页面.php功能被忽略了。
那我该怎么试试呢?
是的,我可能会与该走了,我想过这个问题之前:) – 2012-02-27 17:51:02