我有一种模板文件,可以调用
filename.php
<h1>
<?= $test . ' world'; ?>
</h1>
<p>
Some text
</p>
那么我有一个函数
index.php
像这样的文件?
<?php
function test($args) {
$test = $args;
include 'filename.php';
}
test('Hello');
test('Hello');
test('Hello');
这个代码有效。它将包含的数据输出3次。
我也可以用
output buffering
如果我想的话,可以把输出作为字符串。然而,这并不是我想要的。
问题
我想不出一个办法只需要把
文件名.php
一次(现在加载了3次)。因为它接受参数,所以不能作为字符串返回。我想它需要以匿名函数的形式返回。然后我可以缓冲我的模板,仍然使用新的值。
欢迎任何有创意的想法。