smarty半小时快速上手教程
smarty的程序设计部分:
在smarty的模板设计部分我简单的把smarty在模板中的一些常用设置做了简单的介绍,这一节主要来介绍一下如何在smarty中开始我们程
序设计。
PHP代码:--------------------------------------------------------------------------------
首先来介绍一下在上一节中我们使用的过的.php文件中的一些元素。同样,我们拿上一节中最开始的那个index.php文件来说明一下:
================================================
index.php
================================================
/*********************************************
*
* 文件名: index.php
* 作 用: 显示实例程序
*
*********************************************/
include_once("./comm/Smarty.class.php"); //包含smarty类文件
$smarty = new Smarty(); //建立smarty实例对象$smarty
$smarty->templates("./templates"); //设置模板目录
$smarty->templates_c("./templates_c"); //设置编译目录
//****大家注意,这里我是我新加入的****//
$smarty->cache("./cache"); //设置缓存目录
$smarty->cache_lifetime = 60 * 60 * 24; //设置缓存时间
$smarty->caching = true; //设置缓存方式
//----------------------------------------------------
//左右边界符,默认为{},但实际应用当中容易与JavaScript
//相冲突,所以建议设成或其它。
//----------------------------------------------------
$smarty->left_delimiter = "
$smarty->right_delimiter = "}>";
$smarty->assign("name", "李晓军"); //进行模板变量替换
//编译并显示位于./templates下的index.tpl模板
$smarty->display("index.tpl");
?>
我们可以看到,smarty的程序部分实际就是符合php语言规范的一组代码,我们依次来解释一下:
1。/**/语句:
包含的部分为程序篇头注释。主要的内容应该为对程序的作用,版权与作者及编写时间做一个简单的介绍,这在smarty中不是必
需的,但从程序的风格来讲,这是一个好的风格。
2。include_once语句:
它将安装到网站的smarty文件包含到当前文件中,注意包含的路径一定要写正确。
3。$smarty = new Smarty():
这一句新建一个Smarty对象$smarty,简单的一个对象的实例化。
4。$smarty->templates(""):
这一句指明$smarty对象使用tpl模板时的路径,它是一个目录,在没有这一句时,Smarty默认的模板路径为当前目录的templates
目录,实际在写程序时,我们要将这一句写明,这也是一种好的程序风格。
5。$smarty->templates_c(""):
这一句指明$smarty对象进行编译时的目录。在模板设计篇我们已经知道Smarty是一种编译型模板语言,而这个目录,就是它编译
模板的目录,这里要注意,如果站点位于*nix服务器上,请确保teamplates_c里定义的这个目录具有可写可读权限,默认情况下它的编译目录
是当前目录下的templates_c,出于同样的理由我们将其明确的写出来。
6。$smarty->left_delimiter与$smarty->right_delimiter:
指明在查找模板变量时的左右分割符。默认情况下为"{"与"}",但在实际中因为我们要在模板中使用<script>,Script中的函数定</script>
义难免会使用{},虽然它有自己的解决办法,但习惯上我们将它重新定义为""或是""或其它标志符,注意,如果在这里
定义了左右分割符后,在模板文件中相应的要使每一个变量使用与定义相同的符号,例如在这里指定为"",tpl模板中也要相应的将
{$name}变成,这样程序才可以正确的找到模板变量。
7。$smarty->cache("./cache"):
告诉Smarty输出的模板文件缓存的位置。上一篇我们知道Smarty最大的优点在于它可以缓存,这里就是设置缓存的目录。默认情
况下为当前目录下的cache目录,与templates_c目录相当,在*nix系统中我们要确保它的可读可写性。
8. $smarty->cache_lifetime = 60 * 60 * 24:
这里将以秒为单位进行计算缓存有效的时间。第一次缓存时间到期时当Smarty的caching变量设置为true时缓存将被重建。当它的
取值为-1时表示建立起的缓存从不过期,为0时表示在程序每次执行时缓存总是被重新建立。上面的设置表示将cache_lifetime设置为一天。
9. $smarty->caching = 1:
这个属性告诉Smarty是否要进行缓存以及缓存的方式。它可以取3个值,0:Smarty默认值,表示不对模板进行缓存;1:表示
Smarty将使用当前定义的cache_lifetime来决定是否结束cache;2:表示Smarty将使用在cache被建立时使用cache_lifetime这个值。习惯上使
用true与false来表示是否进行缓存。
10. $smarty->assign("name", "李晓军"):
该数的原型为assign(string varname, mixed var),varname为模板中使用的模板变量,var指出要将模板变量替换的变量名;其
第二种原形为assign(mixed var),我们要在后面的例子详细的讲解这个成员函数的使用方法,assign是Smarty的核心函数之一,所有对模板变
量的替换都要使用它。
11. $smarty->display("index.tpl"):
该函数原形为display(string varname),作用为显示一个模板。简单的讲,它将分析处理过的模板显示出来,这里的模板文件不
用加路径,只要使用一个文件名就可以了,它路径我们已经在$smarty->templates(string path)中定义过了。
程序执行完后我们可以打开当前目录下的templates_c与cache目录,就会发现在下边多出一些%%的目录,这些目录就是Smarty的编译与
缓存目录,它由程序自动生成,不要直接对这些生成的文件进行修改。
以上我简单的把Smarty程序中的一些常用的基本元素介绍了一下,在后边的例子中你可以看到将它们将被多次的使用。
接下来介绍一个section循环块与foreach循环块,本来它应该属于模板部分,但是由于它们是smarty的精华所在,而且与smarty程序设计
部分联系非常紧密,所以就在本节单独拿出来讲一下。
1. foreach:用于循环简单数组,它是一个选择性的section循环,它的定义格式为:
{foreach from=$array item=array_id}
{foreachelse}
{/foreach}
其中,from 指出要循环的数组变量,item为要循环的变量名称,循环次数由from所指定的数组变量的个数所决定。{foreachelse}用来当
程序中传递过来的数组为空时的处理,下面是一个简单的例子:
===========================================
example6.tpl
===========================================
这里将输出一个数组:
{foreach from=$newsArray item=newsID}
新闻编号:{$newsID}
新闻内容:{$newsTitle}
{foreachelse}
对不起,数据库中没有新闻输出!
{/foreach}
==========================================
example6.php
==========================================
/*********************************************
*
* 文件名: example6.php
* 作 用: 显示实例程序2
*********************************************/
include_once("./comm/Smarty.class.php");
$smarty = new Smarty();
$smarty->templates("./templates");
$smarty->templates_c("./templates_c");
$smarty->cache("./cache");
$smarty->cache_lifetime = 0;
$smarty->caching = true;
$smarty->left_delimiter = "{";
$smarty->right_delimiter = "}";
$array[] = array("newsID"=>1, "newsTitle"=>"第1条新闻");
$array[] = array("newsID"=>2, "newsTitle"=>"第2条新闻");
$array[] = array("newsID"=>3, "newsTitle"=>"第3条新闻");
$array[] = array("newsID"=>4, "newsTitle"=>"第4条新闻");
$array[] = array("newsID"=>5, "newsTitle"=>"第5条新闻");
$array[] = array("newsID"=>6, "newsTitle"=>"第6条新闻");
$smarty->assign("newsArray", $array);
//编译并显示位于./templates下的index.tpl模板
$smarty->display("example6.tpl"

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



According to news on April 17, HMD teamed up with the well-known beer brand Heineken and the creative company Bodega to launch a unique flip phone - The Boring Phone. This phone is not only full of innovation in design, but also returns to nature in terms of functionality, aiming to lead people back to real interpersonal interactions and enjoy the pure time of drinking with friends. Boring mobile phone adopts a unique transparent flip design, showing a simple yet elegant aesthetic. It is equipped with a 2.8-inch QVGA display inside and a 1.77-inch display outside, providing users with a basic visual interaction experience. In terms of photography, although it is only equipped with a 30-megapixel camera, it is enough to handle simple daily tasks.

According to news on April 26, ZTE’s 5G portable Wi-Fi U50S is now officially on sale, starting at 899 yuan. In terms of appearance design, ZTE U50S Portable Wi-Fi is simple and stylish, easy to hold and pack. Its size is 159/73/18mm and is easy to carry, allowing you to enjoy 5G high-speed network anytime and anywhere, achieving an unimpeded mobile office and entertainment experience. ZTE 5G portable Wi-Fi U50S supports the advanced Wi-Fi 6 protocol with a peak rate of up to 1800Mbps. It relies on the Snapdragon X55 high-performance 5G platform to provide users with an extremely fast network experience. Not only does it support the 5G dual-mode SA+NSA network environment and Sub-6GHz frequency band, the measured network speed can even reach an astonishing 500Mbps, which is easily satisfactory.

After rain in summer, you can often see a beautiful and magical special weather scene - rainbow. This is also a rare scene that can be encountered in photography, and it is very photogenic. There are several conditions for a rainbow to appear: first, there are enough water droplets in the air, and second, the sun shines at a low angle. Therefore, it is easiest to see a rainbow in the afternoon after the rain has cleared up. However, the formation of a rainbow is greatly affected by weather, light and other conditions, so it generally only lasts for a short period of time, and the best viewing and shooting time is even shorter. So when you encounter a rainbow, how can you properly record it and photograph it with quality? 1. Look for rainbows. In addition to the conditions mentioned above, rainbows usually appear in the direction of sunlight, that is, if the sun shines from west to east, rainbows are more likely to appear in the east.

According to news on July 12, the Honor Magic V3 series was officially released today, equipped with the new Honor Vision Soothing Oasis eye protection screen. While the screen itself has high specifications and high quality, it also pioneered the introduction of AI active eye protection technology. It is reported that the traditional way to alleviate myopia is "myopia glasses". The power of myopia glasses is evenly distributed to ensure that the central area of sight is imaged on the retina, but the peripheral area is imaged behind the retina. The retina senses that the image is behind, promoting the eye axis direction. grow later, thereby deepening the degree. At present, one of the main ways to alleviate the development of myopia is the "defocus lens". The central area has a normal power, and the peripheral area is adjusted through optical design partitions, so that the image in the peripheral area falls in front of the retina.

According to news on July 29, the Honor X60i mobile phone is officially on sale today, starting at 1,399 yuan. In terms of design, the Honor X60i mobile phone adopts a straight screen design with a hole in the center and almost unbounded ultra-narrow borders on all four sides, which greatly broadens the field of view. Honor X60i parameters Display: 6.7-inch high-definition display Battery: 5000mAh large-capacity battery Processor: Dimensity 6080 processor (TSMC 6nm, 2x2.4G A76+6×2G A55) System: MagicOS8.0 system Other features: 5G signal enhancement, smart capsule, under-screen fingerprint, dual MIC, noise reduction, knowledge Q&A, photography capabilities: rear dual camera system: 50 million pixels main camera, 2 million pixels auxiliary lens, front selfie lens: 8 million pixels, price: 8GB

According to news on May 13, vivoX100s was officially released tonight. In addition to excellent images, the new phone also performs very well in terms of signal. According to vivo’s official introduction, vivoX100s uses an innovative universal signal amplification system, which is equipped with up to 21 antennas. This design has been re-optimized based on the direct screen to balance many signal requirements such as 5G, 4G, Wi-Fi, GPS, and NFC. This makes vivoX100s the mobile phone with the strongest signal reception capability in vivo’s history. The new phone also uses a unique 360° surround design, with antennas distributed around the body. This design not only enhances the signal strength, but also optimizes various daily holding postures to avoid problems caused by improper holding methods.

According to news on July 19, Xiaomi MIX Fold 4, the first flagship folding new phone, was officially released tonight and is equipped with a "three-dimensional special-shaped battery" for the first time. According to reports, Xiaomi MIX Fold4 has achieved a major breakthrough in battery technology and designed an innovative "three-dimensional special-shaped battery" specifically for folding screens. Traditional folding screen devices mostly use conventional square batteries, which have low space utilization efficiency. In order to solve this problem, Xiaomi did not use the common winding battery cells, but developed a new lamination process to create a new form of battery, which greatly improved the space utilization. Battery Technology Innovation In order to accurately alternately stack positive and negative electrode sheets and ensure the safe embedding of lithium ions, Xiaomi has developed a new ultrasonic welding machine and lamination machine to improve welding and cutting accuracy.

C++ template specializations affect function overloading and rewriting: Function overloading: Specialized versions can provide different implementations of a specific type, thus affecting the functions the compiler chooses to call. Function overriding: The specialized version in the derived class will override the template function in the base class, affecting the behavior of the derived class object when calling the function.
