1

is it possible to add multiple views in a array to a parent view in Zend Framework 2? For example:

childView.php

echo $this->data;

parentView.php:

foreach($this->views as $view)
    echo '<div>'.$view.'</div>';

controller.php:

public function actionIndex(){
    $children = array(1,2,3);
    foreach($children as $child){
        $childView = new ViewModel(array('data' => $child));
        $childView->setTemplate('childView');
        $childrenViews[] = $childView;
    }
    $view = new ViewModel();
    $view->setTemplate('parentView');

    // some function that adds the childrenViews to the parentView;

    return $view;
}

Expected output: <div>1</div><div>2</div><div>3</div>

ps: It's dummy code so please ignore possible syntax errors.

1

3 Answers 3

3

You could do the following:

public function actionIndex(){
    $children = array(1,2,3);
    foreach($children as $child){
        $childView = new ViewModel(array('data' => $child));
        $childView->setTemplate('childView');
        $view->addChild($childView, 'the-child-views', true);
    }
    $view = new ViewModel();
    $view->setTemplate('parentView');

    return $view;
}

This line:

$view->addChild($childView, 'the-child-views', true);

Will append the child views to this one var. So you can echo your views using:

<?php echo $this->the-child-views ?>

Hope it's what you are looking for.

Sign up to request clarification or add additional context in comments.

Comments

3

You can just assign the array of ViewModels to your parent View Model.

<?php
public function actionIndex()
{
    $children = array(1,2,3);
    foreach($children as $child){
        $childView = new ViewModel(array('data' => $child));
        $childView->setTemplate('childView');
        $childrenViews[] = $childView;
    }
    $view = new ViewModel();
    $view->setVariable('children', $children); // assign to $children
    /**
     * Or set in constructor
     */
    //$view = new ViewModel(array(
    //    'children' => $children
    //));

    $view->setTemplate('parentView');

    return $view;
}

Now you can just iterate over them in your parent template:

parentView.phtml

<?php // $children or $this->children to access your array of ViewModels ?>
<?php foreach($children as $child): ?>
    <?php echo $child // automatically rendered for you ?>
<?php endforeach ?>

2 Comments

That doesn't work for me because addChild expects an interface as the first parameter. And an array does not statisfy that.
Your answer seems better than the accepted because of your example, which is easier to understand and gives the right context
-1

I think it should work with the partial helper: http://framework.zend.com/manual/2.1/en/modules/zend.view.helpers.html#partial-helper

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.