A block is a dynamic data model. The purpose of a block is to replace the static view model of MVC application by using a model that can be updated at run time—that is, by using a dynamic block.
In short blocks are dynamic data models that use block templates to make the data visible to the user in the way you want.
Block templates are fragments of markup for rendering shapes. Examples of shapes include menus, menu items, newsletter, quick contact, mini login, and messages.
In the Zend framework the block is a Partial view helper is used to render a specified template within its own variable scope. The primary use is for reusable template fragments with which you do not need to worry about variable name clashes. Additionally, they allow you to specify partial view scripts from specific modules.
A sibling to the Partial, the PartialLoop view helper allows you to pass iterable data, and render a partial for each item. Click Here to know more about Zend Partial view helper.
Basic Usage of Partials by Zend Framework Docs
Basic usage of partials is to render a template fragment in its own view scope. Consider the following partial script:
-
<?php // partial.phtml ?>
-
<ul>
-
<li>From:
<?php echo $this->
escape($this->
from) ?></li>
-
<li>Subject:
<?php echo $this->
escape($this->
subject) ?></li>
-
</ul>
You would then call it from your view script using the following:
-
<?php echo $this->
partial('partial.phtml',
array(
-
'from' => 'Team Framework',
-
'subject' => 'view partials')); ?>
-
<ul>
-
<li>From: Team Framework</li>
-
<li>Subject: view partials</li>
-
</ul>
Note: What is a model?
A model used with the Partial view helper can be one of the following:
-
Array. If an array is passed, it should be associative, as its key/value pairs are assigned to the view with keys as view variables.
-
Object implementing toArray() method. If an object is passed an has a toArray() method, the results of toArray() will be assigned to the view object as view variables.
-
Standard object. Any other object will assign the results of object_get_vars() (essentially all public properties of the object) to the view object.
If your model is an object, you may want to have it passed as an object to the partial script, instead of serializing it to an array of variables. You can do this by setting the 'objectKey' property of the appropriate helper:
-
// Tell partial to pass objects as 'model' variable
-
$view->partial()->setObjectKey('model');
-
-
// Tell partial to pass objects from partialLoop as 'model' variable
-
// in final partial view script:
-
$view->partialLoop()->setObjectKey('model');
This technique is particularly useful when passing Zend_Db_Table_Rowsets topartialLoop(), as you then have full access to your row objects within the view scripts, allowing you to call methods on them (such as retrieving values from parent or dependent rows).
Sample Block Example :