Home > Article > Backend Development > First experience with Smarty templates in php_PHP tutorial
The following introduces the features of Smarty template engine:
1. Speed: Programs written using Smarty can achieve maximum speed improvements, which is compared to other template engine technologies.
2. Compiled type: A program written in Smarty must be compiled into a non-template technology PHP file at runtime. This file uses a mixture of PHP and HTML. The WEB request will be directly converted to this file the next time the template is accessed. file without recompiling the template (when the source program has not been changed)
3. Caching technology: A caching technology selected by Smarty. It can cache the HTML file that the user finally sees into a static HTML page. When the cache attribute of Smarty is set to true, in the Smarty setting During the cachetime period, the user's WEB request is directly converted into this static HTML file, which is equivalent to calling a static HTML file.
4. Plug-in technology: Smarty can customize plug-ins. Plug-ins are actually some custom functions.
5. If/elseif/else/endif can be used in templates. Using judgment statements in template files can very conveniently reformat the template.
Using Smarty template version Smarty-3.0.8, the unzipped file directory is as follows:
So began my Smarty journey——
Step 1
Create a new smartytest folder in the server web page folder, take only the files in the libs directory, copy them to the smartytest folder, and rename it to smarty
Step 2
Create a new directory templates in test, and create four new folders cache, configs, templates, templates_c in this directory. The created folder format is as shown below
Step 3
Write a configuration file through which you can connect to Smarty, and write it as a separate file to rewrite the same code when writing different pages (of course you can also write it in a class form for easy customization), Here I name its file as config.php
Step 6
Test file:
Summary: I also encountered problems when using Smarty templates. For example, the directory of Smarty can be set by yourself, which is highly customizable, so there are many versions of Smarty tutorials (I saw a version in a book, and Baidu Encyclopedia also has it Another type), but I didn’t understand how to put it after referring to both. In the end, I still used the one from Baidu Encyclopedia (which is what I mentioned above); secondly, the code in Baidu Encyclopedia was copied, and the result was that when using it A syntax error, unexpected T_VARIABLE error occurred. The code looked correct, but there was an error. Finally, I found the cause and found that it was caused by full-width spaces in the web page. Therefore, the best way to find inexplicable errors in the code copied on the web page is to All the spaces have been removed and rewritten; note that hello does not have the $ sign in front of it when writing the PHP file, but the $ sign must be added when quoted in the tpl file.