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

Added sections inheritance of fetched templates #306

Open
wants to merge 5 commits into
base: v3
Choose a base branch
from
Open
Changes from 1 commit
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
90 changes: 76 additions & 14 deletions src/Template/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,21 +261,10 @@ public function stop()
$this->sections[$this->sectionName] = '';
}

switch ($this->sectionMode) {
$sectionContent = ob_get_clean();

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;
$this->pushSection($this->sectionMode, $this->sectionName, $sectionContent);

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;
$this->appendSection = false; /* for backward compatibility */
Expand Down Expand Up @@ -305,6 +294,71 @@ public function section($name, $default = null)
return $this->sections[$name];
}

/**
* Allows templates that are fetching this template to
* fetch the sections as well.
*
* @return array
*/
protected function getSections()
{
return $this->sections;
}

/**
* Joins another Template's sections into this taking in
* consideration the template's section mode.
*
* @param Template $template
* @return void
*/
protected function joinSections($template)
{
foreach ($template->getSections() as $sectionName => $sectionContent) {
$this->pushSection($template->sectionMode, $sectionName, $sectionContent);
}
}

/**
* Pushes the given section content to the sections array.
*
* @param int $sectionMode
* @param string $sectionName
* @param string $sectionContent
*
* @return void
*/
private function pushSection($sectionMode, $sectionName, $sectionContent)
odahcam marked this conversation as resolved.
Show resolved Hide resolved
{
// if ob_clean failed for some reason let's just ignore the result
if ($sectionContent === false) {
return;
}

// if this template doesn't have that section, so we just add it.
if (![$sectionName]) {
$this->sections[$sectionName] = $sectionContent;
return;
}

// otherwise we need to consider the incoming section mode
switch ($sectionMode) {

case self::SECTION_MODE_REWRITE:
$this->sections[$sectionName] = $sectionContent;
break;

case self::SECTION_MODE_APPEND:
$this->sections[$sectionName] .= $sectionContent;
break;

case self::SECTION_MODE_PREPEND:
$this->sections[$sectionName] = $sectionContent.$this->sections[$sectionName];
break;

}
}

/**
* Fetch a rendered template.
* @param string $name
Expand All @@ -313,7 +367,15 @@ public function section($name, $default = null)
*/
public function fetch($name, array $data = array())
{
return $this->engine->render($name, $data);
$template = $this->engine->make($name);
$content = $template->render($data);

// some info like 'sections' are only filled during
// the render processing, so here we have a window to
// fetch them and join to this template.
$this->joinSections($template);

return $content;
}

/**
Expand Down