Revisit whether data should be applied to all templates #136

Open
reinink opened this Issue Dec 27, 2016 · 1 comment

Projects

None yet

2 participants

@reinink
Member
reinink commented Dec 27, 2016

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.

@reinink reinink added this to the 4.0 milestone Dec 27, 2016
@ragboyjr
Contributor

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'])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment