Home >Backend Development >PHP Tutorial >Instructions for using Smarty based on PHP Web development MVC framework_PHP tutorial
1. Smarty concise tutorial
1. Installation demonstration
Download the latest version of Smarty-3.1.12, and then unzip the downloaded file. Next, we will demonstrate the demo example that comes with Smarty.
(1) Download address: http://www.smarty.net/download
(2) Create a new directory in the root directory of your WEB server. Here I create the yqting/ directory under /var/www , and then copy the demo/ and libs/ directories in the decompressed directory to the /var/www/yqting/ directory.
(3) Pay special attention to the cache/ and template_c/ directories under the demo/ directory. Be sure to set them with read and write permissions.
chmod 777 cache/
chmod 777 template_c/
(4) Start apache. Enter http://localhost/yqting/demo/index.php in the browser, and a simple Smarty demo is implemented.
2. Smarty directory structure
(1) Start the analysis with the /var/www/yqting directory:
yqting/
├── demo
│ ├── cache Cache file storage directory
│ ├── configs Configuration file directory
│ ├── index.php
│ └── templates_c Compiled file storage directory
└── libs
├── debug.tpl debug template
├── plugins Some useful plug-ins for customization
├ ─ ─ SmartyBC.class.php Support Smarty 2 compatible
├── Smarty.class.php Smarty class definition file
└── sysplugins Smarty core function plug-in, no need to modify
(2) Add your own definition Plug-in
In the above directory structure, the core part is actually the libs/ directory, and this part is not allowed to be modified.
To add your own plug-ins, one way is to place your own defined plug-ins in the libs/plugins/ directory, and the other way is to create your own plugins/ directory, and also create cache/ and configs/ , templates/ and templates_c/ directories, and ensure the read and write permissions of the cache/ and templates_c/ directories.
It is not difficult to find that in fact, in the above example, the demo/ directory is a complete directory containing self-defined plug-ins. We can refer to the demo/ directory to implement our own program.
3. Implement a simple example
(1) Create the directory weibo/ under /var/www/yqting/, and then create cache/, configs/, templates under the weibo/ directory / and templates_c/ directories, modify the permissions of cache/ and templates_c/ directories to read and write. (2) Create a new template file: index.tpl, and place this file in the /var/www/yqting/weibo/templates directory. The code is as follows: