Php 如何在没有eval的情况下输出这些动态数据?

Php 如何在没有eval的情况下输出这些动态数据?,php,eval,Php,Eval,我一直在用MVC风格编写CMS,并使用模板类通过file_get_内容拉入所需的各种文件 最后我会 eval('?>'.($template).'<?'); 通过用正确的内容替换变量,可以有效地呈现所有代码。在我的模板中,我使用输出缓冲来捕获包含的脚本。包含的代码与任何其他包含的文件一样运行。伪:启动缓冲区,包括文件,捕获缓冲区,擦除缓冲区。下面是一个简短的例子: //just the name of a template file to include. $template =

我一直在用MVC风格编写CMS,并使用模板类通过file_get_内容拉入所需的各种文件

最后我会

eval('?>'.($template).'<?');

通过用正确的内容替换变量,可以有效地呈现所有代码。

在我的模板中,我使用输出缓冲来捕获包含的脚本。包含的代码与任何其他包含的文件一样运行。伪:启动缓冲区,包括文件,捕获缓冲区,擦除缓冲区。下面是一个简短的例子:

//just the name of a template file to include.
$template = "someFile.tpl";
//start output buffering
ob_start();
//include the file. It has full access to all vars and runs
//code just like any other included script.
include($template);
//get anything output by the buffer during the include
$template_output = ob_get_contents();
//clean out the buffer because we already got the contents.
ob_end_clean();
运行之后,
$template\u output
将在包含的文件中运行任何代码后,由该文件输出任何内容。这允许我在处理“视图”时使用循环和变量等


但请注意,这是在我的个人网站上使用的,我是唯一一个对模板文件进行更改的人。我不允许任何其他人编辑模板文件,因为那样做太愚蠢了。

也许你的设计有缺陷。如果你简明扼要地解释了你要解决的问题,那么也许可以通过改变来避免eval。我同意James的观点,不仅不清楚你在做什么,也不清楚你为什么要这么做。你可能对这里的一些对话感兴趣-我试过这方面的一些东西,但它似乎不起作用->主要是因为控制器会将结果分配给模板组件中的变量-导致问题的原因是让它在运行时实际评估结果。我在编辑中添加的代码有效地呈现了动态变量。我仍然不确定这是否比eval更安全,尽管没有正确清理数据库中存储的用户输入的任何内容。
//just the name of a template file to include.
$template = "someFile.tpl";
//start output buffering
ob_start();
//include the file. It has full access to all vars and runs
//code just like any other included script.
include($template);
//get anything output by the buffer during the include
$template_output = ob_get_contents();
//clean out the buffer because we already got the contents.
ob_end_clean();