How can I output child view with dynamically generated captureTo for addChild() in controller

34 views Asked by At

Giving the following manual: enter link description here

It is quite easy to have a child view and echo it when I know captureTo used in the controller (e.g. "<?php echo $this->article ?>"), but can I do similar when I generate view models dynamically, and assign them to dynamically generated captureTo for addChild() function:

        foreach ($studentEvaluations as $studEval) {

            $studEvalId = $studEval->getEvalId();
            $formViewModel = $this->buildStudentEvaluationViewModel($studEval);
            $viewModel->addChild($formViewModel,  $studEvalId);
        }

I tried the following, but it does not work:

    <?php foreach ($this->viewModel()->getCurrent()->getIterator() as $studId => $studEval) : ?>
        <tr>
            <td><?php echo $this->escapeHtml($studEval->fname); ?></td>
            <td><?php echo $this->escapeHtml($studEval->lname); ?></td>
            <td><?php echo $this->escapeHtml($studEval->formName); ?></td>
            <td><?php echo $this->escapeHtml($studEval->supdated); ?></td>
            <td><?php echo $this->escapeHtml($studEval->screated); ?></td>
            <td>

                <button class="btn btn-primary btn-lg"
                        data-toggle="modal"
                        data-target="#myModal<?php echo $studId; ?>"
                        data-loading-text="Loading..."> Edit
                </button>

                <!-- Modal -->
                <div class="modal fade" id="myModal<?php echo $studId; ?>" tabindex="-1" role="dialog"
                     aria-labelledby="myModalLabel" aria-hidden="true">
                    <div class="modal-dialog">
                        <div class="modal-content">
                            <div class="modal-header">
                                <button type="button" class="close" data-dismiss="modal"
                                        aria-hidden="true">&times;</button>
                                <h4 class="modal-title" id="myModalLabel">Modal title</h4>
                            </div>
                            <div class="modal-body">
                                <p> 

<!-- HERE ->>>>>>>>> -->               <?php echo $studEval ?> 

                                </p>
                            </div>
                            <div class="modal-footer">
                                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                                <button type="button" class="btn btn-primary">Save changes</button>
                            </div>
                        </div>
                    </div>
                </div>
                                </td>


        </tr>
    <?php endforeach; ?>

EDIT 1

This is an error when I try to output the view in the this way:

Catchable fatal error: Object of class Zend\View\Model\ViewModel could not be converted to string in C:\dev\projects\OnlineFieldEvaluation\module\OnlineFieldEvaluation\view\online-field-evaluation\online-field-evaluation\test3.phtml on line 62
1

There are 1 answers

0
vlr On

I found how to do it. It was just my luck of knowledge in PHP:

   <?php foreach ($this->viewModel()->getCurrent()->getChildren() as $studEval) : ?>
        <tr>
            <td><?php echo $this->escapeHtml($studEval->fname); ?></td>
            <td><?php echo $this->escapeHtml($studEval->lname); ?></td>
            <td><?php echo $this->escapeHtml($studEval->formName); ?></td>
            <td><?php echo $this->escapeHtml($studEval->supdated); ?></td>
            <td><?php echo $this->escapeHtml($studEval->screated); ?></td>
            <td>

                <button class="btn btn-primary btn-lg"
                        data-toggle="modal"
                        data-target="#myModal<?php echo $studEval->captureTo(); ?>"
                        data-loading-text="Loading...">

                    Edit <?php echo $studEval->captureTo(); ?>

                </button>

                <!-- Modal -->
                <div class="modal hide fade" id="myModal<?php echo $studEval->captureTo(); ?>" tabindex="-1"
                     role="dialog"
                     aria-labelledby="myModalLabel" aria-hidden="true">
                    <div class="modal-dialog">
                        <div class="modal-content">
                            <div class="modal-header">
                                <button type="button" class="close" data-dismiss="modal"
                                        aria-hidden="true">&times;</button>
                                <h4 class="modal-title" id="myModalLabel">Modal title</h4>
                            </div>
                            <div class="modal-body">
                                <p>
                                    <?php

                                    echo $this->{$studEval->captureTo()};

                                    ?>
                                </p>
                            </div>
                            <div class="modal-footer">
                                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                                <button type="button" class="btn btn-primary">Save changes</button>
                            </div>
                        </div>
                    </div>
                </div>

            </td>


        </tr>
    <?php endforeach; ?>