This article describes the method of the smarty template engine to obtain data from configuration files. Share it for your reference. The details are as follows:
When a variable value does not want to be written dead in the program, you can write the variable to the configuration file and get it from it (common configuration styles).
Step 1: Write a configuration file first, such as the database, and the suffix name conf can be written at will, it is OK. The format of the content in the file needs to be fixed: key="value". There is no need to add a semicolon or anything after each line. You can go back to the country and wrap it directly, such as:
Configuration file:
username = "root"
password = "123456"
db_name = "liuyan"
Template file:
Use {config_load file=""} to bring in the file. Note that if you write a relative path, you should look at the page you visited. For example, here, it is placed in the templates directory, and it is placed on the same layer as the templates directory, but since the files accessed by the browser are the same layer, when referring, write {config_load file=""} directly.
<html>
<h2>smarty variable operation, get from the configuration file</h2>
<p style="color:green;">{#host#}</p>
<p style="color:red;">{#username#}</p>
</html>
Browser access:
Unlike obtaining variable data from php, there is no need to use assign to allocate here, but it is loaded directly in the template file.
//Create smarty object
require_once("./libs/");
$smarty = new Smarty();
$smarty->display("");
?>
I hope this article will be helpful to everyone's smarty programming.