Zend Layouts

What is a Layout?

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.


Default Layout Location

By default, the main layout file is:

module/Application/view/layout/layout.phtml

This file wraps around the inner content of each view.


Basic Layout File Example

<!-- 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>



Controller View + Layout in Action

// 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.


Change Layout Dynamically

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


Disable Layout (e.g., for AJAX or API)

If you want to return only view content without wrapping layout:

public function homeAction()
{
    $view = new ViewModel(['data' => 'Hello']);
    $view->setTerminal(true);
    return $view;
}



Whereisstuff is simple learing platform for beginer to advance level to improve there skills in technologies.we will provide all material free of cost.you can write a code in runkit workspace and we provide some extrac features also, you agree to have read and accepted our terms of use, cookie and privacy policy.
© Copyright 2024 www.whereisstuff.com. All rights reserved. Developed by whereisstuff Tech.