From 996eed64ab690b578466dd9c06ae819a6fd6fa04 Mon Sep 17 00:00:00 2001 From: RobinDev Date: Thu, 5 Oct 2023 10:18:45 +0200 Subject: [PATCH 1/4] change section replace way of working --- src/Template/Template.php | 16 ++++++++++++++++ tests/Template/TemplateTest.php | 8 +++----- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/Template/Template.php b/src/Template/Template.php index db11a9f..51e6d7d 100644 --- a/src/Template/Template.php +++ b/src/Template/Template.php @@ -199,6 +199,15 @@ public function layout($name, array $data = array()) $this->layoutData = $data; } + + private function mustStopRenderingSection(): bool + { + if (isset($this->sections[$this->sectionName]) && $this->sectionMode == self::SECTION_MODE_REWRITE) + return true; + + return false; + } + /** * Start a new section block. * @param string $name @@ -218,6 +227,9 @@ public function start($name) $this->sectionName = $name; + if ($this->mustStopRenderingSection()) + return; + ob_start(); } @@ -257,6 +269,10 @@ public function stop() ); } + + if ($this->mustStopRenderingSection()) + return; + if (!isset($this->sections[$this->sectionName])) { $this->sections[$this->sectionName] = ''; } diff --git a/tests/Template/TemplateTest.php b/tests/Template/TemplateTest.php index 788acd0..dc408c3 100644 --- a/tests/Template/TemplateTest.php +++ b/tests/Template/TemplateTest.php @@ -172,11 +172,9 @@ public function testReplaceSection() { vfsStream::create( array( - 'template.php' => implode('\n', array( - 'layout("layout")?>start("test") ?>Hello Worldstop() ?>', - 'layout("layout")?>start("test") ?>See this instead!stop() ?>', - )), - 'layout.php' => 'section("test") ?>', + 'template.php' => 'layout("template2")?>start("test") ?>See this instead!stop() ?>', + 'template2.php' => 'layout("layout")?>start("test") ?>Hello Worldstop() ?>', + 'layout.php' => 'section("test", "initial content") ?>', ) ); From 41b9905eb44c7e72433d8d21fe294139d2edcdba Mon Sep 17 00:00:00 2001 From: RobinDev Date: Thu, 5 Oct 2023 10:36:10 +0200 Subject: [PATCH 2/4] reset sectionName and sectionMode even if we not render the section --- src/Template/Template.php | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/Template/Template.php b/src/Template/Template.php index 51e6d7d..9adc954 100644 --- a/src/Template/Template.php +++ b/src/Template/Template.php @@ -270,27 +270,27 @@ public function stop() } - if ($this->mustStopRenderingSection()) - return; + if (! $this->mustStopRenderingSection()) { - if (!isset($this->sections[$this->sectionName])) { - $this->sections[$this->sectionName] = ''; - } + if (!isset($this->sections[$this->sectionName])) { + $this->sections[$this->sectionName] = ''; + } - switch ($this->sectionMode) { + switch ($this->sectionMode) { - case self::SECTION_MODE_REWRITE: - $this->sections[$this->sectionName] = ob_get_clean(); - break; + case self::SECTION_MODE_REWRITE: + $this->sections[$this->sectionName] = ob_get_clean(); + break; - case self::SECTION_MODE_APPEND: - $this->sections[$this->sectionName] .= ob_get_clean(); - break; + case self::SECTION_MODE_APPEND: + $this->sections[$this->sectionName] .= ob_get_clean(); + break; - case self::SECTION_MODE_PREPEND: - $this->sections[$this->sectionName] = ob_get_clean().$this->sections[$this->sectionName]; - break; + case self::SECTION_MODE_PREPEND: + $this->sections[$this->sectionName] = ob_get_clean().$this->sections[$this->sectionName]; + break; + } } $this->sectionName = null; $this->sectionMode = self::SECTION_MODE_REWRITE; From 7f78cb32048a86a9a9645cd180eb705ccfdb2d08 Mon Sep 17 00:00:00 2001 From: RobinDev Date: Thu, 26 Oct 2023 17:03:08 +0200 Subject: [PATCH 3/4] fix parent section (start) was still rendered --- src/Template/Template.php | 4 ++-- tests/Template/TemplateTest.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Template/Template.php b/src/Template/Template.php index 9adc954..e151f03 100644 --- a/src/Template/Template.php +++ b/src/Template/Template.php @@ -228,9 +228,9 @@ public function start($name) $this->sectionName = $name; if ($this->mustStopRenderingSection()) - return; + return false; - ob_start(); + return ob_start(); } /** diff --git a/tests/Template/TemplateTest.php b/tests/Template/TemplateTest.php index dc408c3..dce88d7 100644 --- a/tests/Template/TemplateTest.php +++ b/tests/Template/TemplateTest.php @@ -173,7 +173,7 @@ public function testReplaceSection() vfsStream::create( array( 'template.php' => 'layout("template2")?>start("test") ?>See this instead!stop() ?>', - 'template2.php' => 'layout("layout")?>start("test") ?>Hello Worldstop() ?>', + 'template2.php' => 'layout("layout")?>start("test")) { ?>stop() ?>', 'layout.php' => 'section("test", "initial content") ?>', ) ); From ca9fa3342d7f88a44f0ddcd0af234c2eaadeef44 Mon Sep 17 00:00:00 2001 From: RobinDev Date: Thu, 26 Oct 2023 17:16:42 +0200 Subject: [PATCH 4/4] fix phpdoc --- src/Template/Template.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Template/Template.php b/src/Template/Template.php index e151f03..697712c 100644 --- a/src/Template/Template.php +++ b/src/Template/Template.php @@ -211,7 +211,7 @@ private function mustStopRenderingSection(): bool /** * Start a new section block. * @param string $name - * @return null + * @return bool */ public function start($name) {