Skip to content

Commit b28cd47

Browse files
authored
Merge pull request #4102 from MGatner/renderer
2 parents bbe67ee + 1430887 commit b28cd47

File tree

4 files changed

+37
-26
lines changed

4 files changed

+37
-26
lines changed

system/Config/View.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@
1616
*/
1717
class View extends BaseConfig
1818
{
19+
/**
20+
* When false, the view method will clear the data between each
21+
* call.
22+
*
23+
* @var boolean
24+
*/
25+
public $saveData = true;
26+
1927
/**
2028
* Parser Filters map a filter name with any PHP callable. When the
2129
* Parser prepares a variable for display, it will chain it

system/View/RendererInterface.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@ interface RendererInterface
2626
* @param array $options Reserved for 3rd-party uses since
2727
* it might be needed to pass additional info
2828
* to other template engines.
29-
* @param boolean $saveData If true, will save data for use with any other calls,
30-
* if false, will clean the data after displaying the view,
31-
* if not specified, use the config setting.
29+
* @param boolean $saveData Whether to save data for subsequent calls
3230
*
3331
* @return string
3432
*/
@@ -44,9 +42,7 @@ public function render(string $view, array $options = null, bool $saveData = fal
4442
* @param array $options Reserved for 3rd-party uses since
4543
* it might be needed to pass additional info
4644
* to other template engines.
47-
* @param boolean $saveData If true, will save data for use with any other calls,
48-
* if false, will clean the data after displaying the view,
49-
* if not specified, use the config setting.
45+
* @param boolean $saveData Whether to save data for subsequent calls
5046
*
5147
* @return string
5248
*/

system/View/View.php

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ public function __construct(ViewConfig $config, string $viewPath = null, FileLoc
142142
$this->loader = $loader ?? Services::locator();
143143
$this->logger = $logger ?? Services::logger();
144144
$this->debug = $debug ?? CI_DEBUG;
145-
$this->saveData = $config->saveData ?? null;
145+
$this->saveData = (bool) $config->saveData;
146146
}
147147

148148
//--------------------------------------------------------------------
@@ -152,12 +152,16 @@ public function __construct(ViewConfig $config, string $viewPath = null, FileLoc
152152
* data that has already been set.
153153
*
154154
* Valid $options:
155-
* - cache number of seconds to cache for
156-
* - cache_name Name to use for cache
155+
* - cache Number of seconds to cache for
156+
* - cache_name Name to use for cache
157157
*
158-
* @param string $view
159-
* @param array|null $options
160-
* @param boolean|null $saveData
158+
* @param string $view File name of the view source
159+
* @param array|null $options Reserved for 3rd-party uses since
160+
* it might be needed to pass additional info
161+
* to other template engines.
162+
* @param boolean|null $saveData If true, saves data for subsequent calls,
163+
* if false, cleans the data after displaying,
164+
* if null, uses the config setting.
161165
*
162166
* @return string
163167
*/
@@ -172,7 +176,7 @@ public function render(string $view, array $options = null, bool $saveData = nul
172176
$fileExt = pathinfo($view, PATHINFO_EXTENSION);
173177
$realPath = empty($fileExt) ? $view . '.php' : $view; // allow Views as .html, .tpl, etc (from CI3)
174178
$this->renderVars['view'] = $realPath;
175-
$this->renderVars['options'] = $options;
179+
$this->renderVars['options'] = $options ?? [];
176180

177181
// Was it cached?
178182
if (isset($this->renderVars['options']['cache']))
@@ -272,13 +276,13 @@ public function render(string $view, array $options = null, bool $saveData = nul
272276
* data that has already been set.
273277
* Cache does not apply, because there is no "key".
274278
*
275-
* @param string $view The view contents
276-
* @param array $options Reserved for 3rd-party uses since
277-
* it might be needed to pass additional info
278-
* to other template engines.
279-
* @param boolean $saveData If true, will save data for use with any other calls,
280-
* if false, will clean the data after displaying the view,
281-
* if not specified, use the config setting.
279+
* @param string $view The view contents
280+
* @param array|null $options Reserved for 3rd-party uses since
281+
* it might be needed to pass additional info
282+
* to other template engines.
283+
* @param boolean|null $saveData If true, saves data for subsequent calls,
284+
* if false, cleans the data after displaying,
285+
* if null, uses the config setting.
282286
*
283287
* @return string
284288
*/

user_guide_src/source/outgoing/view_renderer.rst

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ Several options can be passed to the ``render()`` or ``renderString()`` methods:
102102
ignored for renderString()
103103
- ``saveData`` - true if the view data parameters should be retained for subsequent calls
104104

105+
.. note:: ``saveData`` as defined by the interface must be a boolean, but implementing
106+
classes (like ``View`` below) may extend this to include ``null`` values.
107+
105108
Class Reference
106109
***************
107110

@@ -110,9 +113,9 @@ Class Reference
110113
.. php:method:: render($view[, $options[, $saveData=false]])
111114
:noindex:
112115

113-
:param string $view: File name of the view source
114-
:param array $options: Array of options, as key/value pairs
115-
:param boolean $saveData: If true, will save data for use with any other calls, if false, will clean the data after rendering the view.
116+
:param string $view: File name of the view source
117+
:param array $options: Array of options, as key/value pairs
118+
:param boolean|null $saveData: If true, will save data for use with any other calls. If false, will clean the data after rendering the view. If null, uses the config setting.
116119
:returns: The rendered text for the chosen view
117120
:rtype: string
118121

@@ -123,9 +126,9 @@ Class Reference
123126
.. php:method:: renderString($view[, $options[, $saveData=false]])
124127
:noindex:
125128

126-
:param string $view: Contents of the view to render, for instance content retrieved from a database
127-
:param array $options: Array of options, as key/value pairs
128-
:param boolean $saveData: If true, will save data for use with any other calls, if false, will clean the data after rendering the view.
129+
:param string $view: Contents of the view to render, for instance content retrieved from a database
130+
:param array $options: Array of options, as key/value pairs
131+
:param boolean|null $saveData: If true, will save data for use with any other calls. If false, will clean the data after rendering the view. If null, uses the config setting.
129132
:returns: The rendered text for the chosen view
130133
:rtype: string
131134

0 commit comments

Comments
 (0)