开发者

Accessing zend view variables from breadcrumb partial

开发者 https://www.devze.com 2023-04-11 00:13 出处:网络
How can I acces开发者_StackOverflow中文版s a variable that is defined in the actual view from a breadcrumb partial?

How can I acces开发者_StackOverflow中文版s a variable that is defined in the actual view from a breadcrumb partial?

I was trying with $this->myVar but I dont get anything.

I also try this and it works:

$view = Zend_Layout::getMvcInstance()->getView();
echo $view->myVar 

Is this correct or there's a better way?


Above answer by Davi Harkness is gr8 but if you still want to use it like $view->var then you don't even need to use partial view helper for that simply do

   $view = new Zend_View();
    $paths = $this->view->getScriptPaths();
    $view->addScriptPath($paths[0]);
    $view->name = "open source";
    $test = $view->render("test.phtml");
    echo $test;

Where test.phtml is inside /views/scripts dir of current module and contains

<?php echo $this->name?>


The Partial View Helper documentation specifically states that it "is used to render a specified template within its own variable scope." It does this by cloning the view and clearing out all existing variables in its cloneView() method:

public function cloneView()
{
    $view = clone $this->view;
    $view->clearVars();
    return $view;
}

Instead of coupling the partial to the views that call it you should have those views pass in the values the partial needs in an array.

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

Then the partial view script can access $this->from and $this->subject.

<?php // partial.phtml ?>
<ul>
    <li>From: <?php echo $this->escape($this->from) ?></li>
    <li>Subject: <?php echo $this->escape($this->subject) ?></li>
</ul>
0

精彩评论

暂无评论...
验证码 换一张
取 消