Skip to content
This repository has been archived by the owner on Jan 31, 2020. It is now read-only.

Commit

Permalink
Merge ffa29a2 into 651621d
Browse files Browse the repository at this point in the history
  • Loading branch information
gsteel committed Oct 24, 2018
2 parents 651621d + ffa29a2 commit 09f50b7
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 2 deletions.
17 changes: 16 additions & 1 deletion doc/book/helpers/placeholder.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ $this->placeholder('foo')
<?= $this->placeholder('foo') ?>
```

The above results in an unodered list with pretty indentation.
The above results in an unordered list with pretty indentation.

Because the `Placeholder` container objects extend `ArrayObject`, you can also
assign content to a specific key in the container easily, instead of simply
Expand Down Expand Up @@ -131,6 +131,21 @@ foreach ($this->data as $datum): ?>
<?= $this->placeholder('foo')->data ?>
```

## Clearing Content

In certain situations it is desirable to remove or clear containers and aggregated content. The placeholder view helper
provides two methods to either delete a specific container or clear all containers at once:

```php
<?php
// Delete a single Container
$placeholderHelper = $this->plugin('placeholder');
$placeholderHelper->deleteContainer('myNamedContainer');
// Clear all containers at once
$placeholderHelper->clearContainers();
?>
```

## Concrete Implementations

zend-view ships with a number of "concrete" placeholder implementations. These
Expand Down
26 changes: 25 additions & 1 deletion src/Helper/Placeholder.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Placeholder extends AbstractHelper
/**
* Placeholder items
*
* @var array
* @var Container\AbstractContainer[]
*/
protected $items = [];

Expand Down Expand Up @@ -97,4 +97,28 @@ public function containerExists($key)
$return = array_key_exists($key, $this->items);
return $return;
}

/**
* Delete a specific container by name
*
* @param string $key
* @return self
*/
public function deleteContainer($key)
{
$key = (string) $key;
unset($this->items[$key]);
return $this;
}

/**
* Remove all containers
*
* @return self
*/
public function clearContainers()
{
$this->items = [];
return $this;
}
}
25 changes: 25 additions & 0 deletions test/Helper/PlaceholderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,29 @@ public function testPlaceholderRetrievesSameContainerOnSubsequentCalls()
$container2 = $this->placeholder->__invoke('foo');
$this->assertSame($container1, $container2);
}

public function testContainersCanBeDeleted()
{
$container = $this->placeholder->__invoke('foo');
$container->set('Value');
$this->assertTrue($this->placeholder->containerExists('foo'));
$this->assertSame('Value', (string) $this->placeholder->__invoke('foo'));
$this->placeholder->deleteContainer('foo');
$this->assertFalse($this->placeholder->containerExists('foo'));
$this->assertSame('', (string) $this->placeholder->__invoke('foo'));
}

public function testClearContainersRemovesAllContainers()
{
$this->placeholder->__invoke('foo');
$this->placeholder->__invoke('bar');

$this->assertTrue($this->placeholder->containerExists('foo'));
$this->assertTrue($this->placeholder->containerExists('bar'));

$this->placeholder->clearContainers();

$this->assertFalse($this->placeholder->containerExists('foo'));
$this->assertFalse($this->placeholder->containerExists('bar'));
}
}

0 comments on commit 09f50b7

Please sign in to comment.