【发布时间】:2013-04-18 19:21:57
【问题描述】:
我有需要进一步处理的模板文件,因此想使用包含而不使用 file_get_contents 将其分配给字符串变量。这样做的原因是因为最终输出是通过ob缓冲的,并且包含需要执行的php。如果我使用 file_get_contents 它不会执行。问题是如果我使用 $output = include 'file.php' 它会打印输出而不分配。
【问题讨论】:
-
输出缓冲在哪里 - 包含的脚本或主脚本?
-
再增加一层输出缓冲,使用
ob_get_clean() -
主脚本调用一个类来设置页面和处理模板。在主页中它调用这个类,处理输出,我使用 ob_start() //$template = ob_get_contents(); //ob_clean(); //$registry->getObject('template')->parseOutput(); //$template = $registry->getObject('template')->getPage()->getContentToPrint(); //$template = str_replace(array("\r\n","\n","\r"),'',$template); //回显$模板; //ob_end_flush();
-
$template = ob_get_contents(); ob_clean(); $registry->getObject('template')->parseOutput(); $template = $registry->getObject('template')->getPage()->getContentToPrint(); $template = str_replace(array("\r\n","\n","\r"),'',$template);回声$模板; ob_end_flush();抱歉,我已经评论了测试行
-
我相信
CORRUPT下面的答案正是你想要的,但请注意,如果你在file_get_contents()中使用url而不是文件路径,php应该执行。