0

私のzendレイアウトとスクリプトは正常に検出されています。しかし、IndexControllerのinit関数では、$ this-> view-> render( "header.phtml")と記述しますが、echo($ this-> view-> render( ")と書くと、画面には何も表示されません。 header.phtml ");私のheader.phtmlファイルを表示します。これが私のIndexControllerです。

クラスIndexControllerはZend_Controller_Actionを拡張します{

public function init()
{
    $layout = $this->_helper->layout();
    //$this->view = $this->getResource('View');
    echo ($this->view->render("header.phtml"));
        //echo"<pre>";
        //var_dump($this->view);
        //var_dump(APPICATION_PATH);
}

public function indexAction()
{
    // action body
    echo "In default index con";
}

}

また、URLを/ mypath / indexにIgiveすると、エコーしている「I'minindexcontroller」という単純な行が表示されません。私のブートストラップには、Zend_Layout設定があります。

        Zend_Loader::loadClass('Zend_View');
$this->view = new Zend_View();
$this->view->setEncoding('UTF-8');
$this->view->setScriptPath($this->root .'/application/default/views/scripts');
    $viewRenderer=new Zend_Controller_Action_Helper_ViewRenderer();
     // Zend_Controller_Action_HelperBroker::addHelper($viewRenderer);
     // $view->setScriptPath($this->root.'/'.$theme.'/views/scripts/');
     //$view->setScriptPath($this->root.'/application/default/views/scripts/');
     $this->view->addScriptPath($this->root.'/application/admin/views/scripts');
     $this->view->addScriptPath($this->root.'/application/business/views/scripts');
    // $this->view->setHelperPath($this->root .'/application/default/views/helpers');
     $this->layout = Zend_Layout::startMvc(
        array(
                'layoutPath' => $this->root . '/application/default/views/'.$crt_theme.'/layouts',
                'layout' => 'layout'
                )
                );
        $this->registry->set("theme", $crt_theme);

変数$crt_themeは「デフォルト」に設定されています。

4

2 に答える 2

6

ロブの答えは正しいですが、これを複雑にするために懸命に取り組んでいるように見えるため、さらに情報が必要になる場合があります。
ZF を MVC として使用する場合、それらを使用するためのレイアウトとデフォルトを配置する場所があります。

Zend_Tool コマンド ライン インターフェイスを使用している場合は、次のコマンドで開始します。zf enable layoutツールはデフォルト ディレクトリとデフォルトのlayout.phtmlをプロジェクトに追加します。application.ini
に次の行を追加します。

resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/"

そのパスにファイルが追加されlayout.phtml
ますデフォルトのレイアウトの名前を変更する必要がある場合は、スクリプトの名前を.phtmlなしでこの行に追加します

resources.layout.layout = master

このファイルの使い方は思いつく限りたくさんありますが、ここでは私の使い方の例を示します。
プロジェクトのデフォルトを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"

次に、ブートストラップで、使用するビューをセットアップします。ここでそれを行うので、複数のレイアウトがある場合 (通常はそうします)、css または js ファイルを 1 か所で簡単に変更できます。

protected function _initView() {
        //Initialize view
        $view = new Zend_View();

        $view->addHelperPath('/../library/Application/View/Helper');

        $view->doctype(Zend_Registry::get('config')->resources->view->doctype);

        $view->headTitle('Our Home');

        $view->headMeta()->appendHttpEquiv('Content-Type', Zend_Registry::get(
                        'config')->resources->view->contentType);
        $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');
        $view->headScript()->appendFile('/javascript/modernizr.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;
    }

headLink()、headScript()、および docType() エントリのすべてに注意してください。これらは、レイアウトで使用されるplaceholdersのデータが設定されている場所です。

これで、レイアウト、他のアクション ベースのスクリプトからの実際のコンテンツは通常、プレースホルダーによってレンダリングされます。$this->layout()->content

<?php
echo $this->doctype() . "\n";//placeholder
?>
<html>
    <head>
        <title></title>
        <?php echo $this->headMeta() . "\n" ?><!-- displays all meta data passed -->
        <?php echo $this->headLink() . "\n" ?><!-- displays all links passed -->
        <?php echo $this->headscript(). "\n"?><!-- displays all scripts passed -->
    </head>
    <body>
        <section class="container">
            <header class="block">
                <hgroup id="header" class ="column span-24">
                    <h1>Our Home</h1>
                </hgroup>
                <nav>
                    <div id="nav" class="column span-24">
                        <?php echo $this->layout()->nav ?>&nbsp;<!-- 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; ?><!-- Default placeholder, where views are rendered -->
                </div>
                <aside id="sidebar" class="column span-4 last">
                    <?php echo $this->layout()->search ?><!-- Custom placeholder -->
                    <div id="subNav">
                        <?php echo $this->layout()->subNav ?>&nbsp;<!-- Custom placeholder -->
                    </div>
                    <div id="adminMenu">
                        <h4>Administration Links</h4>
                        <?php echo $this->layout()->adminMenu ?>&nbsp;<!-- Custom placeholder -->
                    </div>
                </aside>
            </section>
            <footer class="block">
                <div id="footer" class="column span-24">
                    <p>Created with <a href="http://framework.zend.com/">Zend Framework. &COPY; </a></p>
                </div>
            </footer>
        </section>
        <?php echo $this->inlineScript() ?><!-- placeholder -->
    </body>
</html>

お役に立てれば!

[編集] もう1つ、これは常に次の質問のようです。「コントローラー/アクションのレイアウトをデフォルトから変更するにはどうすればよいですか?」

コントローラーからレイアウトを変更するには、通常は preDispatch() メソッドを使用して行い、新しいレイアウトの名前をレイアウト ヘルパーに渡すだけです。

$this->_helper->layout->setLayout('myOtherLayout');

これを行うだけで、コントローラー内のすべてのアクションのレイアウトが変更されます。より選択的にするには、次のような条件を使用できます。

if ($this->getRequest()->getActionName() == 'player') {
            $this->_helper->layout->setLayout('player');//reset layout
            //add 2 new headscripts
            $this->view->headScript()->appendFile(
                    'http://ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js'
                    );
            $this->view->headScript()->appendFile(
                    '/javascript/mediaplayer/jwplayer.js'
                    );
        }
于 2012-05-02T05:21:32.630 に答える
1

render()意図したとおりに機能しています。それはあなたがすべき文字列を返しますecho

ただし、コントローラーで直接レンダリングすることは非常にまれです。少なくとも、ヘッダーとフッターを からレンダリングする必要がありますがviews/scripts/index/index.phtml、を使用Zend_Layoutするとさらに効果的です。を使用している場合は、次を追加するだけでZend_Application使用を開始できます。Zend_Layout

resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/"

あなたのapplication/config/application.iniファイルに。application/layouts/scripts/layout.phtml次に、次のようなファイルを作成する必要があります。

<?php
$this->headMeta()->appendHttpEquiv('Content-Type', 'text/html;charset=utf-8');
$this->headTitle()->setSeparator(' - ');
$this->headTitle('My website');
?>
<!DOCTYPE html>
<html> 
<head>
    <?php echo $this->headMeta(); ?> 
    <?php echo $this->headTitle(); ?>
    <!-- Other <head> elements and view helpers here -->
</head>
<body>
<div id="content">
    <?php echo $this->layout()->content; ?>
</div>
</body>
</html>
于 2012-05-02T05:02:50.033 に答える