(Correct version) smarty half-hour quick tutorial_PHP tutorial
1: Smarty programming part:
In the smarty template design section, I briefly introduced some common settings of smarty in the template. This section mainly introduces how to start our program design in smarty. Download the Smarty file and add it to your site.
index.php PHP code:
/**
*
* @version $Id: index.php
* @package
* @author www.2cto.com
* @action Show example program
*/
include_once("./Smarty/Smarty.class.php"); //Include smarty class files
$smarty = new Smarty(); //Create smarty instance object $smarty
$smarty->templates("./templates"); //Set template directory
$smarty->templates_c("./templates_c"); //Set the compilation directory
$smarty->cache("./cache"); //Cache directory
$smarty->cache_lifetime = 0; //Cache time
$smarty->caching = true; //Caching method
$smarty->left_delimiter = "{#";
$smarty->right_delimiter = "#}";
$smarty->assign("name", "zaocha"); //Replace template variables
$smarty->display("index.htm"); //Compile and display the index.htm template located under ./templates
?>
Two: 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 them in turn:
1:/**/Statement:
The included part is the program header comments. 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 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 of the $smarty object when using the tpl template. It is a directory. Without this sentence, Smarty's default template path is the templates directory of the current directory. When actually writing the 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 make sure
The directory defined in teamplates_c has 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: $smarty->left_delimiter and $smarty->right_delimiter:
Specifies the left and right separators when looking for template variables. By default, it is "{" and "}", but in practice, because we need to use <script> in the template, the function definition in Script will inevitably use {}. Although it has its own solution, it is customary Let’s redefine it </script>
is "{#" and "#}" or "" or other identifiers. Note that if the left and right separators are defined here, in the template Correspondingly, each variable in the file must use the same symbol as the definition, for example, designated as "" here, and
in the htm template.Correspondingly, change {$name} to so that the program can correctly find the template variable.
7: $smarty->cache("./cache"):
Tell Smarty where to cache the output template files. In the previous article, we knew that the biggest advantage of Smarty is that it can be cached. Here is the directory where the cache is set. By default, it is the cache directory under the current directory, which is equivalent to the templates_c directory. In Linux systems
We want to make sure it is readable and writable.
8: $smarty->cache_lifetime = 60 * 60 * 24:
The cache validity time will be calculated in seconds. The cache will be rebuilt when the Smarty cache variable is set to true when the first cache time expires. When its value is -1, it means that the established cache never expires; when it is 0, it means that the cache is cached every time the program is executed
The save is always re-created. The above setting means setting cache_lifetime to one 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 will not be cached; 1: indicating that Smarty will use the currently defined cache_lifetime to decide whether to end the cache; 2: indicating
Smarty will use the cache_lifetime value when the cache is created. It is customary to use true and false to indicate whether to cache.
10:$smarty->assign("name", "zaocha"):
The prototype of this number is assign(string varname, mixed var), varname is the template variable used in the template, and var points out the variable name to be replaced by the template variable; its second prototype is assign(mixed var), which we will describe later. The example explains in detail how to use this member function. assign is one of the core functions of Smarty. It must be used for all substitutions of template variables.
11. $smarty->display("index.tpl"):
The prototype of this function is display(string varname), which is used to display a template. To put it simply, it will display the analyzed and processed templates. There is no need to add a path to the template file here, just use a file name. We have already defined its path in $smarty->templates(string path) .
After the program is executed, we can open the templates_c and cache directories in the current directory, and we will find that there are some more %% directories below. These directories are Smarty's compilation and cache directories. They are automatically generated by the program. Do not generate these directly. file to modify.
Above I briefly introduced some commonly used basic elements in Smarty programs. In the following examples, you can see that they will be used multiple times.
Three: Template description
Next, we will introduce a section loop block and a foreach loop block. Originally they should belong to the template part, but since they are the essence of smarty and are very closely related to the smarty programming part, they will be discussed separately in this section. one time.
1: foreach: used to loop simple arrays. It is a selective section loop. Its definition format is:
{foreach from=$array item=array_id}
{foreachelse}
{/foreach}
Among them, from indicates the array variable to be looped, item is the name of the variable to be looped, and the number of loops is determined by the number of array variables specified by from. {foreachelse} is used to process when the array passed in the program is empty. Here is a simple example:
Template file: example.htm
Foreach outputs the data of a "two-dimensional associative array":
{#foreach item=new from=$news#}
News number: {#$new.id#}
News content: {#$new.title#}
-------------------------------------------------- ----------------------------------
{#foreachelse#}
There is no news output in the database!
{#/foreach#}
-------------------------------------------------- ----------------------------------
Note: The writing on bkJia is as follows: (2cto.com link: http://www.BkJia.com/html/webkaifa/PHP/PHPyingyong/2009/0429/2897.html)
{foreach from=$newsArray item=newsID}
News ID: {$newsID}
News content: {$newsTitle}
-------------------------------------------------- ----------------------------------
{foreachelse}
Sorry, there is no news output in the database!
{/foreach}
This was an error in not displaying data, and this article has been corrected. It runs correctly as shown below:

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


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

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

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

SublimeText3 Linux new version
SublimeText3 Linux latest version

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

SublimeText3 English version
Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.