Smarty instructions for developing MVC framework based on PHP Web

  • 2020-06-01 08:22:51
  • OfStack

1. Smarty concise tutorial
1. Installation demo
Download the latest version of Smarty-3.1.12 and unzip the downloaded file. Let's show you the demo example that comes with Smarty.
(1) the download address: http: / / www smarty. net/download
(2) create a new directory in your WEB server root. Here I create the yqting/ directory under /var/www, and then copy the demo/ and libs/ directories in the unzipped directory to /var/www/yqting/ directory.
(3) special attention should be paid to the two directories cache/ and template_c/ in the demo/ directory.
chmod cache / 777
chmod template_c / 777
(4) start apache. In the browser input http: / / localhost yqting demo/index php, such a simple Smarty demo is achieved.
2.Smarty directory structure
(1) start with the /var/www/yqting directory:
yqting /
├ ─ ─ demo
│ ├─ cache buffer file storage directory
│ ├─ configs configuration file directory
│ ├ ─ ─ index php
│ ├─ plugins custom 1 some practical plug-in
│ ├─ templates template directory
│ └ ─ ─ templates_c compiled file storage directory
└ ─ ─ libs
Adobe flash player = adobe flash player = adobe flash player = adobe flash player = adobe flash player = adobe flash player = adobe flash player = adobe flash player
Adobe flash player = adobe flash player = adobe flash player = adobe flash player = adobe flash player = adobe flash player = adobe flash player = adobe flash player = adobe flash player = adobe flash player
Adobe flash player ─ SmartyBC.class.php supports Smarty 2 compatibility
Adobe flash player = adobe flash player = adobe flash player = adobe flash player = adobe flash player = adobe flash player = adobe flash player = adobe flash player = adobe flash player = adobe flash player = adobe flash player = adobe flash player = adobe flash player
└ ─ ─ sysplugins Smarty core function plug-ins, do not need to modify
(2) add your own plug-in
In fact, the core part of the above directory structure is libs/ directory, and this part is not allowed to be modified.
One way to add your own plug-in is to put your own plug-in in the libs/plugins/ directory. The other way is to create a separate plugins/ directory, as well as cache/, configs/, templates/ and templates _c/ directories, and ensure that cache/ and templates_c/ directories have read-write access.
It is easy to see that in the above example, the demo/ directory is a full directory containing the plug-ins you have defined. We can use the demo/ directory to implement our program.
3. Implement a simple example
(1) under the/var www yqting/create weibo/directory, and then create cache in weibo/directory /, configs /, templates/and templates_c/directory, modify cache/and templates_c/directory permissions for read/write.
(2) create a template file: index. tpl, put this file in/var/www/yqting weibo/templates directory, the code is as follows:
< html >
< head >
< metahttp-equiv="Content-Type" content="text/html;charset=gb2312" >
< title > Smarty < /title >
< /head >
< body >
username: {$Name}
< /body >
< /html > This code is very simple, what do not understand continue to look down, don't worry! For each.tpl file displayed, there is a.php file that handles the business logic. Here is the.php file.
(3) new index php, put this file in/var/www/yqting weibo /, the code is as follows:
< ? php /*sample example */require '... / libs Smarty. class. php '; $smarty = new Smarty (); $username = "Smarty"; $smarty - > assign (" Name ", $username); $smarty - > display (' index. tpl '); ? > The path 1 used by require must be correct, you can refer to the directory structure above to see 1!
(4) in Smarty3, the constructor of Smarty class has specified template_dir, compile_dir, config_dir and cache_dir, which need not be specified again.
(5) in the browser input http: / / localhost yqting weibo/index php, you can see the output information username: Smarty.
2. Explain the smarty program
As we can see, the program part of smarty is actually a set of code that conforms to the language specification of php. Let's explain it one by one:
(1) /**/ statement:
The included section is the program header comment. The main content should be a brief introduction to the function of the program, the copyright and the author, and the writing time. This is not required in smarty, but it is a good style in terms of the style of the program.
(2) include_once statement:
It includes the smarty file installed on the web site in the current file. Note that the included path 1 must be written correctly.
(3) $smarty = new Smarty():
This sentence creates a new Smarty object $smarty, a simple instantiation of an object.
(4) $smarty - > templates="":
The default template path of Smarty is the templates directory of the current directory. In the absence of this sentence, Smarty's default template path is the templates directory of the current directory. In fact, when writing a program, we need to specify this sentence, which is also a good program style.
(5) $smarty - > templates_c="":
This 1 sentence indicates the directory where the $smarty object was compiled. In the template design we already know Smarty is one kind of compiled template language, and the directory, is it compiled template directory, note that if your site is located in linux server, please ensure that the directory can be defined within teamplates_c write can read permission, by default it build directory is the current directory templates_c, for the same reason we will be clear.
(6) separator $smarty- > left_delimiter with $smarty - > right_delimiter:
Specifies the left and right separators when looking for template variables. The default is "{" and "}", but in practice because we're going to use them in templates < script > The function definition in Script will inevitably use {}. Although it has its own solution, it is customary to redefine it as "{#" and "#}" or" < ! -- - {" and "} > "Or any other identifier, note that if the left and right delimiters are defined here, the corresponding symbol for each variable in the template file should be the same as the definition, for example, specified here as" < {" and "} > ", html template should also be corresponding to {$name} < {$name} > , so that the program can correctly find the template variables.
(7) $smarty - > cache="./cache":
Tell Smarty the location of the output template file cache. As we learned in the previous article, the biggest advantage of Smarty is that it can be cached. Here is the directory where the cache is set. The default is the cache directory in the current directory, which is equivalent to the templates_c directory, and in the linux system we want to make sure it is readable and writable.
$smarty - (8) > cache_lifetime = 60 * 60 * 24:
This will calculate the time in seconds for the cache to be valid. When the first cache time expires, the cache will be rebuilt when Smarty's caching variable is set to true. A value of -1 means that the cache established never expires, and a value of 0 means that the cache is always re-established every time the program is executed. The Settings above indicate setting cache_lifetime to 1 day.
(9) $smarty - > caching = 1:
This property tells Smarty whether to cache and how to cache.
It can take 3 values, 0: Smarty default value, indicating that the template is not cached; 1: indicates that Smarty will use cache_lifetime as currently defined to decide whether to terminate cache; 2: indicates that Smarty will use the value cache_lifetime when cache is set up. It is customary to use true and false to indicate whether or not to cache.
(10) $smarty - > assign (" name ", $username) :
The prototype of this number is assign(string varname, mixed var). varname is the template variable used in the template. var indicates the variable name to replace the template variable. The second prototype is assign(mixed var). We will explain the usage of this member function in detail in the following examples. assign is the core function of Smarty, and all substitutions of template variables will use it.
$smarty - (11) > display (" index. tpl ") :
This function is display(string varname) and displays one template. Simply put, it displays the template that has been analyzed. The template file here does not need to have a path, just use a file name. We have already found the path in $smarty- > templates(string path) is defined.
After the execution of the program, we can open the templates_c and cache directories in the current directory, and we will find that there are 1 %% more directories at the bottom. These directories are the compiled and cached directory of Smarty, which is generated by the program automatically, and we do not need to modify the generated files directly.
Above I briefly introduced some common basic elements of the Smarty program, which you can see will be used many times in the following examples.


Related articles: