【发布时间】:2012-03-21 23:04:36
【问题描述】:
我正在使用ob_get_contents() 作为核心方法创建自己的模板脚本。通过使用它,它可以渲染出其他文件,从一个文件中调用。
就像,假设我们有 4 个文件:
- index.php
- header.html
- footer.html
- functions.php
index.php 将调用并呈现其他文件的内容(此处为 2 个 html 文件)。通过使用以下代码:
//index.php
function render($file) {
if (file_exists($file)) {
ob_start();
include($file);
$content = ob_get_contents();
ob_end_clean();
return $content;
}
}
echo render('header.html');
echo render('footer.html');
但是(例如)当header.html 包含一个调用include('functions.php') 时,包含的文件(functions.php)不能在footer.html 中再次使用。我的意思是,我必须在footer.html 中再次包含。所以在这里,include('functions.php') 行必须包含在两个文件中。
如何include()一个文件而不从子文件中再次调用它?
【问题讨论】:
标签: php include require ob-get-contents