operate layout

Accessing the Layout Object

On occasion, you may need direct access to the layout object. There are three ways you can do this:

  • Within view scripts: use the layout() view helper, which returns the Zend_Layout instance registered with the front controller plugin.

    1. <?php $layout = $this->layout(); ?>

    Since it returns the layout instance, you can also simply call methods on it, rather than assigning it to a variable.

  • Within action controllers: use the layout() action helper, which acts just like the view helper.

    1. // Calling helper as a method of the helper broker:
    2. $layout = $this->_helper->layout();
    3.  
    4. // Or, more verbosely:
    5. $helper = $this->_helper->getHelper('Layout');
    6. $layout = $helper->getLayoutInstance();

    As with the view helper, since the action helper returns the layout instance, you can also simply call methods on it, rather than assigning it to a variable.

  • Elsewhere: use the static method getMvcInstance(). This will return the layout instance registered by the bootstrap resource.

    1. $layout = Zend_Layout::getMvcInstance();
  • Via the bootstrap: retrieve the layout resource, which will be the Zend_Layout instance.

    1. $layout = $bootstrap->getResource('Layout');

    Anywhere you have access to the bootstrap object, this method is preferred over using the static getMvcInstance() method.

Other Operations

In most cases, the above configuration and layout script (with modifications) will get you what you need. However, some other functionality exists you will likely use sooner or later. In all of the following examples, you may use one of the methods listed above for retrieving the layout object.

  • Setting layout variables. Zend_Layout keeps its own registry of layout-specific view variables that you can access; the $content key noted in the initial layout script sample is one such example. You can assign and retrieve these using normal property access, or via the assign() method.

    1. // Setting content:
    2. $layout->somekey = "foo"
    3.  
    4. // Echoing that same content:
    5. echo $layout->somekey; // 'foo'
    6.  
    7. // Using the assign() method:
    8. $layout->assign('someotherkey', 'bar');
    9.  
    10. // Access to assign()'d variables remains the same:
    11. echo $layout->someotherkey; // 'bar'
  • disableLayout(). Occasionally, you may want to disable layouts; for example, when answering an Ajax request, or providing a RESTful representation of a resource. In these cases, you can call the disableLayout() method on your layout object.

    1. $layout->disableLayout();

    The opposite of this method is, of course, enableLayout(), which can be called at any time to re-enable layouts for the requested action.

  • Selecting an alternate layout: If you have multiple layouts for your site or application, you can select the layout to use at any time by simply calling the setLayout() method. Call it by specifying the name of the layout script without the file suffix.

    1. // Use the layout script "alternate.phtml":
    2. $layout->setLayout('alternate');

    The layout script should reside in the $layoutPath directory specified in your configuration. Zend_Layout will then use this new layout when rendering.                 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值