Search code examples
zend-frameworkzend-view

example for view and partial helpers


I want to get deeper in the topic of view helpers and partial helpers. I tried with a treeview element of a tutorial which works quite fine. This would be a good example to try with the view helpers I thought, but I'm lost. In my understanding the recursive part should be a partial?! Can someone explain how to split the following function up in the different helper parts? And how to implement in my view?

    function treeview($array, $currentParent, $currLevel = 0, $prevLevel = -1) {

    foreach ($array as $categoryId => $category) {
        if ($currentParent == $category['parent_id']) {                       
            if ($currLevel > $prevLevel) echo " <ol class='tree'> "; 
                if ($currLevel == $prevLevel) echo " </li> ";
                echo '<li> <label for="subfolder2">'.$category['name'].'</label> <input type="checkbox" name="subfolder2"/>';
                if ($currLevel > $prevLevel) { $prevLevel = $currLevel; }
                $currLevel++; 
                treeview ($array, $categoryId, $currLevel, $prevLevel);
                $currLevel--;               
            }   
        }
        if ($currLevel == $prevLevel) echo " </li>  </ol> ";
}

My view looks like follows:

require_once('../application/library/Treeview.php');
header("Content-Type: text/html; charset=utf-8");
echo $this->headLink()->prependStylesheet($this->baseUrl().'/css/tree.css'); 
$this->title = "Treeview Test";
$this->headTitle($this->titel);

$arrayCategories = array();
foreach($this->treeviewitems as $row) : 
    $arrayCategories[$row['id']] = array("parent_id" => $row['parent_id'], "name" =>$row['name']);

endforeach; 
treeview($arrayCategories, 0);

EDIT, because naming and path problem:

New viewhelper head:

class Application_View_Helper_Treeview extends Zend_View_Helper_Abstract
{
    public function treeview($array, $currentParent, $currLevel = 0, $prevLevel = -1) {

path in my application.ini (as you can see I tried before with naming stuff)

resources.view.helperPath.Company_View_Helper = "Company/View/Helper"
resources.view.helperPath.Britta_View_Helper = "Britta/View/Helper"
resources.view.helperPath.Application_View_Helper = APPLICATION_PATH "/views/helpers"
resources.view.helperPath.Zend_Dojo_View_Helper = "Zend/Dojo/View/Helper"

and now the path, where I saved the viewhelper file:

name: Treeview.php path: C:\wamp\www\riba_doc\application\views\helpers

Error: Fatal error: Call to undefined function treeview() in C:\wamp\www\riba_doc\application\views\scripts\treeview\index.phtml on line 13


Solution

  • You could do this in a little bit of a more ZF1-like way as follows.

    Create the file ./library/My/View/Helper/Treeview.php with:

    <?php
    
    class My_View_Helper_Treeview extends Zend_View_Helper_Abstract
    {
        public function treeview($array, $currentParent, $currLevel = 0, $prevLevel = -1)
        {
    
            foreach ($array as $categoryId => $category) {
                if ($currentParent == $category['parent_id']) {
                    if ($currLevel > $prevLevel) {
                        echo " <ol class='tree'> ";
                    }
                    if ($currLevel == $prevLevel) {
                        echo " </li> ";
                    }
                    echo '<li> <label for="subfolder2">' . $category['name'] . '</label> <input type="checkbox" name="subfolder2"/>';
                    if ($currLevel > $prevLevel) {
                        $prevLevel = $currLevel;
                    }
                    $currLevel++;
                    $this->treeview($array, $categoryId, $currLevel, $prevLevel);
                    $currLevel--;
                }
            }
            if ($currLevel == $prevLevel) {
                echo " </li>  </ol> ";
            }
        }
    }
    

    Next, we need to tell ZF how to find our Treeview class and to treat it as a view-helper. This is done by adding the following to our ./application/config/application.ini file:

    resources.view.helperPath.My_View_Helper_ = "My/View/Helper/"
    

    We might - not sure, aaugh! - also have to add the My_ namespace to the autoloader:

    autoloaderNameSpaces[] = "My_"
    

    Finally, we can invoke the Treeview helper in a view script using:

    <? $this->treeview($arrayCategories, 0) ?>