PhpRenderer View Scripts¶
Once you call
include()s the requested view script and
executes it “inside” the scope of the
PhpRenderer instance. Therefore, in your view scripts, references to
$this actually point to the
PhpRenderer instance itself.
Variables assigned to the view – either via a View Model, Variables container, or simply by passing an array of variables to
render()– may be retrieved in three
- Explicitly, by retrieving them from the
Variablescontainer composed in the
- As instance properties of the
PhpRendererinstance: $this->varname. (In this situation, instance property access is simply proxying to the composed
- As local PHP variables: $varname. The
PhpRendererextracts the members of the
We generally recommend using the second notation, as it’s less verbose than the first, but differentiates between variables in the view script scope and those assigned to the renderer from elsewhere.
By way of reminder, here is the example view script from the
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
<?php if ($this->books): ?> <!-- A table of some books. --> <table> <tr> <th>Author</th> <th>Title</th> </tr> <?php foreach ($this->books as $key => $val): ?> <tr> <td><?php echo $this->escapeHtml($val['author']) ?></td> <td><?php echo $this->escapeHtml($val['title']) ?></td> </tr> <?php endforeach; ?> </table> <?php else: ?> <p>There are no books to display.</p> <?php endif;?>
One of the most important tasks to perform in a view script is to make sure that output is escaped properly; among other things, this helps to avoid cross-site scripting attacks. Unless you are using a function, method, or helper that does escaping on its own, you should always escape variables when you output them and pay careful attention to applying the correct escaping strategy to each HTML context you use.
PhpRenderer includes a selection of helpers you can use for this purpose:
EscapeUrl. Matching the correct helper (or combination of
helpers) to the context into which you are injecting untrusted variables will ensure that you are protected against
Cross-Site Scripting (XSS) vulnerabilities.
1 2 3 4 5 6 7 8 9 10