Home >Backend Development >PHP Tutorial >The use of ob class functions when php introduces templates

The use of ob class functions when php introduces templates

WBOY
WBOYOriginal
2016-08-18 09:16:351309browse

Recently, I am learning about some principles of PHP operation. Generally, PHP calls template files through the controller to display the page. At the same time, the values ​​of some variables in the controller are also assigned to the template files. I use the template.php file as the template file. Therefore, you can directly use PHP syntax in the template file to display the variables assigned in the controller. By looking at the code of the CI framework, he will first enable ob_start, then introduce the template file template.php, and finally return ob_get_clean(), as shown below

<code>ob_start();
include($_ci_path);
$buffer = ob_get_contents();
@ob_end_clean();
return $buffer;</code>

At the same time, I see that the code principles of the company's internal framework are basically the same. Now I want to know what is the use of using a series of functions such as ob. It feels like a simple include file can be assigned values.

Reply content:

Recently, I am learning about some principles of PHP operation. Generally, PHP calls template files through the controller to display the page. At the same time, the values ​​of some variables in the controller are also assigned to the template files. I use the template.php file as the template file. Therefore, you can directly use PHP syntax in the template file to display the variables assigned in the controller. By looking at the code of the CI framework, he will first enable ob_start, then introduce the template file template.php, and finally return ob_get_clean(), as shown below

<code>ob_start();
include($_ci_path);
$buffer = ob_get_contents();
@ob_end_clean();
return $buffer;</code>

At the same time, I see that the code principles of the company's internal framework are basically the same. Now I want to know what is the use of using a series of functions such as ob. It feels like a simple include file can be assigned values.

<code>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输出
}</code>

The Ouba series of functions are still very useful. You can see that after the controller calls render to render the view, it gets HTML. You can write these HTML into static files and cache them, or replace some things in the HTML and output them. It is very Convenient.
echo render('index.php', $data);

The buffer is quite useful. For example, if you want to save client information; sending file header information will not affect header or setcookie, etc.

I can only understand that you don’t understand what include is for

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn