initphp framework to build a highly available Web application 03: Template view Using _php Tutorial

Source: Internet
Author: User

Template Basic Use

1. Template Configuration

We continue with the code design of the previous articles. This section focuses on how to use the template for the initphp framework.

First look at the configuration, the configuration file from the template configuration in initphp/initphp.conf.php to the conf/comm.conf.php file. The configuration of the template looks like this:

/*********************************view Configuration *****************************************//** * Template configuration * 1. You can customize the template's folder, compile template path, template file suffix name, compile template suffix name * whether to compile, template drive and template Theme * 2. In general, the default configuration is the optimal configuration scheme, you can not choose to modify the template file parameters */$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[' templates ' [' template_c_type ']    = ' tpl.php ';//template compilation file type $InitPHP _conf[' template ' [' template_tag_left ']  = '
 
    ;//Template Right tag $initphp_conf[' template ' [' is_compile ']         = true;//template per compilation-the system can turn off this feature after it is online $initphp_conf[' template ' [' driver ']             = ' simple ';//different template-driven compilation $initphp_conf[' Template ' [' theme ']              = ';//Templates Theme

Detailed comments are already in the configuration file, so here are a few configuration details to consider:

The Is_compile parameter. In general, the template will be written under the Template_path folder, and a new compiled file will be generated under the Template_c_path folder. Open this parameter in debug environment, close this parameter after going online so that no new files are generated for each request.

The driver parameter. This parameter is a template driver, the default is two, the first is the default second is simple, the default is simple

2. Using templates

Create a template folder under the web/directory, which is used primarily to store templates, template suffix names. htm

In the app/directory to create the Data/template_c folder, the Data folder is mainly used to store the generated template files, this file needs to have writable 777 permissions.

Create a hello.htm template file under Templates

Then invoke the template in the Indexcontroller

 
   ;} Public function after () {echo after;} /** * Entry */public function run () {$this->view->display (hello);//call hello.htm template, suffix. htm does not need to fill in the}/** * Get method */public Fu Nction 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 there is a new file under the template compilation folder hello.tpl.php

3. View API Usage

The View API has a total of 5 API interfaces. Interface Documentation: http://www.initphp.com/6_4.htm

The template API uses:

The template is set in order, but the second parameter F and L in the SET_TPL function let the template first and last show $this->view->set_tpl (' Test '); Set up a template/test.htm template page $this->view->set_tpl (' User/userinfo '); Set Template/user/userinfo.htm$this->view->set_tpl (' Header ', ' F '); Set to head, first display $this->view->set_tpl (' footer ', ' L '); Finally show $THIS->VIEW->REMOVE_TPL (' Test '); You can remove the test.htm template $this->view->get_tpl () that has been set above; You can get an array of templates that have been set $this->view->display (); Template display

4. Template label for default

The default template tag is actually the most primitive way to use PHP, simple brute force.

/* The following is the default template label usage (as in PHP): *//* Profile Modification: */$InitPHP _conf[' template ' [' driver ']             = ' default ';/* a simple template */
 
    //This outputs a variable
 
   
 
   
 
   

5. Simple Template Label

/* The following is a simple template label usage: *//* configuration file Modification: */$InitPHP _conf[' template ' [' driver ']             = ' basic ',/* label usage: *//* normal use: */ /* If statement using:///////
 
   
 
   
 
   
 
   
 
    foreach statement using: *
 
   
 / 
   /* For statement use: *
 
   
 
    / /* OUTPUT Variable: */
 
    * Output constant: */
 
   
 
   

Template Advanced Usage

1. Custom Labels

The custom template label is placed under the: initphp/core/view/driver/folder, which already has default.init.php and simple.init.php, respectively, for default and simple two template tag drivers. The name of the file is: Tag driver name + '. init.php ', for example: simple.init.php class Name: Tag driver name + ' init ', for example: Simpleinit need to define a public function for init ($STR, $left, $right) , $STR is the HTML code that needs to be replaced, $left is the left tag of the template label (default: ' ') When you need to use a custom template tag, don't forget to modify the configuration file: $InitPHP _conf[' template ' [' Driver ']

See the Simple template label driver below:

if (!defined (' is_initphp ')) exit (' Access denied! ');  * initphp 2.1 Domestic PHP development Framework View-simple Simple template Driven Rule model *-------------------------------------------------------------------------------* All rights reserved: CopyRight by initphp.com * You are free to use the source code, but in the course of use, please keep the author information. Respect for the fruits of others ' work is respect for 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) { The If Action $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./for $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_-ÿ][a-za-z0-9_-ÿ]*). $right./,, $str);//constant Output $STR = Preg_replace (/. $left. [A-z_-ÿ] [a-z0-9_-ÿ]*). $right./s,, $str);//label Resolution $STR = Preg_replace (/. $left./if $right./,, $STR); $pattern = Array ('/'. $left. ' /', '/'. $right. ' /'); $replacement = Array ("); return Preg_replace ($pattern, $replacement, $STR); }}

2. Layout layouts

Use layout in the template HTML page: 。 Rules: Left label + ' layout: ' + ' template name (No. htm required, directory is user/version, and SET_TPL () as used) ' + right label layout is to solve the user in the HTML page to implement the template layout, HTML used in the , the version.htm static page is displayed

/* Template layout, the method is common in all template engines *//* template layout is mainly to solve the template is cut into multiple small templates, the template can be directly invoked using the *//* tag: layout: Template name (no need to have a template name suffix, such as the user.htm template, the direct user. If the template has more than one folder, Test/user represents the test/user.htm template) */
 
   

3. Template Themes

In general, a site will have more than one template theme, initphp provides a simple template theme configuration method users only need to configure $initphp_conf[' template ' [' theme '] This variable, if the red theme, then the value is ' red ' Template themes are placed under the default directory of the template, such as the ' red ' theme, the template folder directory is: template/red/folder is actually just in the template default file directory and then add a layer of directory, so that can be convenient for multi-theme applications

4. Compilation mechanism


The initphp template mechanism compiles HTML pages into. php files, by default (template_c/compilation folder and. tpl.php suffix name) $InitPHP _conf[' template ' [' Is_compile '] Whether to open the compilation mechanism, recommend the development of the time to open, development completed on-line, close the parameter


http://www.bkjia.com/PHPjc/635032.html www.bkjia.com true http://www.bkjia.com/PHPjc/635032.html techarticle template Basic use 1. Template configuration We continue the code design for several previous articles. This section focuses on how to use the template for the initphp framework. First look at the configuration, 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.