如何重构代码以避免将部分ViewHelper的输出分配给另一个部分ViewHelper的变量?

时间:2016-04-14 15:34:59

标签: php zend-framework2 refactoring

我有一些代码,我正在努力分离"数据"从"发送数据到Zend partial查看助手"。例如,即我想创建"只是数据",只返回从我的数据函数到父调用者的数据

//contains no $this->partial(), deals only with accumulating data
//that may or may not be used for partial later on
$data = $this->getDataOnly(); 

//deals only with sending data to partial for rendering purposes
$this->generatePartials($data);

在下面的代码中,我似乎将两者混合在一起。具体来说,我有一个partial 作为我数据的一部分,这就是我想要避免的。

// $modalData is an array accumulating data for Zend Partial View Helper
// it already has some data, i.e. id, boolean vars 
foreach($modalData as $i => $value)
{
    // here we are adding 'modalHtml' view helper variable 
    // which will contain HTML gotten from (another) partial
    // Problem:  I am not comfortable using a 'partial' as part of a parent 'partial'
    // (which is further down in the code)

    // what could've been "clean data only" generation is about to be
    // polluted by stuffing a partial into it, mixing data and view
    $modalData[$i]['modalHtml'] = $this->partial(
        "partial/product.phtml", 
        parent::getModalContentData($i)
    );
}

//here we are appending HTML produced from partialLoop, using $modalData
$modalBoxesHtml .= $this->partialLoop("partial/modal_box.phtml", $modalData);

问题:

我可以避免我所在的部分"将部分(即HTML代码而非数据)直接填充到另一个视图帮助器部分的变量中吗? 我可以使用某种更智能的构造,类似于addChild的{​​{1}}吗?

我想摆脱分配" HTML块"查看辅助变量并仅分配实际数据。可以这样做吗?

1 个答案:

答案 0 :(得分:0)

我可能能够做我想要的事情

我已将partial方法调用移至partial/*.phtml文件本身

<div>
    <?php
    echo $this->partial("partial/product.phtml", $this->modalData);
    ?>            
</div>

我现在已经嵌套了部分内容。

我的数据也嵌套了

foreach($modalData as $i => $value)
    $modalData[$i]['modalHtml'] = parent::getModalContentData($i);