@@ -6,27 +6,32 @@ View Renderer
6
6
:local:
7
7
:depth: 2
8
8
9
+ ***********************
9
10
Using the View Renderer
10
11
***********************
11
12
12
- The `` view() ` ` function is a convenience function that grabs an instance of the
13
+ The :php:func: ` view() ` function is a convenience function that grabs an instance of the
13
14
``renderer `` service, sets the data, and renders the view. While this is often
14
15
exactly what you want, you may find times where you want to work with it more directly.
15
16
In that case you can access the View service directly:
16
17
17
18
.. literalinclude :: view_renderer/001.php
19
+ :lines: 2-
18
20
19
21
Alternately, if you are not using the ``View `` class as your default renderer, you
20
22
can instantiate it directly:
21
23
22
24
.. literalinclude :: view_renderer/002.php
25
+ :lines: 2-
23
26
24
27
.. important :: You should create services only within controllers. If you need
25
28
access to the View class from a library, you should set that as a dependency
26
29
in your library's constructor.
27
30
28
31
Then you can use any of the three standard methods that it provides:
29
- **render(viewpath, options, save) **, **setVar(name, value, context) ** and **setData(data, context) **.
32
+ :php:meth: `render() <CodeIgniter\\ View\\ View::render()> `,
33
+ :php:meth: `setVar() <CodeIgniter\\ View\\ View::setVar()> ` and
34
+ :php:meth: `setData() <CodeIgniter\\ View\\ View::setData()> `.
30
35
31
36
What It Does
32
37
============
@@ -45,13 +50,28 @@ script. You will have to give each escaped value a unique parameter name.
45
50
No special meaning is attached to parameters whose value is an array. It is up
46
51
to you to process the array appropriately in your PHP code.
47
52
53
+ Setting View Parameters
54
+ =======================
55
+
56
+ The :php:meth: `setVar() <CodeIgniter\\ View\\ View::setVar()> ` method sets a view parameter.
57
+
58
+ .. literalinclude :: view_renderer/008.php
59
+ :lines: 2-
60
+
61
+ The :php:meth: `setData() <CodeIgniter\\ View\\ View::setData()> ` method sets multiple view
62
+ parameters at once.
63
+
64
+ .. literalinclude :: view_renderer/007.php
65
+ :lines: 2-
66
+
48
67
Method Chaining
49
68
===============
50
69
51
70
The ``setVar() `` and ``setData() `` methods are chainable, allowing you to combine a
52
71
number of different calls together in a chain:
53
72
54
73
.. literalinclude :: view_renderer/003.php
74
+ :lines: 2-
55
75
56
76
Escaping Data
57
77
=============
@@ -63,9 +83,10 @@ escape the data for. See below for context descriptions.
63
83
If you don't want the data to be escaped, you can pass ``null `` or ``'raw' `` as the final parameter to each function:
64
84
65
85
.. literalinclude :: view_renderer/004.php
86
+ :lines: 2-
66
87
67
88
If you choose not to escape data, or you are passing in an object instance, you can manually escape the data within
68
- the view with the `` esc() ` ` function. The first parameter is the string to escape. The second parameter is the
89
+ the view with the :php:func: ` esc() ` function. The first parameter is the string to escape. The second parameter is the
69
90
context to escape the data for (see below)::
70
91
71
92
<?= esc($object->getStat()) ?>
@@ -99,9 +120,10 @@ Several options can be passed to the ``render()`` or ``renderString()`` methods:
99
120
- ``cache_name `` - the ID used to save/retrieve a cached view result; defaults to the viewpath; ignored for ``renderString() ``
100
121
- ``saveData `` - true if the view data parameters should be retained for subsequent calls
101
122
102
- .. note :: ``saveData`` as defined by the interface must be a boolean, but implementing
123
+ .. note :: ``saveData() `` as defined by the interface must be a boolean, but implementing
103
124
classes (like ``View `` below) may extend this to include ``null `` values.
104
125
126
+ ***************
105
127
Class Reference
106
128
***************
107
129
@@ -110,7 +132,6 @@ Class Reference
110
132
.. php :class :: View
111
133
112
134
.. php :method :: render($view[, $options[, $saveData = false]])
113
- :noindex:
114
135
115
136
:param string $view: File name of the view source
116
137
:param array $options: Array of options, as key/value pairs
@@ -121,9 +142,9 @@ Class Reference
121
142
Builds the output based upon a file name and any data that has already been set:
122
143
123
144
.. literalinclude :: view_renderer/005.php
145
+ :lines: 2-
124
146
125
147
.. php :method :: renderString($view[, $options[, $saveData = false]])
126
- :noindex:
127
148
128
149
:param string $view: Contents of the view to render, for instance content retrieved from a database
129
150
:param array $options: Array of options, as key/value pairs
@@ -134,14 +155,14 @@ Class Reference
134
155
Builds the output based upon a view fragment and any data that has already been set:
135
156
136
157
.. literalinclude :: view_renderer/006.php
158
+ :lines: 2-
137
159
138
160
.. warning :: This could be used for displaying content that might have been stored in a database,
139
161
but you need to be aware that this is a potential security vulnerability,
140
162
and that you **must ** validate any such data, and probably escape it
141
163
appropriately!
142
164
143
165
.. php :method :: setData([$data[, $context = null]])
144
- :noindex:
145
166
146
167
:param array $data: Array of view data strings, as key/value pairs
147
168
:param string $context: The context to use for data escaping.
@@ -151,6 +172,7 @@ Class Reference
151
172
Sets several pieces of view data at once:
152
173
153
174
.. literalinclude :: view_renderer/007.php
175
+ :lines: 2-
154
176
155
177
Supported escape contexts: ``html ``, ``css ``, ``js ``, ``url ``, or ``attr `` or ``raw ``.
156
178
If ``'raw' ``, no escaping will happen.
@@ -159,7 +181,6 @@ Class Reference
159
181
until the view is rendered.
160
182
161
183
.. php :method :: setVar($name[, $value = null[, $context = null]])
162
- :noindex:
163
184
164
185
:param string $name: Name of the view data variable
165
186
:param mixed $value: The value of this view data
@@ -170,6 +191,7 @@ Class Reference
170
191
Sets a single piece of view data:
171
192
172
193
.. literalinclude :: view_renderer/008.php
194
+ :lines: 2-
173
195
174
196
Supported escape contexts: ``html ``, ``css ``, ``js ``, ``url ``, ``attr `` or ``raw ``.
175
197
If ``'raw' ``, no escaping will happen.
0 commit comments