Zend Framework 2从父视图继承变量

时间:2013-05-09 22:51:12

标签: php zend-framework2 zend-view zend-framework-mvc

我有以下代码返回带子视图的视图。 正如它所看到的,子视图重复变量$ form和数组&字段重复传递变量进入视图。

如何从responseView继承userProfileInformation查看这些变量并将它们只注入responseView?

    $responseView = new ViewModel();
    $responseView->setVariables(array(
        'form' => $form,  
        'fields' => $fields,  
        'userType' => $userType,                
    ));
    $responseView->setTemplate('templates/logged_user_profile');
    $responseView->setTerminal(true);
    $userProfileInformationView = new ViewModel();
    $userProfileInformationView->setTemplate('templates/logged_user_profile_information');
    $userProfileInformationView->setVariables(array(
        'form' => $form,  //I don't want this
        'fields' => $fields,  //I don't want this
    ));
    $responseView->addChild($userProfileInformationView, 'userProfileInformation');     
    return $responseView;

1 个答案:

答案 0 :(得分:2)

我只知道一种编码方式:

    <?php
    $responseView = new ViewModel();
    $responseView->setVariables(array(
        'form' => $form,  
        'fields' => $fields,  
        'userType' => $userType,                
    ));
    $responseView->setTemplate('templates/logged_user_profile');
    $responseView->setTerminal(true);
    $userProfileInformationView = new ViewModel();
    $userProfileInformationView->setTemplate('templates/logged_user_profile_information');

    $userProfileInformationView->setVariables($responseView->getVariables());

    $responseView->addChild($userProfileInformationView, 'userProfileInformation');     
    return $responseView;
    ?>