After this lesson you can pass a named data value into a view and render it as HTML text safely. A template is a PHP file used for presentation. Its purpose is to describe the page, not to hide database queries behind markup.
Send a small data contract
For this stage, replace the controller's index method with the following method. It uses fictional in-memory data while you learn rendering; SQLite arrives later. This is a method fragment for the existing Books class, not a second complete controller file.
public function index(): string
{
return view('books/demo', ['title' => 'The Paper Orchard']);
}Create app/Views/books/demo.php with this template:
<!doctype html>
<html lang="en">
<head><meta charset="utf-8"><title>Book preview</title></head>
<body><h1><?= esc($title) ?></h1></body>
</html>Both PHP snippets were syntax-checked in their intended contexts. The controller names the variable contract: the view receives title. The template uses that variable and escapes it before insertion. The view function returns the rendered content, which the controller returns to the application.
The official Views guide documents passing data into templates. CodeIgniter's Global Functions guide documents esc and its output contexts. These are framework facilities; choosing a small explicit data contract is a design practice that makes templates easier to reason about.
Store original data, escape at output
A book title is text even when it contains characters that have meaning in HTML. If a title includes an ampersand, angle bracket or quote, the browser should display the intended text rather than interpret it as page structure. Escaping prepares a value for the specific output location.
Do not pre-escape titles before saving them in the database. The same title might later appear in HTML text, an attribute or JSON. Each context has different handling. Storing presentation entities can cause double escaping, such as displaying the literal entity spelling when another template escapes it again.
For ordinary HTML text, esc with its default context is the pattern used here. For a quoted HTML attribute, use the attribute context. Do not assume HTML escaping makes an arbitrary URL safe to navigate: URL scheme validation and application policy remain separate concerns.
<a href="<?= esc(site_url('books'), 'attr') ?>">Catalogue</a>This template fragment was linted. The URL is generated from a fixed application path rather than supplied by a visitor. That distinction matters: escaping an attacker-chosen destination would not by itself make that destination appropriate.
Keep logic in proportion
A view can use a foreach loop to render records and an if condition to show an empty state. Those are presentation decisions. Opening a database connection or accepting a write inside the template would make page rendering perform hidden work. Keep the controller's data contract clear and let the model own persistence.
A missing variable is often a contract error. Check the array passed to view and compare its keys with the template variables. Do not silence every warning with a default value before understanding why the expected data is absent. An intentional optional value can have a default; a required value should be supplied deliberately.
Understand what a preview proves
A PHP template cannot run inside the site's static HTML playground. It requires the temporary CodeIgniter application. If you want to inspect a rendered page's plain HTML, you can copy the response markup into the HTML viewer, but remove private information first. That tool will not execute the server-side PHP, query the model or verify access controls.
The fetched official Views guide and Global Functions guide are the references for rendering and escaping. The PHP manual's htmlspecialchars documentation also explains why special characters need encoding when used as HTML. Escaping remains necessary for database values as well as freshly submitted values.
Common mistakes
Printing title without escaping creates an HTML interpretation boundary where you wanted text. Escaping on input and again on output can damage display. Another mistake is treating an attribute as ordinary body text and omitting its proper context. Finally, copying PHP into a static preview and seeing the code as text is not a framework failure; the preview does not contain a PHP runtime.
Exercise
Change the fictional title to contain angle brackets and an ampersand. Predict whether the page should add a new element or show those characters as text. Explain how the same stored title should be handled if the JSON API later returns it.
Show an answer
The escaped template displays the characters as title text rather than introducing an element. The database should keep the original title. For the API, use JSON serialization through the response facilities rather than manually inserting HTML entities. The eventual client still needs to choose safe rendering for its own context.
Previous lesson · Next lesson · Related: explicit routes · Related: security boundaries
Key points
- Views receive data through named values.
- Escape at the output boundary rather than before storage.
- A static HTML preview cannot execute CodeIgniter PHP templates.