Home Backend Development PHP Tutorial 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

Jul 21, 2016 pm 03:11 PM
mvc php smarty web one download Instructions for use based on Install develop frame Demo of Concise 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:
 
 
 
 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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1266
29
C# Tutorial
1239
24
Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

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,

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

Explain the match expression (PHP 8 ) and how it differs from switch. Explain the match expression (PHP 8 ) and how it differs from switch. Apr 06, 2025 am 12:03 AM

In PHP8, match expressions are a new control structure that returns different results based on the value of the expression. 1) It is similar to a switch statement, but returns a value instead of an execution statement block. 2) The match expression is strictly compared (===), which improves security. 3) It avoids possible break omissions in switch statements and enhances the simplicity and readability of the code.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

See all articles