Documentation · Chapter 05

Build views
that stay simple.

A view turns prepared data into a document understood by both the browser and the person using the site.

We improve reading-list without changing its HTTP logic or temporary session storage.

≈ 75 min

The behavior works; now improve its presentation.

The previous chapter pages repeat the doctype, head, and sometimes navigation. This works, but makes every change riskier. We will share common HTML and give CSS, images, and optional JavaScript clear locations.

A view does three things

Structure

Headings, lists, forms, and links in semantic HTML.

Display

Values supplied by the controller, always escaped.

Composition

Assembly of small shared partials.

References

Links to public styles, images, and scripts.

A view does not decide everything

IN THE VIEW

Loop through books, escape a title, choose an HTML element, display an already prepared error.

OUTSIDE THE VIEW

Query a database, modify the session, decide permissions, or validate a business rule.

The new map

phpaml — zsh
src/views/
├── books/
│   ├── index.php
│   ├── create.php
│   └── edit.php
├── partials/
│   ├── head.php
│   ├── header.php
│   ├── footer.php
│   └── book-form.php
└── errors/
    ├── 404.php
    ├── 500.php
    └── default.php

public/
├── css/app.css
├── img/logo.svg
└── js/books.js

SUMMARY

What to remember

  • The controller prepares; the view presents.
  • A partial prevents copying shared HTML.
  • Private views stay in src/views.
  • Files requested directly by the browser go in public.