Zend Basic Tutorial
Zend Forms
Zend Database
Zend Advanced
A layout is a shared HTML structure (like a template) that wraps around your views. Think of it like a common header, footer, and sidebar that all pages use.
Zend layouts help avoid repeating the same HTML structure in every view.
By default, the main layout file is:
module/Application/view/layout/layout.phtml
This file wraps around the inner content of each view.
<!-- layout.phtml --> <!DOCTYPE html> <html> <head> <title><?= $this->headTitle() ?></title> </head> <body> <header> <h1>My Zend App</h1> <nav> <a href="<?= $this->url('home') ?>">Home</a> <a href="<?= $this->url('about') ?>">About</a> </nav> </header> <main> <?= $this->content ?> <!-- Page-specific view content --> </main> <footer> <p>Copyright © <?= date('Y') ?></p> </footer> </body> </html>
// in IndexController return new ViewModel([ 'message' => 'Welcome to our site!' ]);
<!-- index.phtml --> <h2><?= $this->message ?></h2>
This index.phtml
view will be automatically rendered insidelayout.phtml
.
If you want to use a different layout for a specific action:
public function homeAction() { $this->layout()->setTemplate('layout/custom'); return new ViewModel(); // renders 'main/index.phtml' }
Make sure the file exists at:/module/Application/view/layout/custom.phtml
If you want to return only view content without wrapping layout:
public function homeAction() { $view = new ViewModel(['data' => 'Hello']); $view->setTerminal(true); return $view; }