When using PHP to develop large and interactive websites, we often encounter the problem of how to cooperate with the artist. Generally, our solution is to design the page and deliver it to the program designer for development, the delivery artist improved the page and repeat it several times back and forth. If the programmer is unfamiliar with HTML, it is even more difficult for both parties, and the efficiency is also lower, in this case, template support is very important.
We know that as a member of the open-source community, PHP provides various template engines, such as fasttemplate, smarty, and simpletemplate. However, smarty is a PHP template engine that currently uses a lot of resources, today, I want to share with you how to install and use smarty in PHP development.
Preparations
1. Select the directory for installing smarty
If you have server permissions, you can choose to install smarty outside the Web application documentation directory, and then include the smarty installation directory address in the include_path option in the PHP. ini file.
If it is a virtual host permission or several projects, you can install smarty in their respective project directories, In the require smarty file, or use the smarty template engine. For the sake of security, you can use Apache to disable access to related directories.
In addition, the two smarty installation methods differ in portability. The first method requires that each server has the same smarty configuration. The second method does not affect the configuration of each server. You can select the smarty installation method as needed.
2. Download smarty. Click here to download smarty. I chose smarty-2.6.25.
Install Smarty
1. decompress the downloaded smarty-2.6.25 package
2. Copy the libs folder to the web program directory. My directory is test/smarty.
For installation methods in Linux, refer to here.
After installing the smarty template, we started to use the smarty.
Use Smarty
1. Create related directories
Because in the process of using smarty, smarty generates compiled template files, other configuration files, and cache files, we need to create related directories. I created the tpls directory under the test/smarty directory, and created the templates, templates_c, configs, and cache directories under the tpls directory. Why do we need to create these directories? Open the smarty. Class. php file. We can see that the smarty class defines some member attributes.
$ Template_dir: Set the directory address to be placed for all template files. By default, the directory is "./templates", that is, find the template directory under the same directory of the PHP Execution program.
$ Compile_dir: Sets the directory where all template files compiled by smarty are stored. The default directory is "./templates_c", that is, find the compiling directory under the same directory of the PHP Execution program. If you create this directory on a Linux server, you also need to modify the directory permission so that it has the write permission.
$ Config_dir: Set the directory used to store the special configuration files of the template. The default directory is "./configs", that is, find the configuration directory under the same directory of the PHP Execution program.
$ Cache_dir: When the cache feature is enabled, all templates cached by smarty are placed in the directory specified by this attribute. The default directory is "./cache", that is, to find the cache directory under the same directory of the PHP Execution program. You can also use your custom cache handler to control cached files, which will ignore this setting. Similarly, if you create this directory on a Linux server, you also need to modify the directory permission so that it has the write permission.
For the sake of system security and portability, we recommend that you do not create these directories in the same directory of the PHP Execution program. You can create these directories in addition to the PHP Execution program directory, if you have already created a PHP Execution program in the same directory, you can use Apache to restrict access to directories.
2. Create related configuration files
We need to create a configuration file to overwrite the default member attributes of the smarty class and name it main. PHP, saved in the smarty Directory, which script needs to use smarty in the future, we just need to put main. include PHP.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<? Include ("./smarty/libs/Smarty. class. php "); Define ('smarty _ root', './SMARTY/tpls '); $ Tpl = new Smarty (); $ Tpl-> template_dir = SMARTY_ROOT. "/templates /"; $ TPL-> compile_dir = smarty_root. "/templates_c /"; $ TPL-> config_dir = smarty_root. "/configs /"; $ TPL-> cache_dir = smarty_root. "/Cache /"; $ TPL-> caching = 1; $ TPL-> cache_lifetime = 60*60*24; $ TPL-> left_delimiter = '<{'; $ TPL-> right_delimiter = '}> '; ?> |
Note:
Line 1-8: defines a smarty object, and sets the directory for storing template files, compilation files, cache files, and configuration files to overwrite the default values in smarty. Class. php.
Row 9-10: set to enable the cache and set the cache validity period to 1 day.
Knowledge Point: $ Caching is used to set whether the cache function is enabled. The default value is 0 or invalid. You can also set multiple caches for the same template. When the value is 1 or 2, the cache is started. 1. Tell smarty to use the current $ cache_lifetime variable to determine whether the cache has expired. 2. Tell smarty to use the cache_lifetime value when the cache is generated. We recommend that you disable caching during project development and set the value to 0.
Line 11-12: Set the Left and Right terminator of the smarty language. We know that braces are the default delimiters of the smarty language, but they may conflict when combined with JavaScript and CSS, so here we set it to <{And}>.
3. Create a template file
Generally, after the design of the meigong page is completed, the intersection of the two parties is a template file. after both parties agree, programmers do not need to spend too much energy on the front-end, this is the benefit of using the smarty template engine for development.
First, create a simple template file named leapsoul. TPL. You can add the smarty variable to the HTML file and save the file as a TPL file.
1 2 3 4 5 6 7 8 9 10 11 |
<HTML> <Head> <Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"> <Title> <{$ Title}> </Title> </Head> <Body> <{$ Content}> </Body> </Html> |
Note: The title and content smarty variables are set in the TPL file. The files are saved as leapsoul. TPL and saved in the directory of the test/smarty/tpls/templates template file.
4. Create an application file
The template file is similar to a presentation layer. After the template file is created, we need an application to drive the presentation layer. The application file is defined as smarty. php.
1 2 3 4 5 6 7 8 9 |
<? Include ("Smarty/Main. php ");$ TPL-> assign ("title", "leapsoul.cn: smarty template technology "); $ TPL-> assign ("content", "leapsoul.cn demonstrate the smarty template technology through detailed installation and usage steps "); $ TPL-> display ("leapsoul. TPL "); ?> |
Note:
In this Code, we mainly use the two functions assign and display in smarty. Assign can be understood as a variable assignment, and display is mainly used to output webpages. More smarty functions will be introduced in detail in the future.
Other Instructions
Since we have enabled the cache function, interested friends can open cache and templates_c. The cache directory stores the cached files of this template. The file starts with cache information, for example, the file generation time and expiration time are not much different from other HTML files, while templates_c stores the PHP Execution file compiled by the template.
Now, the introduction of a simple getting started Smarty template application instance is complete.