smarty半小时快速上手入门教程
这篇文章主要介绍了smarty半小时快速上手入门教程,以实例的形式分析了smarty在实际使用过程中的属性、方法及具体用法,需要的朋友可以参考下
本文讲述了smarty快速上手入门的方法,可以让读者在半小时内快速掌握smarty的用法。分享给大家供大家参考。具体实现方法如下:
一、smarty的程序设计部分:
在smarty的模板设计部分我简单的把smarty在模板中的一些常用设置做了简单的介绍,这一节主要来介绍一下如何在smarty中开始我们程序设计。下载Smarty文件放到你们站点中。
index.php代码如下:
复制代码 代码如下:
/**
*
* @version $Id: index.php
* @package
* @author
* @action 显示实例程序
*/
include_once("./Smarty/Smarty.class.php"); //包含smarty类文件
$smarty = new Smarty(); //建立smarty实例对象$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 = "#}";
$smarty->assign("name", "zaocha"); //进行模板变量替换
$smarty->display("index.htm"); //编译并显示位于./templates下的index.htm模板
?>
二、解释smarty的程序
我们可以看到,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是一种编译型模板语言,而这个目录,就是它编译模板的目录,要注意,如果站点位于linux服务器上,请确保
teamplates_c里定义的这个目录具有可写可读权限,默认情况下它的编译目录是当前目录下的templates_c,出于同样的理由我们将其明确的写出来。
6:$smarty->left_delimiter与$smarty->right_delimiter:
指明在查找模板变量时的左右分割符。默认情况下为"{"与"}",但在实际中因为我们要在模板中使用<script>,Script中的函数定义难免会使用{},虽然它有自己的解决办法,但习惯上我们将它重新定义</script>
为"{#"与"#}"或是""或其它标志符,注意,如果在这里定义了左右分割符后,在模板文件中相应的要使每一个变量使用与定义相同的符号,例如在这里指定为"",htm模板中也要
相应的将{$name}变成,这样程序才可以正确的找到模板变量。
7:$smarty->cache("./cache"):
告诉Smarty输出的模板文件缓存的位置。上一篇我们知道Smarty最大的优点在于它可以缓存,这里就是设置缓存的目录。默认情况下为当前目录下的cache目录,与templates_c目录相当,在linux系统中
我们要确保它的可读可写性。
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", "zaocha"):
该数的原型为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}用来当程序中传递过来的数组为空时的处理,下面是一个简单的例子:
模板文件:
example.htm页面如下:
复制代码 代码如下:
foreach 输出一个“二维关联数组”的数据:
{#foreach item=new from=$news#}
新闻编号:{#$new.id#}
新闻内容:{#$new.title#}
{#foreachelse#}
数据库中没有新闻输出!
{#/foreach#}
{foreach from=$newsArray item=newsID}
新闻编号:{$newsID}
新闻内容:{$newsTitle}
{foreachelse}
对不起,数据库中没有新闻输出!
{/foreach}
这是一个错误的不显示数据,本文做了更正。
程序文件:example.php如下:
复制代码 代码如下:

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

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

Diffusion can not only imitate better, but also "create". The diffusion model (DiffusionModel) is an image generation model. Compared with the well-known algorithms such as GAN and VAE in the field of AI, the diffusion model takes a different approach. Its main idea is a process of first adding noise to the image and then gradually denoising it. How to denoise and restore the original image is the core part of the algorithm. The final algorithm is able to generate an image from a random noisy image. In recent years, the phenomenal growth of generative AI has enabled many exciting applications in text-to-image generation, video generation, and more. The basic principle behind these generative tools is the concept of diffusion, a special sampling mechanism that overcomes the limitations of previous methods.

Kimi: In just one sentence, in just ten seconds, a PPT will be ready. PPT is so annoying! To hold a meeting, you need to have a PPT; to write a weekly report, you need to have a PPT; to make an investment, you need to show a PPT; even when you accuse someone of cheating, you have to send a PPT. College is more like studying a PPT major. You watch PPT in class and do PPT after class. Perhaps, when Dennis Austin invented PPT 37 years ago, he did not expect that one day PPT would become so widespread. Talking about our hard experience of making PPT brings tears to our eyes. "It took three months to make a PPT of more than 20 pages, and I revised it dozens of times. I felt like vomiting when I saw the PPT." "At my peak, I did five PPTs a day, and even my breathing was PPT." If you have an impromptu meeting, you should do it

In the early morning of June 20th, Beijing time, CVPR2024, the top international computer vision conference held in Seattle, officially announced the best paper and other awards. This year, a total of 10 papers won awards, including 2 best papers and 2 best student papers. In addition, there were 2 best paper nominations and 4 best student paper nominations. The top conference in the field of computer vision (CV) is CVPR, which attracts a large number of research institutions and universities every year. According to statistics, a total of 11,532 papers were submitted this year, and 2,719 were accepted, with an acceptance rate of 23.6%. According to Georgia Institute of Technology’s statistical analysis of CVPR2024 data, from the perspective of research topics, the largest number of papers is image and video synthesis and generation (Imageandvideosyn

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.

We know that LLM is trained on large-scale computer clusters using massive data. This site has introduced many methods and technologies used to assist and improve the LLM training process. Today, what we want to share is an article that goes deep into the underlying technology and introduces how to turn a bunch of "bare metals" without even an operating system into a computer cluster for training LLM. This article comes from Imbue, an AI startup that strives to achieve general intelligence by understanding how machines think. Of course, turning a bunch of "bare metal" without an operating system into a computer cluster for training LLM is not an easy process, full of exploration and trial and error, but Imbue finally successfully trained an LLM with 70 billion parameters. and in the process accumulate

Editor of the Machine Power Report: Yang Wen The wave of artificial intelligence represented by large models and AIGC has been quietly changing the way we live and work, but most people still don’t know how to use it. Therefore, we have launched the "AI in Use" column to introduce in detail how to use AI through intuitive, interesting and concise artificial intelligence use cases and stimulate everyone's thinking. We also welcome readers to submit innovative, hands-on use cases. Video link: https://mp.weixin.qq.com/s/2hX_i7li3RqdE4u016yGhQ Recently, the life vlog of a girl living alone became popular on Xiaohongshu. An illustration-style animation, coupled with a few healing words, can be easily picked up in just a few days.

1. First open WeChat. 2. Click [+] in the upper right corner. 3. Click the QR code to collect payment. 4. Click the three small dots in the upper right corner. 5. Click to close the voice reminder for payment arrival.

Retrieval-augmented generation (RAG) is a technique that uses retrieval to boost language models. Specifically, before a language model generates an answer, it retrieves relevant information from an extensive document database and then uses this information to guide the generation process. This technology can greatly improve the accuracy and relevance of content, effectively alleviate the problem of hallucinations, increase the speed of knowledge update, and enhance the traceability of content generation. RAG is undoubtedly one of the most exciting areas of artificial intelligence research. For more details about RAG, please refer to the column article on this site "What are the new developments in RAG, which specializes in making up for the shortcomings of large models?" This review explains it clearly." But RAG is not perfect, and users often encounter some "pain points" when using it. Recently, NVIDIA’s advanced generative AI solution
