The smarty template engine obtains data from the configuration file. The smarty template _ PHP Tutorial

Source: Internet
Author: User
The smarty template engine obtains data from the configuration file. The smarty template engine obtains data from the configuration file. This document describes how the smarty template engine obtains data from the configuration file. For your reference, the smarty template engine obtains data from the configuration file.

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.

Examples in this article describes how the smarty template engine obtains data from the configuration file. Share it with you for your reference...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.