簡體   English   中英

在ZF2 MVC項目中進行條件格式化的正確方法是什么?

[英]What is the proper way to do conditional formatting in a ZF2 MVC project?

在我正在開發的ZF2項目中,我想在echo $this->content;周圍創建一個shell 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似乎很奇怪。

有什么意見或建議嗎?

創建兩個布局,然后在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 ");
                }
        }

有很多方法可以完成此任務。 這是一種方法,邏輯存在於控制器中:

控制者

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;        
}

two-column-wrapper.phtml

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

single-column-wrapper.phtml

<div class='col-md-12'>
    <?php echo $innerConntent; ?>
</div>

無需設置其他模板,而是可以通過使必要的類依賴於布局變量來調整Bootstrap Twitter類。 您可以在控制器動作中使用邏輯將變量直接傳遞到布局(而不是視圖),如下所示:

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

然后只需像將變量發送到視圖一樣在布局中訪問這些變量即可。 您甚至不必在它們前面加上“布局”,它們不會發生沖突。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM