Home  >  Article  >  Backend Development  >  Using Smarty in PHP Part 2: Using Configuration Files in Template Variables_PHP Tutorial

Using Smarty in PHP Part 2: Using Configuration Files in Template Variables_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 17:46:23864browse

The role of the configuration file in the template is to define variables for the front-end design page. It mainly controls the appearance of the template and has nothing to do with the PHP program.

​ ​ ​ Usage steps:

​ ​ 1. Use $tpl->configs_dir="directory" //Specify the directory where the configuration file is stored;

​ ​ 2. Use <{configs_load file="config file"}> in the template to load the f configuration file. If there is an area, you can use section="area" to specify the area

The purpose of setting the area is to call configuration file variables in different areas for different files.

In the configuration file, the region is specified through "[region name]". Other variables that do not specify the region are common variables, that is, every page can be used.

​ ​ 3. Create a configuration file in the specified directory.

The following is an example to demonstrate the idea: the main file index.php calls the template file index.tpl, and sets the configuration file variables in index.tpl (independent of the PHP program)

init.inc.php Smart template engine initialization file

define('ROOT_PATH', dirname(__FILE__)); //Website root directory

require ROOT_PATH.'/libs/Smarty.class.php'; //Introduce Smart template engine

$_tpl = new Smarty(); //Initialize an object

$_tpl->template_dir = ROOT_PATH.'/tpl/'; //Reset the template directory of the website

$_tpl->compile_dir = ROOT_PATH.'./com/'; //Reset the compiled file directory of the website

$_tpl->config_dir = ROOT_PATH.'/configs/'; //Reset the website’s configuration file directory

$_tpl->left_delimiter = '<{'; //Reset the left delimiter of the website

$_tpl->right_delimiter = '}>'; //Reset the right delimiter of the website

?>

index.php

require 'init.inc.php'; //Introduce template initialization file

global $_tpl;

$_tpl->display('index.tpl'); //Load template file

?>

There are two ways to use index.tpl configuration variables: 1. <{#Configuration Variables#}>; 2. <{$smart.config.Configuration Variables}>

<{config_load file="view.conf" section="one"}>

http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

http://www.w3.org/1999/xhtml">

         

& Lt; title & gt; The use of the configuration file in the template variable & lt;/title & gt;

        

                      

                

 

                

 

                

 

                 

 

             

 

             

 

                

 

                

 

                

 

                

 

             

 

             

 

                 

 

                

 

                

 

                

 

             

 

             

 

                

 

             

 

      

aaaa aaaa aaaa aaaa
aaaa aaaa aaaa aaaa
aaaa aaaa aaaa aaaa
 

                        区域变量的显示: 

                        <{#aa#}>
 

                        <{#bb#}>
 

                        <{#cc#}>
 

                

 

  

     

 

 

/configs/view.conf 配置文件

border=2

tabw=600

tabh=500

bgcolor=yellow

align=center

 

[one]

colspan=4

aa=one section

 

[two]

bb=two section

 

[three]

cc=three section

 

执行结果,如图所示:

摘自:Lee.的专栏

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478573.htmlTechArticleThe role of the configuration file in the template is to define variables for the front-end design page, which mainly controls the appearance of the template. Has nothing to do with PHP programs. Usage steps: 1. Use $tpl-configs_dir=directory...
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