Zend reusable widgets / plugins / gadgets?

I am new to Zend Framework and trying to get an idea of ​​the possibility of code reuse. I definitely know about modules, but there seems to be some uncertainty about which functionality should go into modules and which should not.

What am I trying to do:

1) have reusable mini-programs / widgets / plugins (whatever you call them) that you can simply connect to any site by doing this in a layout or view:

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

or this in the view:

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

I would call them an extension. so basically what you see in Joomla / WordPress / Concrete5 templates.

2) All code associated with this particular extension must be in a separate directory.

3) We should be able to display extensions only for certain modules / controllers where they are required. they should not be displayed unnecessarily unless they are displayed.

4) Each extension can display several areas of content on a page.

Do you have a well thought out structure / approach that you are using?

+3
zend-framework
source share
1 answer

It looks like you need to study viewing helpers . Viewing helpers can be simple as it returns the version number of the application or as complex as adding html to multiple place owners. 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 foo.phtml script view for example:

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

Now, if you want to use this again and 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 the foo.pthml file with:

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

Both examples of foo.phtml output:

Hello World!

Hello World!

Bar Bar!

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

+6
source share

All Articles