thephpleague / plates

Native PHP template system
https://platesphp.com
MIT License
1.47k stars 180 forks source link

Stacked Layouts - variables not passing to main page layout? #80

Closed waifung0207 closed 9 years ago

waifung0207 commented 9 years ago

Hi,

First of all, thanks for the great project.

I am encountering an issue when trying out Stacked Layouts (as shown on this page). Following the example, the $title variable should be set from blog-article.php as below:

$this->layout('blog', ['title' => $article->title])

While I was expecting the variable $title to be passed through blog.php (Blog layout) then into template.php (Main site layout), however an error Undefined variable: title is shown when executing this line from template.php:

<title><?=$this->e($title)?></title>

And quite strangely, when I update the first line of blog.php like this, the page will show the article title properly:

$this->layout('template', ['title' => $title])

Is it an expected result, Or am I doing anything wrong here?

jk3us commented 9 years ago

Yeah, that's the way it works, and I think that's the way it should be. Depending on how your code is organized, you might be able to use $plates->addData() method to assign a variable to a template (or even to all templates). But I would suggest it's best in the long run to always be explicit about passing variables into templates. addData makes those variables more like global variables, which can make it hard to test and debug things.

waifung0207 commented 9 years ago

Yes I was thinking of addData() to solve the issue, thanks for explanation :)