在 zf2 中访问 layout.phtml 中的变量
Accessing variables in layout.phtml in zf2
我在 zf2 中有一个简单的应用程序,我在布局中有用于每个应用程序页面的菜单。例如,provinces/index.phtml、districts/index.phtml、cities/index.phtml 等
菜单是:
<li class="start active ">
<a href="admin">
<i class="fa fa-home"></i>
<span class="title">
Dashboard
</span>
<span class="selected">
</span>
</a>
</li>
<li>
<a href="provinces">
<i class="fa fa-globe"></i>
<span class="title">
Provinces
</span>
</a>
</li>
<li>
<a href="districts">
<i class="fa fa-map-marker"></i>
<span class="title">
Districts
</span>
</a>
</li>
<li>
<a href="cities">
<i class="fa fa-tint"></i>
<span class="title">
Cities
</span>
</a>
</li>
我有代码:
<div class="page-content-wrapper">
<?php echo $this->content; ?> <!--this print contents of index.phtml -->
</div>
以相同的布局访问每个页面。
Now I want to make menu dynamically, i.e., when provinces is selected then provinces should highlighted, if districts is selected then districts in menu should be highlighted.
我尝试了如下逻辑:
在 provinces/index.phtml 我写代码 $selected_page="provinces", 在 districts/index.phtml 我写代码 $selected_page="districts" 等等
然后在布局的菜单中:
我写class="start active" >
地区和省等也是如此
但是这里变量$selected_page访问不到,因为
<?php echo $this->content; ?>
只能打印显示index.phtml的内容,变量不传递给layout。
那么我应该怎么做呢?我应该如何将变量 $current_page 传递给布局,或向我展示有关它的其他逻辑。
提前致谢:
屏幕截图如下,仪表板突出显示:
如果你想从controller/action(例如provincesAction()
)传递变量来查看。使用 layout()
控制器插件:
public function provincesAction()
{
$this->layout()->setVariable('foo', 'bar');
return new ViewModel([]);
}
在layout.phtml
// html code
<?php echo $this->foo; ?> // It will display "bar"
// more html code
但以你为例。我建议您使用 navigation()
查看助手。在 https://framework.zend.com/manual/2.4/en/modules/zend.navigation.view.helper.menu.html 上阅读更多内容。这个插件就是为你需要的东西而制作的。
我在 zf2 中有一个简单的应用程序,我在布局中有用于每个应用程序页面的菜单。例如,provinces/index.phtml、districts/index.phtml、cities/index.phtml 等
菜单是:
<li class="start active ">
<a href="admin">
<i class="fa fa-home"></i>
<span class="title">
Dashboard
</span>
<span class="selected">
</span>
</a>
</li>
<li>
<a href="provinces">
<i class="fa fa-globe"></i>
<span class="title">
Provinces
</span>
</a>
</li>
<li>
<a href="districts">
<i class="fa fa-map-marker"></i>
<span class="title">
Districts
</span>
</a>
</li>
<li>
<a href="cities">
<i class="fa fa-tint"></i>
<span class="title">
Cities
</span>
</a>
</li>
我有代码:
<div class="page-content-wrapper">
<?php echo $this->content; ?> <!--this print contents of index.phtml -->
</div>
以相同的布局访问每个页面。
Now I want to make menu dynamically, i.e., when provinces is selected then provinces should highlighted, if districts is selected then districts in menu should be highlighted.
我尝试了如下逻辑: 在 provinces/index.phtml 我写代码 $selected_page="provinces", 在 districts/index.phtml 我写代码 $selected_page="districts" 等等
然后在布局的菜单中:
我写class="start active" >
地区和省等也是如此
但是这里变量$selected_page访问不到,因为
<?php echo $this->content; ?>
只能打印显示index.phtml的内容,变量不传递给layout。 那么我应该怎么做呢?我应该如何将变量 $current_page 传递给布局,或向我展示有关它的其他逻辑。
提前致谢:
屏幕截图如下,仪表板突出显示:
如果你想从controller/action(例如provincesAction()
)传递变量来查看。使用 layout()
控制器插件:
public function provincesAction()
{
$this->layout()->setVariable('foo', 'bar');
return new ViewModel([]);
}
在layout.phtml
// html code
<?php echo $this->foo; ?> // It will display "bar"
// more html code
但以你为例。我建议您使用 navigation()
查看助手。在 https://framework.zend.com/manual/2.4/en/modules/zend.navigation.view.helper.menu.html 上阅读更多内容。这个插件就是为你需要的东西而制作的。