


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

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
