Home  >  Article  >  Backend Development  >  Instructions for using Smarty based on PHP Web development MVC framework_PHP tutorial

Instructions for using Smarty based on PHP Web development MVC framework_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:11:41837browse

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:
 
 
 
 Smarty
 

username:{$Name}

Each .tpl file for display will correspond to a .php file that handles business logic. This .php file is introduced below.
(3) Create a new index.php and place this file under /var/www/yqting/weibo/. The code is as follows:
 assign("Name",$username); $smarty->display('index.tpl') ; ?> The path used by require must be correct. You can refer to the directory structure above to take a look!
(4) In Smarty3, template_dir, compile_dir, config_dir and cache_dir have been specified in the constructor of the Smarty class and do not need to be specified again.   
(5) Enter http://localhost/yqting/weibo/index.php in the browser, and you can see the output information username:Smarty.

2. Explain the smarty program
We can see that the program part of smarty is actually a set of codes that conform to the PHP language specification. Let’s explain it in turn:
(1) /**/Statement:
The included part is the program header comment. The main content should be a brief introduction to the function of the program, copyright, author and writing time. This is not necessary in smarty, but from the perspective of the style of the program, this is a good style. ​
(2) include_once statement:
It will include the smarty file installed on the website into the current file. Note that the included path must be written correctly.   
(3)$smarty = new Smarty():
  This sentence creates a new Smarty object $smarty, which is a simple instantiation of an object.
(4) $smarty->templates="":
This sentence specifies the path when the $smarty object uses the tpl template. It is a directory. Without this sentence, Smarty's default template path is the current The templates directory of the directory. When actually writing a program, we need to specify this sentence. This is also a good programming style.   
(5)$smarty->templates_c="":
 This sentence specifies the directory where the $smarty object is compiled. In the template design chapter, we already know that Smarty is a compiled template language, and this directory is the directory where it compiles templates. Please note that if the site is located on a Linux server, please ensure that the directory defined in teamplates_c is writable and readable. Permissions, by default its compilation directory is templates_c in the current directory, for the same reason we write it out explicitly. ​
(6)Delimiter $smarty->left_delimiter and $smarty->right_delimiter:
Specifies the left and right delimiters when looking for template variables. By default, it is "{" and "}", but in practice, because we need to use