View Layouts

CodeIgniter supports a simple, yet very flexible, layout system that makes it simple to use one or morebase page layouts across your application. Layouts support sections of content that can be inserted fromany view being rendered. You could create different layouts to support one-column, two-column,blog archive pages, and more. Layouts are never directly rendered. Instead, you render a view, whichspecifies the layout that it wants to extend.

Creating A Layout

Layouts are views like any other. The only difference is their intended usage. Layouts are the only viewfiles that would make use of the renderSection() method. This method acts as a placeholder for content.

  1. <!doctype html>
  2. <html>
  3. <head>
  4. <title>My Layout</title>
  5. </head>
  6. <body>
  7. <?= $this->renderSection('content') ?>
  8. </body>
  9. </html>

The renderSection() method only has one argument - the name of the section. That way any child views knowwhat to name the content section.

Using Layouts in Views

Whenever a view wants to be inserted into a layout, it must use the extend() method at the top of the file:

  1. <?= $this->extend('default') ?>

The extend method takes the name of any view file that you wish to use. Since they are standard views, they willbe located just like a view. By default, it will look in the application’s View directory, but will also scanother PSR-4 defined namespaces. You can include a namespace to locate the view in particular namespace View directory:

  1. <?= $this->extend('Blog\Views\default') ?>

All content within a view that extends a layout must be included within section($name) and endSection() method calls.Any content between these calls will be inserted into the layout wherever the renderSection($name) call thatmatches the section name exists.:

  1. <?= $this->extend('default') ?>
  2.  
  3. <?= $this->section('content') ?>
  4. <h1>Hello World!</h1>
  5. <?= $this->endSection() ?>

The endSection() does not need the section name. It automatically knows which one to close.

Rendering the View

Rendering the view and it’s layout is done exactly as any other view would be displayed within a controller:

  1. public function index()
  2. {
  3. echo view('some_view');
  4. }

The renderer is smart enough to detect whether the view should be rendered on its own, or if it needs a layout.

Including View Partials

View partials are view files that do not extend any layout. They typically include content that can be reused fromview to view. When using view layouts you must use $this->include() to include any view partials.

  1. <?= $this->extend('default') ?>
  2.  
  3. <?= $this->section('content') ?>
  4. <h1>Hello World!</h1>
  5.  
  6. <?= $this->include('sidebar') ?>
  7. <?= $this->endSection() ?>

When calling the include() method, you can pass it all of the same options that can when rendering a normal view, includingcache directives, etc.