Build a highly available WEB application using the InitPHP framework 03: Template View _ PHP Tutorial

Source: Internet
Author: User
Build highly available WEB applications using the InitPHP framework 03: use the template View. The template is basically used. 1. the template configuration continues with the code design in previous articles. This section describes how to use the InitPHP framework template. Let's take a look at the configuration.

Basic template usage
1. template configuration

Let's continue with the code design in previous articles. This section describes how to use the InitPHP framework template.

The configuration file is copied from the template configuration in initphp/initphp. conf. php to the conf/comm. conf. php file. The template configuration is as follows:

/********************************** View configuration **** *************************************//* ** template configuration ** 1. you can customize the template folder, compile the template path, template file suffix name, compile template suffix name * Whether to compile, template driver and template topic * 2. generally, the default configuration is the optimal configuration scheme, you can choose not to modify the template file parameter */$ InitPHP_conf ['template'] ['Template _ path'] = 'web/template '; // template path $ InitPHP_conf ['template'] ['Template _ c_path'] = 'data/template_c '; // template compilation path $ InitPHP_conf ['template'] ['Template _ type'] = 'htm '; // template file type $ InitPHP_conf ['template'] ['Template _ c_type '] = 'tpl. php '; // template compilation file type $ InitPHP_conf ['template'] ['Template _ tag_left'] ='
  '; // Right tag of the template $ InitPHP_conf ['template'] ['is _ compile'] = true; // every template compilation-you can disable this function after the system goes online $ InitPHP_conf ['template'] ['driver '] = 'simple '; // different template drivers compile $ InitPHP_conf ['template'] ['theme '] = ''; // template topic

The configuration file has detailed annotations. here, you need to pay attention to several configuration details:

Is_compile parameter. Generally, the template is written in the template_path folder, and a new compilation file is generated to the template_c_path folder. Enable this parameter in the debugging environment. disable this parameter after launch so that no new file is generated for each request.

Driver parameters. This parameter is a template driver. There are two default options: default, simple, and simple.

2. use a template

Create a templatefolder in the web/directory, and use it to save the template. the template suffix is .htm.

Create the data/template_c folder in the app/Directory. The data folder is mainly used to store the generated template file. this file requires 777 write permission.

Create a hello.htm template file under Template

Then, call the template in indexController.

  ;} Public function after () {echo after;}/*** entry */public function run () {$ this-> view-> display (hello ); // call the hello.htmtemplate. do not enter the suffix .htm.}/*** get Method */public function get () {echo this is get ;}}

After accessing http: // 127.0.0.1/test/www.initphp.com/in the browser, you can see that the template has been called and a new file hello. tpl. php is added to the template compilation folder.

3. View API usage

View APIs have only five APIs. Interface Document: http://www.initphp.com/6_4.htm

Template API usage:

// The template is set in sequence, but the second parameter F and L in the set_tpl function show the template first and last $ this-> view-> set_tpl ('test '); // set a template/test.htm template page $ this-> view-> set_tpl ('user/userinfo '); // Set template/user/userinfo.htm $ this-> view-> set_tpl ('header', 'F'); // Set it to the header, display $ this-> view-> set_tpl ('footer ', 'L') first; // Display $ this-> view-> remove_tpl ('test') at last '); // you can delete the preset test.htm template $ this-> view-> get_tpl (); // you can get the template array you have set $ this-> view-> display (); // The template is displayed.

4. default template tag

The default template tag is actually the most primitive PHP Usage method, which is simple, violent, and easy to use.

/* The following is how to use the default template label (which is the same as that in PHP): * // * modify the configuration file: */$ InitPHP_conf ['template'] ['driver '] = 'default';/* a simple template */
  // Output a variable
  
  
   

5. simple template tag
/* The following is how to use the simple template label: * // * modify the configuration file: */$ InitPHP_conf ['template'] ['driver '] = 'simple '; /* label usage: * // * common usage :*/
  /* If statement :*/
  
  
  
  /* Use the foreach statement :*/
  
  /* For statement usage :*/
  
  /* Output variable :*/
  /* Output constant :*/
  
   

Advanced template usage
1. custom tag: The custom template tag is placed in the: initphp/core/view/driver/folder, which has default. init. php and simple. init. php, which is the default and simple template label drivers respectively. File name: Tag driver name + '. init. php ', for example: simple. init. php class name: label driver name + 'init '. for example, simpleInit needs to define a public function of Init ($ str, $ left, $ right, $ str is the HTML code to be replaced, and $ left is the left tag of the template tag (default :' ') When you need to use a custom template tag, do not forget to modify: $ InitPHP_conf ['template'] ['driver'] in the configuration file.

For details, see the simple template label driver below:

If (! Defined ('is _ initphp') exit ('Access Denied! '); /*************************************** **************************************** * ** InitPHP 2.1 domestic PHP development framework View-simple template driver rule model ** ------------------------------------------------------------------------------- * copyright: copyRight By initphp.com * you can use the source code freely, but keep the author information during use. Respecting Others means respecting yourself. * ----------------------------------------------------------------------------------- * $ Author: zhuli * $ Dtime: 2011-10-09 *************************************** **************************************** * ***/class simpleInit {/*** template driver-simple driver * @ param string $ str template file data * @ return string */public function init ($ str, $ left, $ right) {// if Operation $ str = preg_replace (/. $ left. ifs + (. + ?). $ Right. /, $ str); $ str = preg_replace (/. $ left. else. $ right. /, $ str); $ str = preg_replace (/. $ left. elseifs + (. + ?). $ Right./, $ str); // for Operation $ str = preg_replace (/. $ left. fors + (. + ?). $ Right. /, $ str); $ str = preg_replace (/. $ left. /. $ right. //, $ str); // foreach operation $ str = preg_replace (/. $ left. foreachs + (. + ?). $ Right. /, $ str); $ str = preg_replace (/. $ left. /foreach. $ right. //, $ str); // output variable $ str = preg_replace (/. $ left. (\ $ [a-zA-Z _-rows] [a-zA-Z0-9 _-rows] *). $ right. //, $ str); // constant output $ str = preg_replace (/. $ left. ([A-Z _-rows] [A-Z0-9 _-rows] *). $ right. /s, $ str); // tag parsing $ str = preg_replace (/. $ left. /if. $ right. /, $ str); $ pattern = array ('/'. $ left. '/','/'. $ right. '/'); $ replacement = array (''); return preg_replace ($ pattern, $ replacement, $ str );}}

2. layout

Use Layout on the HTML page of the template: . Rule: left tag + 'layout: 'users' Template name (without adding .htm; user/version if there is a directory, use the same as set_tpl) '+ the right tag Layout is used to implement Template Layout on the HTML page. Will display the version.htm static page

/* Template layout, which is common in all template engines * // The Template layout is mainly used to cut a template into multiple small templates, you can directly use the * // * label in the Template: layout: template name (without a template name extension, for example, user.htm template, user is used directly. If the template has multiple folders, test/user represents the test/user.htm template )*/
   

3. template topic

Generally, a website has multiple template themes. InitPHP provides a simple template theme configuration method. you only need to configure the variable $ InitPHP_conf ['template'] ['theme, if it is a red topic, the 'red' template topic is placed under the default directory of the template. for example, if the 'red' topic is used, the template folder directory is: in the template/red/folder, a directory is added to the default file directory of the template, which facilitates the application of multiple themes.

4. compilation mechanism
The template mechanism of InitPHP will compile the HTML page. php file. the default value is (template_c/compiling folder and. tpl. php suffix name) $ InitPHP_conf ['template'] ['is _ compile '] whether to enable the compilation mechanism. we recommend that you enable this function during development. after the development is complete, disable this parameter.


Article 1. template configuration we continue the code design in previous articles. This section describes how to use the InitPHP framework template. First look at the configuration, the configuration file from in...

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.