This repository was archived by the owner on Jan 31, 2020. It is now read-only.
File tree Expand file tree Collapse file tree 3 files changed +66
-2
lines changed Expand file tree Collapse file tree 3 files changed +66
-2
lines changed Original file line number Diff line number Diff line change @@ -63,7 +63,7 @@ $this->placeholder('foo')
6363<?= $this->placeholder('foo') ?>
6464```
6565
66- The above results in an unodered list with pretty indentation.
66+ The above results in an unordered list with pretty indentation.
6767
6868Because the ` Placeholder ` container objects extend ` ArrayObject ` , you can also
6969assign content to a specific key in the container easily, instead of simply
@@ -131,6 +131,21 @@ foreach ($this->data as $datum): ?>
131131<?= $this->placeholder('foo')->data ?>
132132```
133133
134+ ## Clearing Content
135+
136+ In certain situations it is desirable to remove or clear containers and aggregated content. The placeholder view helper
137+ provides two methods to either delete a specific container or clear all containers at once:
138+
139+ ``` php
140+ <?php
141+ // Delete a single Container
142+ $placeholderHelper = $this->plugin('placeholder');
143+ $placeholderHelper->deleteContainer('myNamedContainer');
144+ // Clear all containers at once
145+ $placeholderHelper->clearContainers();
146+ ?>
147+ ```
148+
134149## Concrete Implementations
135150
136151zend-view ships with a number of "concrete" placeholder implementations. These
Original file line number Diff line number Diff line change @@ -23,7 +23,7 @@ class Placeholder extends AbstractHelper
2323 /**
2424 * Placeholder items
2525 *
26- * @var array
26+ * @var Container\AbstractContainer[]
2727 */
2828 protected $ items = [];
2929
@@ -97,4 +97,28 @@ public function containerExists($key)
9797 $ return = array_key_exists ($ key , $ this ->items );
9898 return $ return ;
9999 }
100+
101+ /**
102+ * Delete a specific container by name
103+ *
104+ * @param string $key
105+ * @return self
106+ */
107+ public function deleteContainer ($ key )
108+ {
109+ $ key = (string ) $ key ;
110+ unset($ this ->items [$ key ]);
111+ return $ this ;
112+ }
113+
114+ /**
115+ * Remove all containers
116+ *
117+ * @return self
118+ */
119+ public function clearContainers ()
120+ {
121+ $ this ->items = [];
122+ return $ this ;
123+ }
100124}
Original file line number Diff line number Diff line change @@ -77,4 +77,29 @@ public function testPlaceholderRetrievesSameContainerOnSubsequentCalls()
7777 $ container2 = $ this ->placeholder ->__invoke ('foo ' );
7878 $ this ->assertSame ($ container1 , $ container2 );
7979 }
80+
81+ public function testContainersCanBeDeleted ()
82+ {
83+ $ container = $ this ->placeholder ->__invoke ('foo ' );
84+ $ container ->set ('Value ' );
85+ $ this ->assertTrue ($ this ->placeholder ->containerExists ('foo ' ));
86+ $ this ->assertSame ('Value ' , (string ) $ this ->placeholder ->__invoke ('foo ' ));
87+ $ this ->placeholder ->deleteContainer ('foo ' );
88+ $ this ->assertFalse ($ this ->placeholder ->containerExists ('foo ' ));
89+ $ this ->assertSame ('' , (string ) $ this ->placeholder ->__invoke ('foo ' ));
90+ }
91+
92+ public function testClearContainersRemovesAllContainers ()
93+ {
94+ $ this ->placeholder ->__invoke ('foo ' );
95+ $ this ->placeholder ->__invoke ('bar ' );
96+
97+ $ this ->assertTrue ($ this ->placeholder ->containerExists ('foo ' ));
98+ $ this ->assertTrue ($ this ->placeholder ->containerExists ('bar ' ));
99+
100+ $ this ->placeholder ->clearContainers ();
101+
102+ $ this ->assertFalse ($ this ->placeholder ->containerExists ('foo ' ));
103+ $ this ->assertFalse ($ this ->placeholder ->containerExists ('bar ' ));
104+ }
80105}
You can’t perform that action at this time.
0 commit comments