PHP缓冲区包含的文件作为匿名函数

时间:2018-11-07 12:42:33

标签: php include buffer anonymous-function

我有一种可以称为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将输出作为字符串获取。但是,这并不是我想要的。

问题

我想不出一种方法,只需要将filename.php包含一次(现在已加载3次)。因为它接受参数,所以不能作为字符串返回。我猜它需要作为匿名函数返回。然后,我可以缓冲模板并仍将其与新值一起使用。

欢迎任何创意。

0 个答案:

没有答案