-3

我正在关注https://stackoverflow.com/questions/62617/whats-the-best-way-to-separate-php-code-and-html中的 PHP 模板示例

我的模板是:

class ViewTemplate {
private $args;
private $file;

public function __get($name) {
    return $this->args[$name];
}

public function __construct($file, $args = array()) {
    $this->file = $file;
    $this->args = $args;
}

public function render() {
    include $this->file;
}
}

我这样使用它:

    $event = "calendar";
    $view = new ViewTemplate('views/main.php', array(
        'topNav' => new ViewTemplate('views/topNav.php', array('login' => $login)),
        'leftPane' => new ViewTemplate('views/leftPane.php', array('login' => $login)),
        'mainContentNav' => new ViewTemplate('views/mainContentNav.php', array('login' => $login, 'event' => $event)),
        'mainContent'  => new ViewTemplate('views/mainContent/' + $event + '.php', array('login' => $login, 'event' => $event)),
        'footer'  => new ViewTemplate('views/footer.php', array('login' => $login))
    ));

我越来越:

[12-Apr-2013 21:33:42 UTC] PHP Warning:  include(0): failed to open stream: No such file or directory in C:\Users\Owner\PhpstormProjects\CRM\admin\classes\Lifestyle\ViewTemplate.php on line 23

[12-Apr-2013 21:33:42 UTC] PHP Warning:  include(): Failed opening '0' for inclusion (include_path='C:\Users\Owner\wamp\bin\php\php5.3.13\pear;C:\Users\Owner\PhpstormProjects\CRM\admin\classes\Lifestyle\classes') in C:\Users\Owner\PhpstormProjects\CRM\admin\classes\Lifestyle\ViewTemplate.php on line 23

问题似乎是以下位,我正在初始化数组:

'mainContent'  => new ViewTemplate('views/mainContent/' + $event + '.php', array('login' => $login, 'event' => $event)),

如果我将其更改为:

'mainContent'  => new ViewTemplate('views/mainContent/calendar.php', array('login' => $login, 'event' => $event)),

然后它起作用了,但它剥夺了我想要的动态用例。

是什么赋予了?

4

2 回答 2

3
'mainContent'  => new ViewTemplate('views/mainContent/' + $event + '.php', array('login' => $login, 'event' => $event)),

PHP 与 JS 的连接方式不同。在 PHP 中,连接运算符是..

'mainContent'  => new ViewTemplate('views/mainContent/' . $event . '.php', array('login' => $login, 'event' => $event)),

是对的。

于 2013-04-12T21:39:45.893 回答
1

PHP.用于连接+仅用于添加。

'views/mainContent/' . $event . '.php'
//                   ^        ^
于 2013-04-12T21:38:45.590 回答