thephpleague / plates

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

Revisit whether data should be applied to all templates #136

Closed reinink closed 6 years ago

reinink commented 7 years ago

In earlier versions of Plates all template data was made available to all templates. Meaning this main template being rendered, as well as any layouts and nested templates. However, we received complaints with that approach. Each template was not isolated, and unexpected variable overwriting could occur. So in Plates 3.x each template became its own instance of Template, isolating it from the other templates.

I actually liked this change. It meant you had to explicitly set the data passed to each template. Both layouts and nested templates got the option to pass down an array of template data. We also added the ability to set "global" data using the addData() method, helpful for things like layout template data.

That all said, it does seem more common for template engines (Twig/Blade) to make data accessible to all templates. We're also getting requests for this old behaviour. (#77, #83). I've opened this issue to simply revisit this for the 4.x release. This can be a place for discussion on whether the current implementation is good, or if we should change this back again.

ragboyjr commented 7 years ago

well, I think with something like #77, nothing would get overridden because anything you define to be passed in would be overridden.

Another option would be to add a helper method in the template that will allow users to override the current data in the current template.

<?php $this->insert('tpl', $this->override([
  'name' => 'John',
])); ?>

So, this would just simply be an alias of array_merge($this->data, ['name' => 'John'])

ragboyjr commented 6 years ago

I think it makes the most since to share data downward in the template tree. So any partials will inherit the current templates data, but layouts won't.

Each template will have it's own set of data however, if a template file makes any variable changes, those won't be reflected down stream and will need to be passed accordingly. Will need to have a section in the docs dedicated to this.