如果我有一个这样的hello.php文件:
Hello, <?php echo $foo; ?>!
我想用一些php代码来做这样的事情:
$text = renderPhpToString('hello.php', array('foo'=>'World'));
并最终得到
$text == 'Hello, World!'
用标准的PHP5可以做到这一点吗?显然,我想要更复杂的模板,包括循环等等。
发布于 2009-04-19 16:08:01
正如Gumbo所说,你必须检查$file变量,这是一个微妙的bug,已经让我头疼了。我会使用func_get_arg( I),并且根本没有变量,还有一件小事,我会使用require。
function renderPhpToString( )
{
if( is_array( func_get_arg(1) ) ) {
extract( func_get_arg(1) );
}
ob_start();
require func_get_arg( 0 );
return ob_get_clean();
}
发布于 2009-04-17 19:52:29
发布于 2009-04-20 06:36:59
关于传递$vars = array('file' => '/etc/passwd');
,您可以使用extract($vars, EXTR_SKIP);
https://stackoverflow.com/questions/761922
复制相似问题