Zend reusable widgets / plugins / miniapplications?

百般思念 提交于 2019-12-13 14:52:11

问题


I'm new to Zend framework and trying to get some insights about code re-usability. I definitely know about modules but there seems to be a bit of uncertainty about what functionality should go into modules and what not.

What I'm trying to accomplish:

1) to have reusable mini programs/widgets/plugins (whatever you may call them) that one can simply plug into any site be doing this in layout or view:

<?php echo $this->contactform;?>

or this in the view:

<?php echo $this->layout()->blog;?>

I'd call them extension. so basically sort of what you'd see in Joomla/ WordPress/Concrete5 templates.

2) All code that is related to that specific extension should be in it's separate directory.

3) We should be able to output extensions only for certain modules/controllers where they are required. they shouldn't be rendered needlessly if it won't be displayed.

4) each extension may output multiple content areas on the page.

Do you have a nicely laid out structure / approach that you use?


回答1:


It sounds like you need study up on view helpers. View helpers can be a simple as returning the App Version number or as complicated as adding html to multiple place holders. For example:

layout.phtml:

<h1><?php echo $this->placeholder('title'); ?>
<div class="sidebar">
    <?php echo $this->placeholder('sidebar'); ?>
</div>
<div class="content">
    <?php echo $this->layout()->content; ?>
</div>

in your view script foo.phtml for example:

<?php
    $this->placeholder('title')->set('Hello World!');
    $this->placeholder('sidebar')->set('Hello World!');
?>
<h1>Bar Bar!</h1>

Now if you want to be able to reuse that over and over again you can do this:

<?php
class Zend_View_Helper_MyHelper extends Zend_View_Helper_Abstract
{
    public function myHelper()
    {
        $this->view->placeholder('title')->set('Hello World!');
        $this->view->placeholder('sidebar')->set('Hello World!');
        return '<h1>Bar Bar!</h1>';
    }
}

Now, replace the code in your foo.pthml with:

<?php
echo $this->myHelper();

Both examples of foo.phtml output:

Hello World!

Hello World!

Bar Bar!

Of course this is very simplified example, but I hope this helps point you in the right direction. Happy Hacking!



来源:https://stackoverflow.com/questions/5171160/zend-reusable-widgets-plugins-miniapplications

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!