Search:     Advanced search

Creating Custom Blocks

Article ID: 50
Last updated: 08 Jul, 2011
Revision: 1
Export to PDF
Email to friend
Add comment
Views: 0
Comments: 0

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:

  1. <?php // partial.phtml ?>
  2. <ul>
  3.     <li>From: <?php echo $this->escape($this->from) ?></li>
  4.     <li>Subject: <?php echo $this->escape($this->subject) ?></li>
  5. </ul>

You would then call it from your view script using the following:

  1. <?php echo $this->partial('partial.phtml'array(
  2.     'from' => 'Team Framework',
  3.     'subject' => 'view partials'))?>

Which would then render:

  1. <ul>
  2.     <li>From: Team Framework</li>
  3.     <li>Subject: view partials</li>
  4. </ul>

NoteWhat 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:
  1. // Tell partial to pass objects as 'model' variable
  2. $view->partial()->setObjectKey('model');
  4. // Tell partial to pass objects from partialLoop as 'model' variable
  5. // in final partial view script:
  6. $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 :

This article was:  
Also read
document Customizing the template design layout.
document Default location of Templates , JS , CSS and Images.
document Designing various templates blocks
document Concept of MVC Modules

Also listed in
folder Site Building Guide
folder Templates Customization
folder Third Party Integration
folder Developer Documentation

Prev   Next
Customizing the template design layout.     Language architecture and it hierarchical overview