This view file is very simple. Ob_start () and ob_get_content () are mainly used. This file is located in the des folder.
View. php
[Php]
<? Php
Class view {
// View type default/wap
Public static $ view_type = null;
Public function _ construct (){
Ob_start ();
}
Public function finish (){
$ Content = ob_get_contents ();
Return $ content;
}
Public static function set_view_type (){
Switch (true ){
Case stripos ($ _ SERVER ['HTTP _ USER_AGENT '], 'windows CE ')! = FALSE: self ::$ view_type = 'wap '; break;
Default: self: $ view_type = 'default ';
}
}
Public static function show ($ location, $ param = array ()){
If (is_null (self: $ view_type )){
Self: set_view_type ();
}
$ View = SIMPLE_PATH. '/view/'. self: $ view_type. '/'. $ location;
Extract ($ param, EXTR_OVERWRITE );
Ob_start ();
File_exists ($ view )? Require $ view: exit ($ view. 'nonexistent ');
$ Content = ob_get_contents ();
Return $ content;
}
}
For OB functions, we can simply think that after PHP compilation, it will not immediately return to the page, but will first put it in the buffer zone.
The preceding view is just a simple implementation. If we want to extend it, we can improve the set_view_type () method, increase the cache, and increase the template support.
For specific implementation, I will add it in a later chapter. Today we will try to use this VIEW.
Or the index. php file in the controller folder yesterday
[Php]
<? Php
Class index {
Public function demo (){
View: show('index.htm', array ('message' => 'Hello World '));
}
}
Then create a new ultultfolder in the viewfolder, and then create an index.htm
[Html]
<Html>
<Head>
<Title> </title>
</Head>
<Body>
<? Php
Echo $ message;
?>
</Body>
</Html>
When we run the site, we can see the result "hello world ".
I think this result is a witness of every programmer's new thoughts. Www.2cto.com
If you want to insert the header or tail part in this file, you only need to create a new head.htm. Then add it to index.htm.
[Html]
<Html>
<Head>
<Title> </title>
</Head>
<Body>
<? Php
Echo $ message;
View: show('head.htm ');
?>
</Body>
</Html>
The public part can be added.
So far, we have implemented this small View class.
You can experiment on your own. If not, let's talk about it again.
Author: tomyjohn