如何将数据传递给视图并将视图加载到变量中

时间:2015-12-28 11:07:35

标签: codeigniter

我想将$data变量传递给print视图,然后我希望将print视图的结果传递给变量($myvariable),我不会这样做。我想向用户展示任何内容:

$myvariable =  $this->load->view('print' , $data,true);

$myvariable为空,print视图将加载!

更新

    function makePDF(){

        $pdfFilePath = FCPATH."assests/gifts/test.pdf";
        $data['page_title'] = 'Hello world'; // pass data to the view

        if (file_exists($pdfFilePath) == FALSE)
        {
            ini_set('memory_limit','32M'); // boost the memory limit if it's low <img src="https://s.w.org/images/core/emoji/72x72/1f609.png" alt="" draggable="false" class="emoji">
            $html = $this->printer(212486); // render the view into HTML
            var_dump($html);
            return;
            $this->load->library('pdf');
            $pdf = $this->pdf->load();
            $pdf->SetFooter($_SERVER['HTTP_HOST'].'|{PAGENO}|'.date(DATE_RFC822)); // Add a footer for good measure <img src="https://s.w.org/images/core/emoji/72x72/1f609.png" alt="" draggable="false" class="emoji">
            $pdf->WriteHTML($html); // write the HTML into the PDF
            $pdf->Output($pdfFilePath, 'F'); // save to file because we can
        }


    }



    function printer( $id = 0  ){

....


        $data =  array ('coupons'=>$coupons  , 'shop'=>$shop , 'details'=>$parent );
        return $this->view('print' , $data,true);



    }

1 个答案:

答案 0 :(得分:1)

不要使用return来恢复视图,而是调整函数以将视图保存到变量中,然后返回该变量。另外,当你说$myvariable为NULL时,我假设你指的是UPDATED部分中的$html变量?

首先,这些行将显示您的视图,因为您正在转储已编写的html然后退出该函数:

var_dump($html);
return;

所以为了清理,我建议改变你的功能:

function makePDF(){
    $pdfFilePath = FCPATH."assests/gifts/test.pdf";
    $data['page_title'] = 'Hello world'; // pass data to the view

    if (file_exists($pdfFilePath) == FALSE)
    {
        ini_set('memory_limit','32M'); // boost the memory limit if it's low
        $html = $this->printer(212486); // render the view into HTML

        $this->load->library('pdf');
        $pdf = $this->pdf->load();
        $pdf->SetFooter($_SERVER['HTTP_HOST'].'|{PAGENO}|'.date(DATE_RFC822)); // Add a footer for good measure
        $pdf->WriteHTML($html); // write the HTML into the PDF
        $pdf->Output($pdfFilePath, 'F'); // save to file because we can
    }
}

和...

function printer( $id = 0  ){

    ....

    $data =  array ('coupons'=>$coupons  , 'shop'=>$shop , 'details'=>$parent );
    $return_html = $this->load->view('print' , $data, true);

    return $return_html;

}

这里最大的区别是确保从$this->view('print', $data, true);$this->LOAD->view('print', $data, true);正确编写视图语法

此外,为了更好地衡量,视图返回被保存到变量中并作为该对象返回。我会逐步执行这些更改,因为可能没有必要采取第二步。