This post refers to DotKernel 1, based on Zend Framework 1.
If you are looking for DotKernel 3 related posts, go here.

If you want to list items in a 4×4 grid such as a gallery you will need blocks on two layers, lines and columns, which means that you will use nested blocks.

Nested blocks are blocks inside other blocks.

There will be at least two variables representing the dimensions of the grid(user choice), eg: Lines & Columns/ All Items & Items per Line.

For this example we assume that the paginator in order to return only LC results per page, and only check when to go to the next row block.

LC – number of lines * number of columns

First you must create a block with the parent tpl_main (tpl_main is the main content of the web page). This block will be the row block.

Second you must create a block having as parent the row block. This will obviously be the column block.

Below is the visualization of the nested block layout on two levels, blocks could also be nested on three or more levels.

Nested Blocks
Nested Blocks Hierarchy

The Code

After setting the file you set both the row and the column blocks, but make sure that the parent block(list_row in this case) is set first and its parent is tpl_main, the other one will be the child of the list_row.

Next we will iterate the array almost like a normal block, but this time we check if the current value(column) is the last one from the current line. We recommend using a counter ($k in this example)

Note: if at the end ($k % $width == 0) is not true you must parse the remaining columns with empty values, and parse the last line (which was earlier incomplete)!

PHP (View)

public function showItems($templateFile, $items, $width=4)
   $this->tpl->setFile('tpl_main', 'gallery/' . $templateFile . '.tpl');
   $this->tpl->setBlock('tpl_main', 'list_row', 'list_row_block');
   $this->tpl->setBlock('list_row', 'list_col', 'list_col_block');
   foreach ($items as $value)
      $this->tpl->setVar('TITLE', $value['title']);
      $this->tpl->setVar('DESCRIPTION', $value['description']);
      $this->tpl->parse('list_col_block', 'list_col', true);
      if ($k % $width == 0)
         $this->tpl->setVar('LAST_CLASS', '');
         $this->tpl->parse('list_row_block', 'list_row', true);
         $this->tpl->parse('list_col_block', '');

Template File


One Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>