Zend reusable widgets / plugins / miniapplications?
Asked Answered
D

1

3

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?

Dottie answered 2/3, 2011 at 17:31 Comment(0)
C
6

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!

Countermeasure answered 2/3, 2011 at 21:43 Comment(2)
thanks for your response! afaik i can access models from view helper for read operations. But Accessing models for edits/updates/writes from view helpers is not recommended -right?Dottie
I wouldn't. I leave updates and insertions to the form or controller. If you have an update/insertion query you plan on reusing I would recommend added the logic directly in the model itself.Countermeasure

© 2022 - 2024 — McMap. All rights reserved.