在View Script中使用css DIV区域

时间:2015-04-19 16:51:38

标签: html css zend-framework2 zend-form

在我的布局脚本中,我有一些css区域,例如:

<div id="section-navigation">
            Test
</div>

如何在视图脚本中使用其他数据覆盖这些部分?我想动态地覆盖它。

这是我的layout.phtml的片段

<head>
<?php echo $this->headMeta(); ?>
<?php echo $this->headTitle(); ?>
<?php echo $this->headLink()->prependStylesheet($this->baseUrl().'/css/grid.css'); ?>
</head>
<body>
<div id="container">
    <div id="header">
        <h1><img src="./Images/cheyenne.jpg" alt="cheyenne-IT consulting" width="490" height="115" /><?php echo $this->escape($this->title); ?></h1>
    </div>
    <div id="navigation">
        <ul>
                <li><a href="<?php echo $this->url(array('controller'=>'arbeitskalender', 'action'=>'index'));?>">Arbeitskalender</a></li>

        </ul>
    </div>
    <div id="content-container">
        <div id="section-navigation">
            test
        </div>

        <div id="aside">
            test
        </div>
        <div id="content">
            <?= $this->layout()->content ?>
        </div>

  </div>
  <div id="footer">
            copyright 2014  
    </div>
</div>
</body>

表单布局中可能还有另一个属性?

$this->layout()->content 

1 个答案:

答案 0 :(得分:3)

如果导航<div>中的链接会针对每个操作进行更改,并且您想要以动态方式设置它,那么您可以创建自定义视图脚本文件并在布局中呈现它。

这样的事情:

<强> navigation.phtml:

<ul>
    <li><a href="<?php echo $this->link; ?>"><?php echo $this->name; ?></a></li>
</ul>

然后将其添加到view_manager中的module.config

'view_manager' => array(
    'template_map' => array(
         //.....
        'navigation/view' => __DIR__ . '/../view/layout/navigation.phtml',
    ),

操作(控制器):

$url = $this->url()->fromRoute('route-name',
                                array('controller'=>'arbeitskalender', 
                                      'action'=>'index')
                              );
$navlinks = new ViewModel(
                array(
                   'link'    => $url ,
                   'name'    => 'Arbeitskalender'
                )
);
$navlinks ->setTemplate('navigation/view');
$nav= $this->getServiceLocator()->get('Zend\View\Renderer\RendererInterface')
                                ->render($navlinks);

$viewLayout = new ViewModel(array('nav' => $nav));
return $viewLayout;

布局中

<div id="section-navigation">
   <?php echo $this->nav; ?> 
</div>

您还可以看到Partial Helper。像这样创建一个部分脚本:

<?php // partial.phtml ?>
<ul>
 <li><a href="<?php echo $this->link; ?>"><?php echo $this->name; ?></a></li>
</ul>

然后使用以下内容从布局脚本中调用它:

<div id="section-navigation">
 <?php echo $this->partial('partial.phtml', array(
    'link' => $this->link,
    'name' => $this->name)); ?> 
</div>

希望这可以帮到你。