plates icon indicating copy to clipboard operation
plates copied to clipboard

Added sections inheritance of fetched templates

Open odahcam opened this issue 2 years ago • 12 comments

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 ].

odahcam avatar Jan 21 '23 18:01 odahcam

My latest commit now addresses my comments.

odahcam avatar Jan 22 '23 22:01 odahcam

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:

  1. creating a new mutable class called TemplateSections which you can push/unshift/add/replace/etc template section content.
  2. Each Template object has an instance of TemplateSections and optionally can accept a TemplateSections in the constructor.
  3. 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 avatar Jan 23 '23 14:01 ragboyjr

@ragboyjr I agree with you, I don't know if I can find time any sooner to do that though. Maybe next weekend.

odahcam avatar Jan 24 '23 03:01 odahcam

@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.

odahcam avatar Feb 04 '23 16:02 odahcam

I've added more template section tests.

odahcam avatar Mar 07 '23 23:03 odahcam

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

Abdillah avatar Mar 26 '23 08:03 Abdillah

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

Abdillah avatar Mar 27 '23 02:03 Abdillah

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.

odahcam avatar May 15 '23 21:05 odahcam

@Abdillah could you add the scenario for the issues you saw with using layout so I can make a test for it?

odahcam avatar May 15 '23 21:05 odahcam

I'm on vacation, I'll be back tomorrow.

Abdillah avatar May 18 '23 03:05 Abdillah

ping @Abdillah

odahcam avatar Jul 05 '23 19:07 odahcam

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>

Abdillah avatar Jul 06 '23 15:07 Abdillah