Zend框架:使用BreadCrumbs和partial

时间:2012-01-02 14:10:54

标签: zend-framework breadcrumbs

制作自定义面包屑时遇到一些问题: 我的layout.phtml我有:

$container = new Zend_Navigation();
$this->navigation($container);

$container->addPage(
array(
    'label'      => 'Dashboard',
    'module'     => 'default',
    'controller' => 'dashboard',
    'action'     => 'index',
    'pages'      =>
    array(
        array(
            'label'      => 'Create Order',
            'module'     => 'default',
            'controller' => 'createorder',
            'action'     => 'index'
        ),
        array(
            'label'      => 'Query',
            'module'     => 'default',
            'controller' => 'query',
            'action'     => 'index',
            'pages' => array(
                array(
                    'label'      => 'View Order',
                    'module'     => 'default',
                    'controller' => 'order',
                    'action'     => 'vieworder'
                )
            )
        ),
        array(
            'label'      => 'Administration',
            'module'     => 'default',
            'controller' => 'admin',
            'action'     => 'index',
            'pages'      =>
            array(
                array(
                    'label'      => 'News and Announcements',
                    'module'     => 'default',
                    'controller' => 'admin',
                    'action' => 'addnews',
                    'pages' => array(
                        array(
                            'label'      => 'Edit News and Announcements',
                            'module'     => 'default',
                            'controller' => 'admin',
                            'action' => 'editnews'
                        )
                    )
                )
            )
        )
    )
)
);

下一行是:

echo $this->navigation()->breadcrumbs()->setPartial(array('BreadCrumb.phtml','default'));

BreadCrumb.phtml调用得很好,但我不知道如何在BreadCrumb.phtml中创建ul-li菜单。我如何获得我实际进入的导航? 在此先感谢您的帮助。安德烈

3 个答案:

答案 0 :(得分:4)

为此,您的BreadCrumb.phtml应如下所示:

<?php
if (null === $this->container) {
    $this->container = $this->breadcrumbs()->getContainer();
}

// find deepest active
if (!$active = $this->breadcrumbs()->findActive($this->container)) {
    return '';
}

$active = $active['page'];

// put the deepest active page last in breadcrumbs
if ($this->breadcrumbs()->getLinkLast()) {
    $html = ' <li>' . $this->breadcrumbs()->htmlify($active) . '</li>' . PHP_EOL;
} else {
    $html = $active->getLabel();
    if ($this->breadcrumbs()->getUseTranslator() && $t = $this->breadcrumbs()->getTranslator()) {
        $html = $t->translate($html);
    }
    $html = ' <li>' . $this->escape($html) . '</li>' . PHP_EOL;
}

// walk back to root
while (($parent = $active->getParent()) != null) {
    if ($parent instanceof Zend_Navigation_Page) {
        // prepend crumb to html
        $html = ' <li>' . $this->breadcrumbs()->htmlify($parent) . '</li>' . PHP_EOL . $html;
    }

    if ($parent === $this->container) {
        // at the root of the given container
        break;
    }

    $active = $parent;
}
echo strlen($html) ? $this->breadcrumbs()->getIndent() . '<ul id="breadcrumbs">' . PHP_EOL 
                 . $html .  '</ul>' . PHP_EOL : '';
?>

然后你会得到这样的东西:

<ul id="breadcrumbs">
    <li><a href="/home">Home</a></li>
    <li><a href="/articles">Articles</a></li>
    <li><a href="/shop">Shop</a></li>
    <li>Last link</li>
</ul>

答案 1 :(得分:0)

在视图脚本中,可以通过$this->container检索zend导航容器。容器是可迭代的,因此您可以使用简单的foreach循环(例如foreach($this->container as $page)

)来遍历它

答案 2 :(得分:0)

我已经找到了包裹在ul-li菜单中的渲染面包屑的简单解决方案。

您可以将此代码放在 breadcrumbs.phtml 中:

function drawFrame(frameX, frameY, canvasX, canvasY) {
  ctx.drawImage(img,
                frameX * width, frameY * height,
                width, height,
                x_click, y_click,
                scaledWidth, scaledHeight);
}

// Number of frames in animation
var cycleLoop = [3, 2, 1, 0, 7, 6, 5];
// Position of sprite in sheet
var currentLoopIndex = 0;
var frameCount = 0;

var animationid = null;
function step() {

  frameCount++;
  if (frameCount < 30) {
    animationid = window.requestAnimationFrame(step);
    return;
  }
  frameCount = 0;
  // ctx.clearRect(0, 0, canvas.width, canvas.height);
  drawFrame(cycleLoop[currentLoopIndex++], 0, 0, 0);
  // Starts animation over
  if (currentLoopIndex >= cycleLoop.length) {
    // If you want to loop back in oposite direction after full animation
    cycleLoop.reverse();
    // Reseting position of which sprite to use
    currentLoopIndex = 0;
  }
  animationid = window.requestAnimationFrame(step);
}

canvas.addEventListener("mousedown", getPosition, false);
function getPosition(event) {
   x_click = event.x;
   y_click = event.y;

   x_click -= canvas.offsetLeft * 10;
   y_click -= canvas.offsetTop * 10;
   frameCount = currentLoopIndex = 0;
   window.cancelAnimationFrame(animationid);
   step();
}