Smarty Usage Description _php Example of MVC framework based on PHP web development

Source: Internet
Author: User

A concise course of smarty
1. Installation Demo
Download the latest version of Smarty-3.1.12 and unzip the downloaded file. Next, we'll show you the demo smarty.
(1) Download address: Http://www.smarty.net/download
(2) Create a new directory under your Web server root directory, where I created the yqting/directory under/var/www, and then copy the demo/and libs/directories in the unpacked directory to the/var/www/yqting/directory.
(3) Here to pay special attention to the demo/directory under the cache/and template_c/two directories,Be sure to set them to read-write permissions
chmod 777 cache/
chmod 777 template_c/
(4) Start Apache. Enter http://localhost/yqting/demo/index.php in the browser, such a simple Smarty demo is implemented.
2.Smarty directory structure
(1) Starting with the/var/www/yqting directory analysis:
yqting/
├──demo
│├──cache Buffer File storage directory
│├──configs Configuration file Directory
│├──index.php
Some useful plug-ins that │├──plugins custom
│├──templates Template Directory
│└──templates_c compiled file storage directory
└──libs
├──DEBUG.TPL Debug Template
Some useful plug-ins that ├──plugins custom
├──smartybc.class.php supports Smarty 2 compatibility
├──smarty.class.php Smarty class definition file
└──sysplugins smarty Core functionality plugin, no modification required
(2) Add your own definition of plug-ins
In the above directory structure, the core part is the libs/directory, and this part is not allowed to modify.
And to add your own plug-ins, one way is to put your own defined plug-ins in the libs/plugins/directory, the other way is to create a separate plugins/directory, but also to create cache/, configs/, templates/and Templates _ directory, and to ensure that the cache/and templates_c/directories have read and write permissions.
It's not hard to find, in fact, that the demo/directory is a complete directory containing the plug-ins you define. We can refer to the demo/directory to implement our own program.
3. To achieve a simple example
(1) Create a directory weibo/under/var/www/yqting/, then create cache/, configs/, templates/, and templates_c/directories under the weibo/directory, modify cache/and templates_c/ The permissions for the directory are readable and writable.
(2) Create a new template file: Index.tpl, put this file in the/var/www/yqting/weibo/templates directory, the code is as follows:
<metahttp-equiv= "Content-type" content= "text/html;charset=gb2312" >
<title>Smarty</title>
<body>
username:{$Name}
</body>
(3) New index.php, put this file under/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 '); ?> which require use the path must be correct, you can refer to the above directory structure to see!
(4) in Smarty3, Template_dir, Compile_dir, Config_dir, and Cache_dir have already been specified in the constructor of the Smarty class and do not need to be specified again.
(5) In the browser input http://localhost/yqting/weibo/index.php, you can see the output of the information username:smarty.
ii. procedures for interpreting Smarty
We can see that the Smarty part of the program is actually a set of code that conforms to the PHP language specification, which we explain in turn:
(1)/**/statement:
The included section is a program header comment. The main content should be the role of the program, copyright and the author and writing time to do a simple introduction, which is not required in the smarty, but from the style of the program, this is a good style.
(2) include_once statement:
It will be installed to the Web site Smarty file included in the current file, note that the included path 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= "":
This sentence indicates the path of the $smarty object when using the TPL template, which is a directory, in the absence of this sentence, smarty the default template path for the current directory templates directory, in fact, when writing a program, we want to write this sentence, this is a good program style.
(5) $smarty->templates_c= "":
This sentence indicates the directory at compile time for the $smarty object. In the template design article we already know that Smarty is a compiled template language, and this directory is the directory where it compiles templates, and note that if the site is on a Linux server, make sure that the directory defined in Teamplates_c has writable readable permissions, by default its The compiled directory is the Templates_c in the current directory, and for the same reason we write it out clearly.
(6) Separator $smarty->left_delimiter and $smarty->right_delimiter:
Indicates the left and right delimiters when looking for template variables. The default is "{" and "}", but in practice because we want to use the function definition in <script>,script in the template will inevitably use {}, although it has its own solution, but we are accustomed to redefine it as "{#" and "#}" or "<!--{ "and"}--> "or other markers, note that if you define the left and right delimiters here, in the template file, you want each variable to use the same symbol as the definition, such as" <{"and"}> "here, and the HTML template should also change {$name} to <{$name}&gt so that the program can correctly locate the template variable.
(7) $smarty->cache= "./cache":
Tells Smarty the location of the template file cache for output. We know that the biggest advantage of smarty is that it can be cached, and here is the directory where the cache is set. By default, the cache directory in the current directory, as in the Templates_c directory, in the Linux system, we want to ensure that it is readable and writable.
(8) $smarty->cache_lifetime = 60 * 60 * 24:
This will be the time in seconds for the calculation of the cache to be valid. When the first cache time expires, the cache is rebuilt when the Smarty caching variable is set to True. When its value is-1, the established cache is never expired, and 0 indicates that the cache is always being reset every time the program executes. The above setting indicates that the Cache_lifetime is set to one day.
(9) $smarty->caching = 1:
This property tells Smarty whether to cache and how to cache it.
It can take 3 values, 0:smarty the default value, and indicates that the template is not cached; 1: Indicates that Smarty will use the currently defined cache_lifetime to decide whether to end Cache;2: that Smarty will use when cache is built cache_ Lifetime this value. It is customary to use true and false to indicate whether caching is done.
(a) $smarty->assign ("name", $username):
The prototype of the number is assign (string varname, mixed Var), varname the template variable used in the template, var indicates the variable name to replace the template variable, and the second prototype is assign (mixed Var). We want to explain in detail the use of this member function in the following example, assign is one of the core functions of smarty, and all substitutions to template variables are to be used.
(one) $smarty->display ("Index.tpl"):
This function is displayed as display (string varname), which acts as a template. Simply put, it shows the parsed template, where the template file does not need a path, as long as a filename is available, and the path we have defined in $smarty->templates (string path).
After the execution of the program we can open the current directory of the Templates_c and cache directory, you will find in the bottom of a number of% of the directory, these directories are smarty compiled and cached directories, it is automatically generated by the program, do not directly modify these generated files.
I've simply introduced some of the most common basic elements in the Smarty program, and in the example below you can see that they will be used more than once.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.