开发者

Pass a variable from view to layout in CakePHP - or where else to put this logic?

开发者 https://www.devze.com 2023-02-19 02:53 出处:网络
I want to populate an array $pageWidgets in my views so I can control what widgets appear in my sidebar and with what information in my layout but from my view. My only problem is that the layout seem

I want to populate an array $pageWidgets in my views so I can control what widgets appear in my sidebar and with what information in my layout but from my view. My only problem is that the layout seems to be parsed before the view (or some other block) and it isn't working.

Quite simply my $pageWidgets array contains a key with an element name and value of data from my model (ie: $pageWidgets['product_meta'] = $product; and the main layout simply loops through this array and populates the sidebar.

My app is in it's infancy so any other methods开发者_运维技巧 of registering widgets for a page will be taken seriously, but it seemed to makesense that this data be in the view.

I'm using CakePHP 1.3.


I actually use the same technique for the same purpose. However, you have to set the array within the view like this:

$this->set('widgetsToRender', array(foobar));

Then, in your layout or in an element file, you can make use of that array. Example:

<?php
if (in_array('some_widget', $widgetsToRender)):
// do whatever

You can also set a default value for this array within the layout or element in question, like:

if (empty($widgetsToRender)) {
    $widgetsToRender = array(default value);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消