Sigma and nested blocks: replace outer block by inner one

From: Date: Thu, 15 Dec 2005 09:53:13 +0000
Subject: Sigma and nested blocks: replace outer block by inner one
Groups: php.pear.general 
Request: Send a blank email to pear-general+get-22254@lists.php.net to get a copy of this message
Hi In my application that uses Template Sigma I have a navigation with the sub-navigation nested into the main navigation. The navigation part of the template is like this: <!-- BEGIN navigation_1 --> <div class="mainnavi"><a href="{href1}">{name1}</a></div> <!-- BEGIN navigation_2 --> <div class="subnavi"><a href="{href2}">{name2}</a></div> <!-- END navigation_2 --> <!-- END navigation_1 --> Now I need to find a way to hide the level 1 part in some cases, but still show the level 2 part. I try to do this with the replaceBlock method, but there seems to be a mistake in my approach. This is the function I wrote for recursively parsing all levels of navigation (it should be applicable for more than 2 levels anyway): // Structure array given to the function: $structure[$id] = array(
    'name' => 'menuname',
    'href' => '/name.html',
    'hide' => true,
    'subpages' => array([same structure as $structure])
); function output_navigation($structure, $level=0) {
    global $tpl;
    foreach ($structure as $id => $entry) {
        $mainblock = "navigation_".($level+1);
        $subblock  = "navigation_".($level+2);
        // recursively parse subblocks
        if (!empty($entry['subpages']) && $tpl->blockExists($subblock)) {
            output_navigation($entry['subpages'], $level+1);
        }
        if ($tpl->blockExists($mainblock)) {
            $tpl->setCurrentBlock($mainblock);
            // Try to hide the main entry:
            // replace the mainblock by the subblock
            // That's what does not work...
            if (($entry['hide']) && isset($subblock) && $tpl->blockExists($subblock)) {
                $tpl->replaceBlock($mainblock, $tpl->get($subblock));
                // Show the replacement for debugging:
                print_r($tpl->get($subblock)); flush();
                // Outputs: "Error Cannot find block 'navigation_2'"
            }
            else {
                $tpl->setVariable(array('name'.$level => $entry['name'], 'href'.$level => $entry['href']));
            }
            $tpl->parseCurrentBlock($mainblock);
        }
    }
} It all works well as long as $entry['hide'] is false. But in hiding mode, I can't get it working. The condition $tpl->blockExists($subblock) is actually true, but $tpl->get($subblock) makes an error object "Cannot find block". I would highly appreciate to be pointed to my mistake or to other possibilities of hiding the outer block and outputting the inner one! -- Markus

« previous php.pear.general (#22254) next »