Zend Framework

Chapter 4: Views & Templates in Zend Framework 3

Chapter 4: Views & Templates in Zend Framework 3

✅ Introduction to Views

In Zend Framework 3, the view layer is responsible for rendering the final HTML output. Views are written in .phtml files (PHP-based templates) and are located inside the view/ folder of a module.

  • Each module can have its own view folder.
  • Views are mapped based on the controller and action being called.
  • Zend uses the PhpRenderer by default for rendering templates.

✅ View File Structure

Inside the view/ folder of a module, templates are organized by namespace (controller) and action:


/module
  /Application
    /view
      /application
        /index
          index.phtml
  • application → matches the controller namespace (Application\Controller).
  • index → matches the controller name (IndexController).
  • index.phtml → matches the action method (indexAction()).

✅ Creating a Simple View

Suppose you have an IndexController with an indexAction. The corresponding view file will be:


// File: module/Application/src/Controller/IndexController.php
<?php
namespace Application\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;

class IndexController extends AbstractActionController
{
    public function indexAction()
    {
        return new ViewModel([
            'message' => 'Welcome to Zend Framework 3!',
        ]);
    }
}

// File: module/Application/view/application/index/index.phtml
<h1><?= $this->message; ?></h1>

✅ Passing Variables to Views

You can pass data from a controller to a view using ViewModel:


return new ViewModel([
    'username' => 'John Doe',
    'email' => 'john@example.com',
]);

// index.phtml
<p>User: <?= $this->username; ?></p>
<p>Email: <?= $this->email; ?></p>

✅ Layouts

A layout is a master template that wraps around your views. It usually contains the common HTML structure like header, footer, and navigation.

  • Default layout is stored in: module/Application/view/layout/layout.phtml
  • You can define multiple layouts for different modules.

// layout.phtml
<!DOCTYPE html>
<html>
<head>
  <title>My Zend App</title>
</head>
<body>
  <header><h1>Site Header</h1></header>

  <main>
    <?= $this->content; ?>
  </main>

  <footer><p>Copyright 2025</p></footer>
</body>
</html>

✅ Changing Layout Dynamically

You can change the layout for a controller action:


$this->layout('layout/custom');

This will load:


module/Application/view/layout/custom.phtml

✅ Using Partials

Partials are reusable view snippets. Example: a sidebar, navigation, or footer.


// Inside a view file
<?= $this->partial('application/partial/sidebar', [
    'categories' => ['PHP', 'Zend', 'Laminas']
]); ?>

// File: module/Application/view/application/partial/sidebar.phtml
<ul>
  <?php foreach ($this->categories as $cat): ?>
    <li><?= $cat; ?></li>
  <?php endforeach; ?>
</ul>

✅ Best Practices for Views

  • Keep views simple (avoid complex logic inside .phtml).
  • Use layouts for common page structure.
  • Use partials for reusable UI blocks.
  • Pass only necessary data from controllers to views.

✅ Exercises

  • Create a new layout blog-layout.phtml and apply it to your Blog module.
  • Create a partial view for navigation and include it in your layout.
  • Pass an array of posts to a view and display them in an HTML list.

Leave a Reply

Your email address will not be published. Required fields are marked *