How can I use multiple views in Zend_Application (using Zend_Layout)?

I have a layout with 4 separate "pieces". It:

  • Navigation bar with menus and breadcrumbs. It is built using Zend_Navigation.
  • Sidebar, which by default shows general "news"
  • The content area in which the main output of each controller action will be placed.
  • The title bar, which is above the navigation, which usually displays only some stock text and a photo.

The content area follows the traditional single view model, which corresponds to the documentation for Zend_Application, and the other three do not. All of them have reasonable default views for use, but if necessary, the controller should be able to redefine them. For example, for the administration page, it makes sense to redefine the "news" view to display a log of the latest administrative actions taken on the system.

In the examples Zend_Layout/ Zend_Applicationall take one form (they cause <?php echo $this->layout()->content; ?>.

How can this kind of layout override be performed? The only solution I was thinking about is to keep the redefined Zend_Viewinside Zend_Registry, but it is like keeping things together with duct tape;)

+3
2

, " ". Zend Framework " " .

, :

<div id="nav">
    <?php echo $this->layout()->nav ?>
</div>
<div id="content">
    <?php echo $this->layout()->content ?>
</div>

2 , "content" "nav" . "". "nav" :

<?php

$response = $this->getResponse();
$response->insert('nav', $view->render('nav.phtml'));

?>

ActionStack. , , , "nav" , , . :

<?php

class PageController extends Zend_Controller_Action
{
    public function barAction()
    {
        // this would render the output of NavController::menuAction()
        // to the "nav" segment (note how we set the response segment in the
        // NavController in order to do this)
        $this->_helper->actionStack('menu', 'nav');
    }
}

class NavController extends Zend_Controller_Action
{
    public function menuAction()
    {
        $this->_helper->viewRenderer->setResponseSegment('nav'); 
        // do stuff
    }
}

?>
+4

, , , . $this- > layout() → , , . . , :

<html>
<head>
<title><?=$this->headTitle()?></title>
</head>

<body>

<?=$this->render('_header.phtml')?>

<?=$this->render('_navigation.phtml')?>

<?=$this->render('_sidebar.phtml')?>

<?=$this->layout()->content?>

</body>
</html>

, PHP-, HTML, . , :

<?php
class My_View_Helper_Sidebar extends Zend_view_Helper_Abstract
{
    public function sidebar()
    {
        $html = '';

        // code to generate side bar here

        return $html;
    }
}
?>

, , :

<?=$this->sidebar()?>

, , , , , . , .

, Zend_View, , .

+3

Source: https://habr.com/ru/post/1762703/


All Articles