【问题标题】:What is the proper way to do conditional formatting in a ZF2 MVC project?在 ZF2 MVC 项目中进行条件格式化的正确方法是什么?
【发布时间】:2014-04-24 12:20:49
【问题描述】:

在我正在开发的 ZF2 项目中,我想围绕 layout.phtml 中的 echo $this->content; 语句创建一个外壳,以允许对主要内容区域进行条件格式设置。具体来说,我想将内容放入 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 似乎很奇怪。

有什么建议或建议吗?

【问题讨论】:

    标签: layout if-statement model-view-controller zend-framework2 alternateview


    【解决方案1】:

    创建两个布局并在 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 ");
                    }
            }
    

    【讨论】:

      【解决方案2】:

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

      控制器

      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>
      

      【讨论】:

        【解决方案3】:

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

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

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

        【讨论】:

          猜你喜欢
          • 2011-09-17
          • 1970-01-01
          • 2013-10-26
          • 1970-01-01
          • 2020-06-07
          • 1970-01-01
          • 2013-06-27
          • 2010-11-21
          • 2013-10-03
          相关资源
          最近更新 更多