Skip to content

Commit

Permalink
Add Template.unshift() method
Browse files Browse the repository at this point in the history
  • Loading branch information
kozubsky committed Dec 11, 2020
1 parent 1b29776 commit a5f1630
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 5 deletions.
6 changes: 5 additions & 1 deletion doc/templates/sections.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,16 @@ You define the name of the section with the `start()` function. To end a section

## Stacking section content

By default, when you render a section its content will overwrite any existing content for that section. However, it's possible to append (or stack) the content instead using the `push()` method. This can be useful for specifying any JavaScript libraries required by your child views.
By default, when you render a section its content will overwrite any existing content for that section. However, it's possible to append/prepend (or stack) the content instead using the `push()` or `unshift()` method respectively. This can be useful for specifying any JavaScript libraries or CSS files required by your child views.

~~~ php
<?php $this->push('scripts') ?>
<script src="example.js"></script>
<?php $this->end() ?>

<?php $this->unshift('styles') ?>
<link rel="stylesheet" href="example.css" />
<?php $this->end() ?>
~~~

<p class="message-notice">The <code>end()</code> function is simply an alias of <code>stop()</code>. These functions can be used interchangeably.</p>
Expand Down
46 changes: 42 additions & 4 deletions src/Template/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@
*/
class Template
{
const SECTION_MODE_REWRITE = 1;
const SECTION_MODE_PREPEND = 2;
const SECTION_MODE_APPEND = 3;

/**
* Set section content mode: rewrite/append/prepend
* @var int
*/
protected $sectionMode = self::SECTION_MODE_REWRITE;

/**
* Instance of the template engine.
* @var Engine
Expand Down Expand Up @@ -44,6 +54,7 @@ class Template

/**
* Whether the section should be appended or not.
* @deprecated stayed for backward compatibility, use $sectionMode instead
* @var boolean
*/
protected $appendSection;
Expand Down Expand Up @@ -211,14 +222,26 @@ public function start($name)
}

/**
* Start a new append section block.
* Start a new section block in APPEND mode.
* @param string $name
* @return null
*/
public function push($name)
{
$this->appendSection = true;
$this->appendSection = false; /* for backward compatibility */
$this->sectionMode = self::SECTION_MODE_APPEND;
$this->start($name);
}

/**
* Start a new section block in PREPEND mode.
* @param string $name
* @return null
*/
public function unshift($name)
{
$this->appendSection = false; /* for backward compatibility */
$this->sectionMode = self::SECTION_MODE_PREPEND;
$this->start($name);
}

Expand All @@ -238,9 +261,24 @@ public function stop()
$this->sections[$this->sectionName] = '';
}

$this->sections[$this->sectionName] = $this->appendSection ? $this->sections[$this->sectionName] . ob_get_clean() : ob_get_clean();
switch ($this->sectionMode) {

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->appendSection = false;
$this->sectionMode = self::SECTION_MODE_REWRITE;
$this->appendSection = false; /* for backward compatibility */
}

/**
Expand Down

0 comments on commit a5f1630

Please sign in to comment.