PHPWeb-based development of the MVC Framework Smarty instructions _ php instance

Source: Internet
Author: User
Tags php language
This article introduces the usage of Smarty Based on PHPWeb MVC framework. For more information, see 1. Brief Smarty tutorial
1. Installation demonstration
Download the latest version of Smarty-3.1.12, and decompress the downloaded file. Next, we will demonstrate the demo provided by Smarty.
(1): http://www.smarty.net/download
(2) create a new directory under the root directory of your WEB server. Here I create the yqting/directory under/var/www, 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 in the demo/directory, Be sure to set them as read/write permissions.
Chmod 777 cache/
Chmod 777 template_c/
(4) start apache. Enter http: // localhost/yqting/demo/index. php In the browser to implement a simple Smarty demo.
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
│ ── Some practical plug-ins customized by plugins
│ ── Templates template directory
│ ── Templates_c directory for storing compiled files
── Libs
── Debug. tpl debug Template
── Some practical plug-ins customized by plugins
├ ── SmartyBC. class. php supports Smarty 2 compatibility
├ ── Smarty. class. php Smarty class definition file
└ ── Sysplugins Smarty core function plug-in, no need to modify
(2) Add a custom plug-in
In the preceding directory structure, the core part is the libs/directory, which cannot be modified.
To add your own plug-ins, you can put your own defined plug-ins under the libs/plugins/directory, and create a separate plug-ins/directory, you must also create the cache/, configs/, templates/, and templates _ c/directories, and ensure the read and write permissions of the cache/and templates_c/directories.
In the preceding example, the demo/directory is a complete directory containing your own 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 the cache/, configs/, templates/, and templates_c/directories under the weibo/directory, modify the cache/and templates_c/directory permissions to read and write.
(2) create a new template file: index. tpl. Place the file in the/var/www/yqting/weibo/templates directory. The Code is as follows:
  
  
  
   Smarty
  
  
Username: {$ Name}
  
  This code is very simple. If you don't understand anything, continue reading it. Don't worry! Each displayed. tpl file corresponds to a. php file that processes the business logic. The. php file is described below.
(3) create index. php and put the 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 above directory structure to see it!
(4) In Smarty3, the constructor of the Smarty class has specified template_dir, compile_dir, config_dir, and cache_dir. You do not need to specify them again.
(5) Enter http: // localhost/yqting/weibo/index. php In the browser to view the output username: Smarty.
2. Explain the smarty Program
As we can see, the smarty program is actually a set of code that complies with the php language specification. Let's explain it one by one:
(1)/**/statement:
The included part is the program header annotation. The main content should be a brief introduction to the role of the program, copyright and the author and the writing time, which is not necessary in the smarty, but in terms of the program style, this is a good style.
(2) include_once statement:
It includes the smarty file installed on the website to the current file. Note that the included path must be correctly written.
(3) $ smarty = new Smarty ():
This statement creates a Smarty object $ smarty, which is a simple Object Instantiation.
(4) $ smarty-> templates = "":
This statement specifies the path for the $ smarty object to use the tpl template. It is a directory. If this statement is not provided, the default template path of Smarty is the templates directory of the current directory, when writing a program, we need to write this sentence, which is also a good program style.
(5) $ smarty-> templates_c = "":
This statement specifies the directory when the $ smarty object is compiled. In the template design article, we already know that Smarty is a compilation template language, and this directory is the directory for compiling templates. Note that if the site is on a linux server, make sure that the directory defined in teamplates_c has the write and read permissions. By default, the compiled directory is the templates_c under the current directory. For the same reason, we will write it clearly.
(6) Delimiter $ smarty-> left_delimiter and $ smarty-> right_delimiter:
Specifies the Left and Right delimiters when searching for template variables. The default values are "{" and "}", but actually, because we want to use "script" in the template, the function definition in the Script will inevitably use {}, although it has its own solution, we are used to redefining it as "{#" and "#}" or" "Or other flags. Note: If the left and right delimiters are defined here, each variable must use the same symbol as the definition in the template file, for example, if you specify "<{" and "}>" here, you must change {$ name} to <{$ name}> in the html template, in this way, the program can find the template variables correctly.
(7) $ smarty-> cache = "./cache ":
Tell the Template File Cache location output by Smarty. In the previous article, we learned that the biggest advantage of Smarty is that it can be cached. Here, we set the cache directory. By default, it is the cache directory under the current directory, which is equivalent to the templates_c directory. in linux, we need to ensure its readability and writability.
(8) $ smarty-> cache_lifetime = 60*60*24:
Here, the effective cache time is calculated in seconds. When the first cache time expires, when the caching variable of Smarty is set to true, the cache will be rebuilt. When the value is-1, it indicates that the created cache never expires. If it is 0, it indicates that the saved cache is always re-created every time the program is executed. The preceding settings indicate that cache_lifetime is set to one day.
(9) $ smarty-> caching = 1:
This attribute tells Smarty whether to cache and how to cache it.
It can take three values, 0: Smarty default value, indicating that the template is not cached; 1: indicates that Smarty will use the current defined cache_lifetime to determine whether to end the cache; 2: indicates that Smarty uses the cache_lifetime value when the cache is created. Traditionally, true and false are used to indicate whether the cache is performed.
(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, and var indicates the variable name to replace the template variable; the second prototype is assign (mixed var). We will explain in detail how to use this member function in the following example. assign is one of the core functions of Smarty, all replace template variables must use it.
(11) $ smarty-> display ("index. tpl "):
Display (string varname) is used to display a template. To put it simply, it displays the templates that have been analyzed and processed. The template file here does not need to be added with a path. You only need to use a file name, we have defined its path in $ smarty-> templates (string path.
After the program is executed, we can open the templates_c and cache directories under the current directory, and we will find that there are more % directories at the bottom. These directories are the compilation and cache directories of Smarty, it is automatically generated by the program and does not need to be modified directly.
I briefly introduced some common basic elements in the Smarty program. In the following example, you can see that they will be used multiple times.

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.