Zend reusable widgets / plugins / miniapplications?

1.1k views Asked by At

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

There are 1 answers

2
Fatmuemoo On BEST ANSWER

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!