Use of OB class functions when introducing templates to PHP

Source: Internet
Author: User
Recently in the understanding of some of the principles of PHP operation, the general PHP is called through the Controller template file to display the page, while the controller will be some variable values will be assigned to the template file, I use the template.php file as a template file, so you can use PHP's syntax directly in the template file to display the variables assigned to the controller. By looking at the code of the CI framework he will first open Ob_start, then introduce the template file template.php, and finally return 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 look at the company's internal framework code principle is basically the same, I now want to know, using OB and other functions have what use, feel just simple include file can be assigned to the past.

Reply content:

Recently in the understanding of some of the principles of PHP operation, the general PHP is called through the Controller template file to display the page, while the controller will be some variable values will be assigned to the template file, I use the template.php file as a template file, so you can use PHP's syntax directly in the template file to display the variables assigned to the controller. By looking at the code of the CI framework he will first open Ob_start, then introduce the template file template.php, and finally return 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 look at the company's internal framework code principle is basically the same, I now want to know, using OB and other functions have what use, feel just simple include file can be assigned to the past.

function render($template, array $var = array()) {    extract($var);                // 提取数组中的变量(你关注的问题就在这里)    ob_end_clean();               // 关闭顶层的输出缓冲区内容    ob_start();                   // 开始一个新的缓冲区    require "$template";          // 加载视图view    $view = ob_get_contents();    // 获得缓冲区的内容    ob_end_clean();               // 关闭缓冲区    ob_start();                   // 开始新的缓冲区,给后面的程序用    return $view;                 // 返回文本,此处也可以直接echo输出}

Aubagne series functions are also useful, you see the controller call render render view, you get the HTML, you can write these HTML into a static file cache, or replace some things in the HTML output, very convenient.
echo render('index.php', $data);

The buffers are still pretty big. For example, you want to save the client's information, using the use of sending the file header information is incorrect header or setcookie affect, etc.

I can only understand that you don't understand what the 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.