开发者

Zend framework rendering custom place holders in layout

I have some custom place holders in layout file, like [Region_Contents] now I want to replace these placeholders with my custom html as layout is rendered like instead 开发者_高级运维of displaying [Region_Contents] it may show

Hello this is test block

is there any way to do this?


You can use view filters for this. First we have to implement the Zend_Filter_Interface like so:

class My_View_Filter_PlaceholderReplacer implements Zend_Filter_Interface
{
    public function filter($value) 
    {
        return str_replace('[Region_Contents]', 'Hello this is test block', $value);
    }
}

In the code above, $value contains the string representation of the view just before it is displayed. Whatever is returned by the function above will be used by ZF when rendering the view. Note that we're using str_replace over preg_replace for performance reasons.

Next, we need to tell ZF to use the filter we just made. You can do this in the bootstrap.

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
  protected function _initViewSettings()
  {
    $this->bootstrap('view');
    $view = $this->getResource('view');
    $view->addFilterPath('My/View/Filter', 'My_View_Filter');
    $view->setFilter('PlaceholderReplacer');
    ...
  }
  ...
}

For more info, please refer to the following links:

Zend Manual

Zend Framework and Translation


If it's not necessary to keep the same syntax you describe above, you might just use the standard Zend_View placeholder view helpers: http://framework.zend.com/manual/en/zend.view.helpers.html#zend.view.helpers.initial.placeholder

Hope that helps,

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜