Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace section like twig #322

Open
wants to merge 4 commits into
base: v3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 31 additions & 15 deletions src/Template/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,19 @@ 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
* @return null
* @return bool
*/
public function start($name)
{
Expand All @@ -218,7 +227,10 @@ public function start($name)

$this->sectionName = $name;

ob_start();
if ($this->mustStopRenderingSection())
return false;

return ob_start();
}

/**
Expand Down Expand Up @@ -257,24 +269,28 @@ public function stop()
);
}

if (!isset($this->sections[$this->sectionName])) {
$this->sections[$this->sectionName] = '';
}

switch ($this->sectionMode) {
if (! $this->mustStopRenderingSection()) {

case self::SECTION_MODE_REWRITE:
$this->sections[$this->sectionName] = ob_get_clean();
break;
if (!isset($this->sections[$this->sectionName])) {
$this->sections[$this->sectionName] = '';
}

case self::SECTION_MODE_APPEND:
$this->sections[$this->sectionName] .= ob_get_clean();
break;
switch ($this->sectionMode) {

case self::SECTION_MODE_PREPEND:
$this->sections[$this->sectionName] = ob_get_clean().$this->sections[$this->sectionName];
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_PREPEND:
$this->sections[$this->sectionName] = ob_get_clean().$this->sections[$this->sectionName];
break;

}
}
$this->sectionName = null;
$this->sectionMode = self::SECTION_MODE_REWRITE;
Expand Down
8 changes: 3 additions & 5 deletions tests/Template/TemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,9 @@ public function testReplaceSection()
{
vfsStream::create(
array(
'template.php' => implode('\n', array(
'<?php $this->layout("layout")?><?php $this->start("test") ?>Hello World<?php $this->stop() ?>',
'<?php $this->layout("layout")?><?php $this->start("test") ?>See this instead!<?php $this->stop() ?>',
)),
'layout.php' => '<?php echo $this->section("test") ?>',
'template.php' => '<?php $this->layout("template2")?><?php $this->start("test") ?>See this instead!<?php $this->stop() ?>',
'template2.php' => '<?php $this->layout("layout")?><?php if($this->start("test")) { ?><?php exit() ?><?php } $this->stop() ?>',
'layout.php' => '<?php echo $this->section("test", "initial content") ?>',
)
);

Expand Down