Use of ob functions when php introduces a template

Source: Internet
Author: User
Recently I have been familiar with some principles of php running. generally, php calls the template file through the controller to display the page. at the same time, some variable values in the controller will also be assigned to the template file, I use template. php files are used as template files, so php's... recently I have been familiar with some principles of php running. generally, php calls the template file through the controller to display the page. at the same time, some variable values in the controller will also be assigned to the template file, I use template. php files are used as template files. Therefore, you can directly use the php syntax in the template file to display the assigned variables in the controller. By viewing the CI framework code, the system first enables ob_start, introduces the template file template. php, and returns ob_get_clean (), as shown below.

ob_start();include($_ci_path);$buffer = ob_get_contents();@ob_end_clean();return $buffer;

At the same time, I think the code principles of the company's internal framework are basically the same. now I want to know how to use a series of functions such as ob, it seems that only the include file can be assigned a value.

Reply content:

Recently I have been familiar with some principles of php running. generally, php calls the template file through the controller to display the page. at the same time, some variable values in the controller will also be assigned to the template file, I use template. php files are used as template files. Therefore, you can directly use the php syntax in the template file to display the assigned variables in the controller. By viewing the CI framework code, the system first enables ob_start, introduces the template file template. php, and returns ob_get_clean (), as shown below.

ob_start();include($_ci_path);$buffer = ob_get_contents();@ob_end_clean();return $buffer;

At the same time, I think the code principles of the company's internal framework are basically the same. now I want to know how to use a series of functions such as ob, it seems that only the include file can be assigned a value.

Function render ($ template, array $ var = array () {extract ($ var); // extract the variables in the array (here is your concern) ob_end_clean (); // disable the top-level output buffer content ob_start (); // start a new buffer require "$ template"; // load view $ view = ob_get_contents (); // Obtain the buffer content ob_end_clean (); // Close the buffer ob_start (); // start the new buffer and return $ view to the subsequent programs; // return the text, you can also directly output echo here}

The functions of the Ouba series are still very useful. you can see that after the controller calls the render rendering view, the HTML is obtained. you can write the HTML into a static file and cache it, or, it is very convenient to replace some things in HTML and output them.
echo render('index.php', $data);

The effect of the buffer zone is still quite large. For example, if you want to save the client information, or use the wrong sending file header informationheaderOrsetcookieImpact

I can only understand that you don't understand what include is.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.