Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

zend framework two step view

I am new to zendframework . I am trying to implement two step view lay out:

Bootstrap.php(view/Bootstrap.php)

<?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{

     public function _initRoutes()
    {
        $options = array(
    'layout'     => 'layout',
    'layoutPath' => '/layout/layout.phtml',);
$layout = Zend_Layout::startMvc($options);
    }
}?>

layout.phtml(application/view/scripts/layout/layout.phtml)

<?php
        include "header.php";

?>
// view contents goes here.
<?php

       $this->layout()->content;

?>
// footer goes here.
<?php
        include "footer.phtml";
?>

i am a absolute beginner step by step explanation is more appreciated .Thanks.

like image 636
sudeep cv Avatar asked Mar 30 '26 19:03

sudeep cv


2 Answers

The easiest way to enable layouts is to run the Zend_Tool command from the command line zf enable layout, this will add the line
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/"
to your application.ini and build the directory for the layout and the default file layout.phtml.

Alternatively you can specify your layout path and default layout name with 2 lines in your application.ini file:

resources.layout.layoutPath = APPLICATION_PATH "/layouts" //path to layout
resources.layout.layout = master //name of layout without extension

other layout/view options may be set in your application.ini to be callled in your view:

;View Settings
;*************
resources.view[]=
resources.view.charset = "UTF-8"
resources.view.encoding = "UTF-8"
resources.view.doctype = "HTML5"
resources.view.language = "en"
resources.view.contentType = "text/html; charset=UTF-8"

then in your bootstrap.php you can call on these resources to initialize your view:

 /**
     * initialize the registry and asign application.ini to config namespace
     */
    protected function _initRegistry() {

        //make application.ini configuration available in registry
        $config = new Zend_Config($this->getOptions());
        Zend_Registry::set('config', $config);
    }

    /**
     * initialize the view and return it
     * @return \Zend_View
     */
protected function _initView() {
        //Initialize view
        $view = new Zend_View();
        //add custom view helper path
        $view->addHelperPath('/../library/Application/View/Helper');
        //set doctype for default layout
        $view->doctype(Zend_Registry::get('config')->resources->view->doctype);
        //set default title
        $view->headTitle('Our Home');
        //set head meta data
        $view->headMeta()->appendHttpEquiv('Content-Type', Zend_Registry::get(
                        'config')->resources->view->contentType);
        //set css includes
        $view->headLink()->setStylesheet('/css/normalize.css');
        $view->headLink()->appendStylesheet('/css/blueprint/src/liquid.css');
        $view->headLink()->appendStylesheet('/css/blueprint/src/typography.css');
        $view->headLink()->appendStylesheet(
                '/javascript/mediaelement/build/mediaelementplayer.css');
        $view->headLink()->appendStylesheet('/css/main.css');
        $view->headLink()->appendStylesheet('/css/nav.css');
        $view->headLink()->appendStylesheet('/css/table.css');
        //add javascript files
        $view->headScript()->setFile('/javascript/mediaelement/build/jquery.js');

        //add it to the view renderer
        $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper(
                        'ViewRenderer');
        $viewRenderer->setView($view);
        //Return it, so that it can be stored by the bootstrap
        return $view;
    }

I also included a convenience method _initRegistry() to make config options available everywhere with minimal code.

The layout in ZF is a simple html page with placeholder added for dynamic or configured options:

<?php
echo $this->doctype() . "\n"; //placeholder assigned in bootstrap $view->doctype
?>
<html>
    <head>
        <title></title>
        <?php echo $this->headMeta() . "\n" //placeholder assigned in bootstrap ?>
        <?php echo $this->headLink() . "\n" //placeholder assigned in bootstrap ?>
        <?php echo $this->headscript(). "\n" //placeholder assigned in bootstrap?>
    </head>
    <body>
        <section class="container">
            <header class="block">
                <hgroup id="header" class ="column span-24">
                    <h1>Our Page</h1>
                </hgroup>
                <nav>
                    <div id="nav" class="column span-24">
                        <?php echo $this->layout()->nav //custom placeholder ?>
                    </div>
                </nav>
            </header>
            <section class="block">
                <div id="main" class="column span-18 border">
                    <div id="flash">
                        <?php
                        //flash messenger display location
                        if (count($this->messages) > 0) {
                            printf("<h3 id='flash'>%s</h3>", $this->messages[0]);
                        }
                        ?>
                    </div>
                    <?php echo $this->layout()->content; //placeholder for redering views ?>
                </div>
                <aside id="sidebar" class="column span-4 last">
                    <?php echo $this->layout()->search //custom placeholder ?>
                    <div id="subNav">
                        <?php echo $this->layout()->subNav //custom placeholder ?>
                    </div>
                    <div id="adminMenu">
                        <?php echo $this->layout()->adminMenu //custom placeholder ?>
                    </div>
                </aside>
            </section>
            <footer class="block">
                <div id="footer" class="column span-24">
                    <p>Created by <em>Your Name</em> with <a href="http://framework.zend.com/">Zend Framework. &COPY; </a></p>
                </div>
            </footer>
        </section>
    </body>
</html>
<?php echo $this->inlineScript() ?> //javascript includes at bottom of page

Hope this helps.

like image 163
RockyFord Avatar answered Apr 01 '26 10:04

RockyFord


Well first off, youre initializing the layout in a method that would seem to be for your routing - probably a bad idea. Secondly if youre using the full stack with Zend_Application then you can use the provided Zend_Application_Resource_Layout and set all your options from configuration.

Additionally you dont want to use a raw include statement to pull in content you should use $this->render('thetemplate.phtml') from within your layout file. Check out the Zend_Layout Quickstart for more info on that.

like image 42
prodigitalson Avatar answered Apr 01 '26 10:04

prodigitalson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!