phpzend-frameworkzend-viewzend-layout

Render a partial view with layout in Zend Framework 1.12


Similar questions have been asked before but unfortunately I am still not able to find a solution of my problem.

I want to render a view with its layout and store its response into a variable.

I have an action called pdf as shown below.

public function pdfAction(){
        $ids = $this->getParam('id');
        $entity = $this->getParam('entity');
        $referer = $this->getRequest()->getServer('HTTP_REFERER');
        if(empty($ids) || empty($entity)){
            $this->_helper->redirector->gotoUrlAndExit($referer);
        }
        $grid = $this->_exportModel->getExportGrid($ids, $entity);
        if(empty($grid->data[0])){
            $this->_helper->messenger->error('No user was found for the given id. Please pass the correct parameters and try again.');
            $this->_helper->redirector->gotoUrlAndExit($referer);
        }

        /* I would like to render the view here and get its response below. */
        $html = '';

        $pdf = new Om_PDF();
        $pdf->writeHtmlToPDF($html);
    } 

This is the pdf view for the above action.

<?php echo $this->render('templates/grid/print-grid.phtml'); ?>

And here's the pdf layout

<?php echo $this->doctype(); ?>
<html moznomarginboxes mozdisallowselectionprint>
<link rel="stylesheet" type="text/css" href="//maxcdn.bootstrapcdn.com/bootstrap/3.1.0/css/bootstrap.min.css" media="all"/>
<style type="text/css">

    .container {
        margin: 0 auto;
        max-width: 1000px;
        text-align: center;
        margin-top: 30px;
    }

    @media print {
        @page {
            size: auto;
            margin: 15mm 10mm 15mm 10mm;
        }

        body {
            margin: 0px;
        }

        .page-break {
            display: block;
            page-break-before: always;
        }

        .print-view-table {
            float: left;
            width: 45% !important;
            margin-right: 5%;
        }

        .print-view-table tr th, .print-view-table tr td {
            width: 30%;
        }

        .print-view-table tr th {
            background-color: #e0e0e0 !important;
            border: 1px solid #ddd;
            -webkit-print-color-adjust: exact;
        }

    }
</style>
<head>
    <?php ?>
</head>
<body>
<div class="container">
    <div class="row">
        <?php echo $this->layout()->content; ?>
    </div>
</div>
</body>
</html>

So far I have tried using render() and partial() methods but none of them worked.

Is there a way using which I can get the out put of the pdf view along with its layout inside the pdfAction() method?


Solution

  • You need to use :

    $this->_helper->layout->setLayout('/path/to/your/pdf_layout_script');
    

    in your pdfAction() and after that you can use render() or partial() methods, the corrects layout should be out put