-3

I'm following the PHP Template example in https://stackoverflow.com/questions/62617/whats-the-best-way-to-separate-php-code-and-html

My Template is:

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;
}
}

I'm using it like this:

    $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))
    ));

I'm getting:

[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

The problem seems to be the following bit, where I'm initializing the array:

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

If I change that to:

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

Then it works, but it robs me of the dynamic use case I'd like.

What gives?

Community
  • 1
  • 1
Lurk21
  • 2,198
  • 11
  • 34
  • 52
  • 'views/mainContent/' + $event + '.php' = 0. – Eugen Rieck Apr 12 '13 at 21:40
  • PHP is not Javascript. http://php.net/language.operators.string - For the general guidance please see: [Reference - What does this symbol mean in PHP?](http://stackoverflow.com/q/3737139/2261774) – M8R-1jmw5r Apr 12 '13 at 21:40

2 Answers2

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

PHP does not concatenate the same way JS does. In PHP the concatenation operator is ..

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

would be right.

Chris Bier
  • 12,978
  • 15
  • 60
  • 100
bwoebi
  • 22,955
  • 4
  • 54
  • 75
  • Ah, yes - hehe, a little lingual confusion there. Thanks - one of those "if it was a snake it would have bit you". – Lurk21 Apr 12 '13 at 21:44
1

PHP uses . for concatenation. + is for addition only.

'views/mainContent/' . $event . '.php'
//                   ^        ^
Joseph Silber
  • 193,614
  • 53
  • 339
  • 276