plates
plates copied to clipboard
Added sections inheritance of fetched templates
This PR partially addresses the case described in #305 , it still lacks considering the section mode of each of the appended sections where you can append, prepend or replace the sections entirely. Unfortunately we don't current store how the sections where filled in the template so it is currently impossible to know that during the inheritance in the parent template. My current best idea for that is to create yet a new property called $sectionsAddedMode
so we can track per section name the mode used during the push/unshift/replace, e.g. $sectionsAddedMode = [ 'head' => 1, 'appbar' => 1, 'footerScripts' => 2 ]
.
My latest commit now addresses my comments.
Let's add a test to highlight the bug, and then also we'd need to support this in the insert
method as well.
Another solution which might work fine is:
- creating a new mutable class called TemplateSections which you can push/unshift/add/replace/etc template section content.
- Each Template object has an instance of TemplateSections and optionally can accept a TemplateSections in the constructor.
- when creating partials via insert or fetch, we can pass in the current templates TemplateSections instance to the child which would cause the push/unshift/etc methods to interact with the same instance of template sections which I think is definitely a bit more of the expectation.
Your solution isn't bad, but having one mutable class that defines the API clearly might simplify the solution.
@ragboyjr I agree with you, I don't know if I can find time any sooner to do that though. Maybe next weekend.
@ragboyjr my latest commit addresses the need for a TemplateSections API and defines the base for the implementation of the other ideas you gave. I'd like you to take a look at this initial stage and help me define the next steps.
I've added more template section tests.
Unfortunately we don't current store how the sections where filled in the template so it is currently impossible to know that during the inheritance in the parent template.
Let the template knows the parent and reuse the parent's section may solve this issue. I think that's more proper point of view on this problem-space.
I mean,
diff --git a/src/Template/Template.php b/src/Template/Template.php
index 21c66f0..b096633 100644
--- a/src/Template/Template.php
+++ b/src/Template/Template.php
@@ -40,6 +40,8 @@ class Template
*/
protected $data = array();
+ protected $parent;
+
/**
* An array of section content.
*
@@ -77,11 +79,12 @@ class Template
* @param Engine $engine
* @param string $name
*/
- public function __construct(Engine $engine, $name)
+ public function __construct(Engine $engine, $name, $parent = null)
{
$this->engine = $engine;
+ $this->parent = $parent;
$this->name = new Name($engine, $name);
- $this->sections = new TemplateSectionCollection();
+ $this->sections = $this->parent ? $this->parent->sections : new TemplateSectionCollection();
$this->data($this->engine->getData($name));
}
Ah, I see some complication when using layout
.
By the way, I have implemented the merging of multiple section modes based on this PR:
https://github.com/thephpleague/plates/pull/310
I think referencing the same sections as the parent template does is exactly one of the things we want to avoid. IN my understanding the Render should handle reading all sections and put them all together accordingly to whats registered in each template.
@Abdillah could you add the scenario for the issues you saw with using layout so I can make a test for it?
I'm on vacation, I'll be back tomorrow.
ping @Abdillah
Hello, I'm sorry for not responding soon.
I've written some of my though in this comment (quoted below). I expect Plates to overwrite section when multiple child template using ->start() ... ->end()
and append the ->push() ... ->end()
content after the rewrite.
I'm thinking of grouping REWRITE before any mutation. That is consistent with what template system should do in my mind.
layout.php
<?php $this->section('javascript') ?>
page.php
<?php $this->layout('layout') ?> <?php $this->start('javascript') ?> <script src="base-page-script-very-light-weight.js"></script> <?php $this->end() ?> <?= $this->fetch('common-component-a') ?> <?= $this->fetch('heavyweight-component-b') ?>
heavyweight-component-b.php
<?php $this->start('javascript') ?> <script src="base-page-script-support-heavy-weight.js"></script> <?php $this->end() ?>
common-component-a.php
<?php $this->push('javascript') ?> <script src="common-component-a.js"></script> <?php $this->end() ?>
The end result will be,
<script src="base-page-script-support-heavy-weight.js"></script> <script src="common-component-a.js"></script>