0

在我正在开发的 ZF2 项目中,我想围绕echo $this->content;layout.phtml 中的语句创建一个外壳,以允许对主要内容区域进行条件格式设置。具体来说,我想将内容放入 75% 宽的列中,并在大多数页面的 25% 宽的列中包含一些“装饰性”元素。但是,我想将需要更多空间的页面更改为单列。我的项目是一个 CMS,其中每个页面都有一个属性,可以告诉视图或控制器页面是正常的还是宽的。我已经考虑了一些方法来实现我所追求的。

我的“布局视图中的条件格式”可能如下所示:

// module/Application/view/layout/layout.phtml:
//...

  if ($templateNormal) {
     echo "<div class='col-md-9'>";
  } else {
     echo "<div class='col-md-12'>";
  }

   echo $this->content;

  if ($templateNormal) {
     echo "</div>";
     echo "<div class='col-md-3'>";
       //... ornamentation
     echo "</div>";
  } else {
     echo "</div>";
  }

//...

虽然上述方法可以工作,但对于纯 MVC,我认为布局视图中不应该进行任何决策。

我的“部分视图中的条件格式”可能如下所示:

// module/Application/view/layout/layout.phtml:
//...

  echo $this->partial('partial/open-shell.phtml');

   echo $this->content;

  echo $this->partial('partial/close-shell.phtml');

//...


// module/Application/view/partial/open-shell.phtml:
  if ($templateNormal) {
     echo "<div class='col-md-9'>";
  } else {
     echo "<div class='col-md-12'>";
  }


// module/Application/view/partial/close-shell.phtml:
  if ($templateNormal) {
     echo "</div>";
     echo "<div class='col-md-3'>";
       //... ornamentation
     echo "</div>";
  } else {
     echo "</div>";
  }

这里的决策是从布局视图中取出的,但它只是简单地放入其他视图中,所以它仍然在视图包中,仍然不是纯 MVC。

在我的“控制器中的条件格式”解决方案中,在控制器函数中开发了一对 html 脚本字符串,然后传递给视图。它可能看起来像这样:

// module/Application/view/layout/layout.phtml:
//...

  echo $this->open-shell-script';

   echo $this->content;

  echo $this->close-shell-script';

//...


// some controller function:
  //...
  if ($templateNormal) {
     $open-shell-script = "<div class='col-md-9'>";
     $close-shell-script = "</div>";
     $close-shell-script = "<div class='col-md-3'>";
     $close-shell-script .= //... ornamentation
     $close-shell-script .= "</div>";
  } else {
     $open-shell-script = "<div class='col-md-12'>";
     $close-shell-script = "</div>";
  }
  //...

在这种方法中,决策是在我认为应该在的控制器中完成的,但是在那里编写 html 似乎很奇怪。

有什么意见或建议吗?

4

3 回答 3

0

您可以通过使必要的类依赖于布局变量来调整 Bootstrap Twitter 类,而不是设置不同的模板。您可以使用控制器操作中的逻辑将变量直接传递给布局(而不是视图),如下所示:

$this->layout()->setVariables
        (
            array
            (
                'layoutVar1'     => 75,
                'someColClass'   => ($someSwitch ? 'col-md-9':'col-md-12' ),
                'layoutVar1'     => 75,
            )
         );

然后只需访问布局中的这些变量,就像将变量发送到视图一样。您甚至不必在它们前面加上“布局”,它们不会发生冲突。

于 2014-03-18T19:34:50.960 回答
0

有很多方法可以做到这一点。这是一种方法,逻辑存在于控制器中:

控制器

public function yourSampleAction()
{
    // assign variables as usual to this view model
    $viewModel = new ViewModel(array(
       //vars
    );

    // this will be the "wrapper" and can be single, double column or anything else.
    $wrapperViewModel = new ViewModel();
    $wrapperViewModel->addChild($viewModel, 'innerContent');

    // use this line when you want one column
    $wrapperViewModel->setTemplate('path/to/your/single-column-wrapper.phtml');

    // when this line you want two columns
    $wrapperViewModel->setTemplate('path/to/your/two-column-wrapper.phtml');

    return $wrapperViewModel;        
}

两列包装器.phtml

<div class='col-md-9'>
    <?php echo $innerConntent; ?>
</div>
<div class='col-md-3'>
    <!--- something else in here? -->
</div>

单列包装器.phtml

<div class='col-md-12'>
    <?php echo $innerConntent; ?>
</div>
于 2014-03-18T19:18:11.503 回答
0

创建两个布局并在 Module.php 的 init() 方法中决定应该使用哪个布局。

    public function init(ModuleManager $moduleManager) {
        $sharedEvents = $moduleManager->getEventManager()->getSharedManager();
        $sharedEvents->attach(__NAMESPACE__, 'dispatch', function($e) {
// This event will only be fired when an ActionController under the MyModule namespace is dispatched.
            $controller = $e->getTarget(); 
                        $controller->layout(" your chose layout ");
                }
        }
于 2014-03-18T19:01:20.987 回答