Zend framework zend框架两步视图

Zend framework zend框架两步视图,zend-framework,Zend Framework,我是zendframework的新手。我正在尝试实施两步视图布局: php(view/Bootstrap.php) phtml(应用程序/视图/脚本/布局/布局.phtml) //在这里查看内容。 //页脚在这里。 我是一个绝对的初学者,请逐步解释。谢谢。首先,您正在使用一种似乎适合您的路由的方法初始化布局-可能是个坏主意。其次,如果您使用的是完整的堆栈,那么您可以使用提供的并设置所有的from 此外,您不希望使用原始的include语句从布局文件中提取内容,您应该使用$this->re

我是zendframework的新手。我正在尝试实施两步视图布局:

php(view/Bootstrap.php)


phtml(应用程序/视图/脚本/布局/布局.phtml)


//在这里查看内容。
//页脚在这里。

我是一个绝对的初学者,请逐步解释。谢谢。

首先,您正在使用一种似乎适合您的路由的方法初始化布局-可能是个坏主意。其次,如果您使用的是完整的堆栈,那么您可以使用提供的并设置所有的from


此外,您不希望使用原始的
include
语句从布局文件中提取内容,您应该使用
$this->render('thetemplate.phtml')
。查看以了解更多信息。

启用布局的最简单方法是从命令行
zf enable layout
运行Zend_Tool命令,这将添加该行
resources.layout.layoutPath=APPLICATION\u PATH”/layouts/scripts/“

到application.ini,并为布局和默认文件
layout.phtml

构建目录

或者,您可以在application.ini文件中用两行指定布局路径和默认布局名称:

其他布局/视图选项可能在application.ini中设置,以便在视图中调用:

;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"
 /**
     * 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;
    }
然后在bootstrap.php中,您可以调用这些资源来初始化视图:

;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"
 /**
     * 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;
    }
我还提供了一个方便的方法_initRegistry(),使配置选项在任何地方都可用,只需最少的代码。

ZF中的布局是一个简单的html页面,为动态或配置选项添加了占位符:

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

我们的网页

谢谢你给我时间回答我的问题。我只是按照你在这里解释的方式实施。但是我面临一些困难。echo his->doctype()工作得很好,但是所有其他函数,如$this->headMeta(),$this->headLink()工作不正常。原因是什么?感谢初学者,从zend程序员指南中学习是非常困难的。我需要对每一段代码进行更多的解释;如果您不介意的话,请您让我参考帮助我解决这个问题的基础教程。$this->layout()->content,$this->layout()->subNav,echo$this->layout()->adminMenu您能解释一下这个代码吗?这是个问题吗?您的名字是什么?就教程而言,我能找到的最好的是。在我的示例中,有些占位符是自定义的,要实现的代码并不存在,事实上,目前它们只是尚未编写的代码的占位符。
<?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