PHP and MySQL programming (version 3rd) -- 19.6 create a configuration file

Source: Internet
Author: User

Developers always use configuration files to store data that determines application behavior and operations. For example, the php. ini file determines a large number of PHP behaviors. For Smarty, template designers can also take advantage of the powerful role of the configuration file. For example, a designer can use a configuration file to store the page title, user messages, and any information that needs to be centrally stored.

The following is an example configuration file named app. config ):

The items enclosed in parentheses are called sections ). Items outside of the section are considered global. These items should be defined before any section is defined. The next section shows how to use the config_load function to load the configuration file and explains how to reference configuration variables in the template. Finally, note that the data of the warning variable is enclosed in three quotation marks. This syntax is required if the string contains multiple lines of the file.

Note: Of course, the Smarty configuration file will not replace the stacked style sheet (CSS ). CSS can be used in website design (background color, Font, etc.), and configuration files are used in areas not supported by CSS (such as page titles.

19.6.1 Config_Load

The configuration file is stored in the configs directory and loaded using the Smarty function config_load. The following is an example of loading the configuration file app. config:

But remember, this call can only load the global variables of the configuration file. To load a specific section, use the section attribute. Therefore, you can use the following syntax to load the Aggregation section of app. config:

The other two optional parameters are described as follows.

Q scope. Determine the scope of the loaded configuration variable. By default, this parameter is set to local, indicating that the variable can only be used in a local template. Other possible settings include parent and global. When the scope is set to parent, variables can be used in local templates and call templates. When the scope is set to global, variables can be used in all templates.

Q section. Specifies the specific section for loading the configuration file. Therefore, if you are only interested in a specific section, you can only load the section, not the entire file.

19.6.2 Reference configuration variables

The reference method of variables in the configuration file is different from that of other variables. In fact, these configuration variables are referenced using several different syntaxes. The following sections will introduce this content.

1 .#

In the Smarty template, you can add # Before the variable to reference the configuration variable. For example:

2. Smarty's$ Smarty. configVariable

When referencing configuration variables, if you prefer more formal syntax, you can use the $ Smarty. config variable of smarty. For example:

3.Get_Config_Vars ()Method

The get_config_vars () method returns an array containing all the loaded configuration variable values. If you are only interested in a variable value, you can pass in the variable through variablename. For example, if you are only interested in the $ title variable of the Aggregation section in the above app. config configuration file, you can first use the config_load function to load the Section:

Then, call get_config_vars () in the section that enables PHP in the template, as shown below:

Of course, do not forget to use the config_load function to load the configuration file no matter which syntax you select to obtain the configuration parameters.

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.