Example of how to use the configuration file of the PHP template engine Smarty in template variables, template smarty
This document describes how to use the configuration file of the PHP template engine Smarty in template variables. We will share this with you for your reference. The details are as follows:
The 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 and has nothing to do with the PHP program.
Procedure:
1. Use $ tpl-> configs_dir = "directory" // specify the directory where the configuration file is stored;
2. Use <{configs_load file = "configuration file"}> in the template to load the f configuration file. If there is a region, use section = "region" to specify the region.
The purpose of setting a region is to call the configuration file variables of different regions for different files.
In the configuration file, "[region name]" is used to specify the region. Other variables without the specified region are common variables, that is, each page can be used.
3. Create a configuration file under the specified directory.
The following example demonstrates how to use an example: index. php In the main file calls index. tpl in the template file and sets the configuration file variable in index. tpl (unrelated to the PHP program)
Init. inc. php Smart template engine Initialization File
<? Php define ('root _ path', dirname (_ FILE _); // The website ROOT directory require ROOT_PATH. '/libs/Smarty. class. php'; // introduce the 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 website's compilation file directory $ _ tpl-> config_dir = ROOT_PATH. '/configs/'; // reset the configuration file directory $ _ tpl-> left_delimiter = '<{'; // reset the left delimiter of the website $ _ tpl-> right_delimiter = '}>'; // reset the right delimiter of the website Operator?>
Index. php
<? Php require 'init. inc. php'; // introduce the template initialization file global $ _ tpl; $ _ tpl-> display ('index. tpl '); // load the template file?>
There are two ways to use index. tpl configuration variables:
1. <{# configuration variable #}>;
Ii. <{$ smart. config. configuration variable}>
<{Config_load file = "view. conf" section = "one"}> <! -- The view. conf file cannot write the complete path, because it is specified in the initialization file. section = "one" indicates loading the [one] region --> <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
/Configs/view. conf configuration file
border=2tabw=600tabh=500bgcolor=yellowalign=center[one]colspan=4aa=one section[two]bb=two section[three]cc=three section
Execution result ,: