Skip to content
This repository was archived by the owner on Jan 31, 2020. It is now read-only.
Merged
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
10 changes: 9 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,15 @@ All notable changes to this project will be documented in this file, in reverse

### Added

- Nothing.
- [#168](https://github.com/zendframework/zend-view/pull/168) adds two new methods to `Zend\View\Helper\Placeholder` (and thus any
helper extending it):

- `deleteContainer(string $name)` can be used to delete a placeholder container.
- `clearContainers()` can be used to clear all placeholder containers.

These new features are particularly useful when in long-running server
environments, such as Swoole, where you may need to clear the contents on each
request.

### Changed

Expand Down
20 changes: 19 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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


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,24 @@ 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:

### Delete a single container

```php
$this->plugin('placeholder')->deleteContainer('myNamedContainer');
```

### Clear all containers

```php
$this->plugin('placeholder')->clearContainers();
```

## Concrete Implementations

zend-view ships with a number of "concrete" placeholder implementations. These
Expand Down
24 changes: 23 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,26 @@ public function containerExists($key)
$return = array_key_exists($key, $this->items);
return $return;
}

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

/**
* Remove all containers
*
* @return void
*/
public function clearContainers()
{
$this->items = [];
}
}
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'));
}
}