PHP framework 3 (view)

Source: Internet
Author: User

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

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.