Skip to content

Commit

Permalink
Merge b5a7880 into 5874ba8
Browse files Browse the repository at this point in the history
  • Loading branch information
reinink committed Dec 27, 2016
2 parents 5874ba8 + b5a7880 commit 4d3bf9f
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 3 deletions.
12 changes: 10 additions & 2 deletions docs/templates/sections.md
Expand Up @@ -7,11 +7,11 @@ title: Sections
Sections
========

The `start()` and `stop()` functions allow you to build sections (or blocks) of content within your template, and instead of them being rendered directly, they are saved for use elsewhere. For example, in your [layout](/templates/layouts/) template.
The `start()`, `stop()` and `append()` functions allow you to build sections (or blocks) of content within your template, and instead of them being rendered directly, they are saved for use elsewhere. For example, in your [layout](/templates/layouts/) template.

## Creating sections

You define the name of the section in the `start()` function, and end the section with the `stop()` function.
You define the name of the section in the `start()` function, and end the section with the `stop()` or `append()` functions.

~~~ php
<?php $this->start('welcome') ?>
Expand All @@ -22,6 +22,14 @@ You define the name of the section in the `start()` function, and end the sectio
<?php $this->stop() ?>
~~~

Use the `append()` function instead of `stop()` to append the content of the section instead of replacing it. This can be useful for specifying any JavaScript libraries required by your child views.

~~~ php
<?php $this->start('script') ?>
<script src="example.js"></script>
<?php $this->append() ?>
~~~

## Accessing section content

Access rendered section content using the name you assigned in the `start()` method. This variable can be accessed from the current template and layout templates using the `section()` function.
Expand Down
25 changes: 24 additions & 1 deletion src/Template/Template.php
Expand Up @@ -189,7 +189,13 @@ public function start($name)
);
}

$this->sections[$name] = '';
if (isset($this->sections[$name])) {
$content = $this->sections[$name];
unset($this->sections[$name]);
$this->sections[$name] = $content;
} else {
$this->sections[$name] = '';
}

ob_start();
}
Expand All @@ -211,6 +217,23 @@ public function stop()
$this->sections[key($this->sections)] = ob_get_clean();
}

/**
* Stop the current section block and append the results.
* @return null
*/
public function append()
{
if (empty($this->sections)) {
throw new LogicException(
'You must start a section before you can append it.'
);
}

end($this->sections);

$this->sections[key($this->sections)] .= ob_get_clean();
}

/**
* Returns the content for a section block.
* @param string $name Section name
Expand Down
49 changes: 49 additions & 0 deletions tests/Template/TemplateTest.php
Expand Up @@ -198,6 +198,55 @@ public function testNullSection()
$this->assertEquals($this->template->render(), 'NULL');
}

public function testAppendSection()
{
vfsStream::create(
array(
'template.php' => implode('\n', array(
'<?php $this->layout("layout")?>',
'<?php $this->start("scripts") ?><script src="example1.js"></script><?php $this->stop() ?>',
'<?php $this->start("scripts") ?><script src="example2.js"></script><?php $this->append() ?>',
)),
'layout.php' => '<?php echo $this->section("scripts") ?>',
)
);

$this->assertEquals($this->template->render(), '<script src="example1.js"></script><script src="example2.js"></script>');
}

public function testAppendingWithMultipleSections()
{
vfsStream::create(
array(
'template.php' => implode('\n', array(
'<?php $this->layout("layout")?>',
'<?php $this->start("scripts") ?><script src="example1.js"></script><?php $this->stop() ?>',
'<?php $this->start("test") ?>test<?php $this->stop() ?>',
'<?php $this->start("scripts") ?><script src="example2.js"></script><?php $this->append() ?>',
)),
'layout.php' => implode('\n', array(
'<?php echo $this->section("test") ?>',
'<?php echo $this->section("scripts") ?>',
)),
)
);

$this->assertEquals($this->template->render(), 'test\n<script src="example1.js"></script><script src="example2.js"></script>');
}

public function testAppendSectionBeforeStarting()
{
$this->setExpectedException('LogicException', 'You must start a section before you can append it.');

vfsStream::create(
array(
'template.php' => '<?php $this->append() ?>',
)
);

$this->template->render();
}

public function testFetchFunction()
{
vfsStream::create(
Expand Down

0 comments on commit 4d3bf9f

Please sign in to comment.