在PHP中加载延迟的+;参数化执行?

在PHP中加载延迟的+;参数化执行?,php,templates,class,Php,Templates,Class,我有两个PHP文件: template.php template.html.php 第一个是模板的类定义。第二个包含实际的基于HTML的模板,但是带有PHP构造(因此扩展名为.PHP)。我称之为混合html/php文件 是否可以在模板类(special\u include\u parse())中创建一些函数,该函数采用: $path(到混合html/php文件) $model(它被传递到混合html/php文件中的代码,以便可以使用$this->getModel()或$model或其他任何

我有两个PHP文件:

  • template.php
  • template.html.php
第一个是模板的类定义。第二个包含实际的基于HTML的模板,但是带有PHP构造(因此扩展名为.PHP)。我称之为混合html/php文件

是否可以在模板类(
special\u include\u parse()
)中创建一些函数,该函数采用:

  • $path(到混合html/php文件)
  • $model(它被传递到混合html/php文件中的代码,以便可以使用$this->getModel()或$model或其他任何东西引用它)


template.php

class Template {
    function Parse($model) {
        //include('/var/www/template.html.php');
        //fopen('/var/www/template.html.php');
        $return = special_include_parse('/var/www/template.html.php', $model);
    }
}
<html>
    <head>
        <title><? echo $this->getModel()->getTitle(); ?></title>
    </head>
</html>
template.html.php

class Template {
    function Parse($model) {
        //include('/var/www/template.html.php');
        //fopen('/var/www/template.html.php');
        $return = special_include_parse('/var/www/template.html.php', $model);
    }
}
<html>
    <head>
        <title><? echo $this->getModel()->getTitle(); ?></title>
    </head>
</html>

嗯。。。考虑到template.html.php基本上是php语法,为什么不直接设置$this(尽管我不会这样称呼它)并包含/需要template.html.php呢?基本上:

class Template {
  function Parse($model) {
    ob_start();
    require '/var/www/template.html.php'; // I wouldn't use absolute paths
    $return = ob_get_clean();
  }
}
就我个人而言,我认为这是一个很好的例子,通过将一个不必要的(事实上,适得其反的)对象抽象引入到一些非常简单的东西中,使事情变得更加困难:

$model = new MyModel(...);
require 'template.html.php';

。。。

我不知道你为什么要把它复杂化,或者更确切地说,你想实现什么。

我有视图类(.php),它可以有一个链接的模板文件(.html.php)。在脚本末尾,对视图类调用getHtml()。此时,应该在视图类上设置模型。Viewclass需要以某种方式将该模型传递给它的模板,以便可以使用模型中的值解析模板。我认为输出缓冲实际上就是我想要的。你认为有没有其他更有效的方法来实现我想要的?你的视图是如何被调用或引用的?听起来你实际上是在重新发明Fry模板系统: