After this lesson you can share a page shell across bookshop views without copying the whole document into every template. A layout gives the site one place for its outer document, navigation and main content area. A child view supplies a named section.
Build the shared shell
Create app/Views/layouts/main.php. This complete template establishes the document around every catalogue page that extends it.
<!doctype html>
<html lang="en">
<head><meta charset="utf-8"><title>Paper Lantern Bookshop</title></head>
<body>
<nav><a href="<?= esc(site_url('books'), 'attr') ?>">Books</a></nav>
<main><?= $this->renderSection('content') ?></main>
</body>
</html>The fixed title is sufficient for this learning stage. A later application could pass a distinct page title, escaping it for HTML text. Avoid adding a dynamic value until its contract is clear. Layouts reduce duplication, but they do not decide which values may be trusted.
CodeIgniter's official View Layouts guide documents extend, section, endSection and renderSection. These methods run through the view renderer. The this variable inside these templates refers to that renderer's context, not the Books controller. Keeping that distinction clear helps you understand why controller request handling does not belong here.
Let a child fill one section
Replace the demo template from the previous lesson with this child view. Keep the existing index action returning books/demo with its title value.
<?= $this->extend('layouts/main') ?>
<?= $this->section('content') ?>
<h1><?= esc($title) ?></h1>
<p>A fictional catalogue for learning CodeIgniter.</p>
<?= $this->endSection() ?>Both templates passed PHP lint. During later application verification the same layout wrapped the model-backed list and detail pages. Rendering a child view that extends a layout causes the layout to be used; the controller still returns the child view's name. Do not switch the controller to rendering only the shell and wonder why the content section is empty.
Match section names exactly
The shell asks for content and the child supplies content. A spelling mismatch is not a database problem. Inspect the section names before changing the controller or model. Similarly, every opened section needs its closing endSection call so that the renderer knows the content boundary.
The shell includes the doctype, html, head and body elements. The child should supply the page's inner content rather than another complete document. Nesting a second body inside main creates invalid structure that a browser may repair in surprising ways. A page looking acceptable does not prove its document structure is correct.
Reuse carefully
Shared navigation is a good layout responsibility because it appears across catalogue pages. A list of books is not automatically a layout responsibility just because several pages might eventually show it. Passing unnecessary data into every layout can make a simple detail request depend on unrelated database work.
CodeIgniter also supports including view partials. You do not need a partial for every short line of markup. Extract a component when it has a useful name and a repeated purpose. Too many tiny files can make a beginner trace several layers just to understand one heading. Start with a shell and clear child views, then extract only when repetition justifies it.
Layout output remains trusted application markup, while inserted data still needs escaping. Do not escape the entire rendered section, because that would display the intended tags as literal text. Instead escape the variable values when the child inserts them. This distinction separates authored structure from data inside that structure.
Verify the rendered result
After rendering the demo page, inspect the response source. It should contain one document shell, the Books navigation link and the escaped title inside main. Browser developer tools may display a repaired DOM, so raw response inspection can be useful when diagnosing duplicate document tags.
The fetched official View Layouts guide supplies the framework mechanism. Choosing a navigation-only shell for Paper Lantern Bookshop is our application design. A layout does not provide authentication, caching policy or form validation merely because all pages use it.
Common mistakes
Rendering the layout directly can omit the child's content. Inconsistent section names can produce an apparently empty main area. Copying complete documents into child sections leads to nested document structure. Escaping the finished section instead of individual values turns intended markup into visible text. Finally, moving database queries into the shell hides work that every page now performs.
Exercise
Create the layout and child above. Explain why the controller still returns books/demo and identify the change needed if the child calls its section catalogue while the layout asks for content. Then decide where escaping belongs for a dynamic book title.
Show an answer
The child declares the layout it extends, so rendering the child combines both templates. Section names must agree; rename the child section or the layout placeholder consistently. Escape the book title where the child inserts it into HTML. Keep the section's authored markup as markup so the final document contains a real heading and paragraph.
Previous lesson · Next lesson · Related: explicit routes · Related: security boundaries
Key points
- A child view declares the layout it extends.
- Section names connect child content to layout placeholders.
- Escape data values rather than the complete rendered section.