This article mainly introduces how the smarty template engine obtains data from the configuration file, involving config_load loading to load the configuration file and related reading skills. For more information, see
This article mainly introduces how the smarty template engine obtains data from the configuration file, involving config_load loading to load the configuration file and related reading skills. For more information, see
This example describes how the smarty template engine obtains data from the configuration file. Share it with you for your reference. The details are as follows:
If you do not want to write a variable value to a program, you can write the variable to the configuration file and obtain it (common configuration styles ).
Step 1: first write a configuration file, such as db. conf of the database. You can write the configuration file with the suffix conf at will, and db. ini can also be used. The format of the content in the file must be fixed: key = "value". Do not add a semicolon or a semicolon to the end of each line. You can directly return to China for a line break, for example:
Configuration File: db. conf
The Code is as follows:
Host = "localhost"
Username = "root"
Password = "123456"
Db_name = "liuyan"
Template File: temp. tpl
Use {config_load file = "db. conf"} to introduce the file. Note: If you write a relative path, you should refer to the accessed page. For example, temp. tpl is stored in the templates directory, db. conf is placed on the same layer as the templates directory, but because of the file index accessed by the browser. php and db. conf is on the same layer. Therefore, when referencing, write {config_load file = "db. conf "}.
The Code is as follows:
{Config_load file = "db. conf "}
Smarty variable operation, obtained from the configuration file
{# Host #}
{# Username #}
Browser access: index. php
Unlike getting variable data from php, assign allocation is not required here, but directly loaded in the template file.
The Code is as follows:
<? Php
// Create a smarty object
Require_once ("./libs/Smarty. class. php ");
$ Smarty = new Smarty ();
$ Smarty-> display ("index. tpl ");
?>
I hope this article will help you design your smarty program.