与使用 include 不同,include 执行文件中包含的 php...是否可以将 php 文件的内容保存到变量中 - 但 php 仍然完整且可执行?
我的目标看起来像这样:
$template = some_imaginary_include_function('myfile.php');
foreach($list_of_blogs as $blog) {
// somehow get blog content in template and render template;
}
我知道这是一个愚蠢的例子......但我希望它能说明总体思路。如果我必须在一个页面上循环使用模板 50 次(假设这是一个博客列表),那么实际运行并包含每个模板似乎很愚蠢。
我错了吗?有没有办法做到这一点?
这个怎么样...
function getTemplate($file) {
ob_start(); // start output buffer
include $file;
$template = ob_get_contents(); // get contents of buffer
ob_end_clean();
return $template;
}
基本上,这会得到任何$file
是,并用 PHP 解析它,然后将输出返回到变量中。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)