Home >Backend Development >PHP Tutorial >How the smarty template engine obtains data from the configuration file, smarty template_PHP tutorial

How the smarty template engine obtains data from the configuration file, smarty template_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:09:14761browse

How the smarty template engine obtains data from the configuration file, smarty template

The example in this article describes how the smarty template engine obtains data from the configuration file. Share it with everyone for your reference. The details are as follows:

When you do not want the value of a variable to be hard-coded in the program, you can write the variable into the configuration file and obtain it from it (common configuration style).

Step one: First write a configuration file, such as db.conf of the database. You can write the suffix name conf casually, or db.ini. 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. Just return to the country and change the line, such as:

Configuration file: db.conf

Copy code 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 import the file. Note that if you write a relative path, it must be viewed based on the page you are visiting. For example, here, temp.tpl is placed in the templates directory, and db.conf is placed on the same level as the templates directory. However, since the file index.php accessed by the browser is on the same level as db.conf, when referencing it, write {config_load directly. file="db.conf"} .

Copy code The code is as follows:
{config_load file="db.conf"}

smarty variable operation, obtained from the configuration file


{#host#}


{#username#}


Browser access: index.php

Different from getting variable data from php, there is no need to assign it here, but it is loaded directly in the template file

Copy code The code is as follows:
//Create smarty object
require_once("./libs/Smarty.class.php");
$smarty = new Smarty();
$smarty->display("index.tpl");
?>

I hope this article will be helpful to everyone’s smarty programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/946751.htmlTechArticleHow the smarty template engine obtains data from the configuration file, smarty template This article describes how the smarty template engine obtains data from the configuration file method to obtain data. Share it with everyone for your reference...
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