Home php教程 php手册 PHP模板之Smarty教程

PHP模板之Smarty教程

Jun 13, 2016 am 11:13 AM
php smarty Open source engine us supply Tutorial template of Know Community language

我们知道PHP语言作为开源社区的一员,提供了各种模板引擎,如FastTemplate,Smarty,SimpleTemplate等,而Smarty是现在使用得比较多的PHP模板引擎,今天和大家分享在PHP开发中如何安装与使用Smarty,也算是对Smarty的入门学习。

一、准备工作

1、选择安装Smarty的目录

如果拥有服务器权限,考虑到安全性可以选择将Smarty安装在WEB程序文档目录之外的地方,然后通过将Smarty安装目录地址包含在PHP.INI文件中的include_path选项。

如果是虚拟主机权限,或者好几个项目,可以将Smarty安装在各自的项目目录中,在require Smarty类文件,也可以使用Smarty模板引擎。当然为了安全考虑,你可以通过apache禁止相关目录访问。

另外这两种Smarty安装方式在移植性方面有所区别,第一种方式需要保证每台服务器有相同的Smarty配置,第二种方式对每台服务器配置没有影响。你可以根据各自的需要选择Smarty的安装方式。

2、下载Smarty,请点击这里下载Smarty,我选择的是Smarty-2.6.25

Smarty安装步骤

1、解压下载的Smarty-2.6.25压缩包

2、拷贝libs文件夹到WEB程序目录下,我的目录为testsmarty

Linux下的安装方法可以参考这里。

在安装完Smarty模板后,我们开始简单使用Smarty。

三、Smarty使用

1、创建相关目录

由于在使用Smarty的过程中,Smarty会生成编译的模板文件以及其他配置文件、缓存文件,我们需要创建相关的目录。我在testsmarty目录下,另外创建了tpls目录,并在tpls目录下创建templates、templates_c、configs、cache目录。为什么需要创建这些目录呢?打开Smarty.class.php文件,我们可以看到Smarty类定义了部分的成员属性。

$template_dir:设定所有模板文件都需要放置的目录地址。默认情况下,目录是:“./templates”,也就是在PHP执行程序同一个目录下寻找该模板目录。

$compile_dir:设定Smarty编译过的所有模板文件的存放目录地址。默认目录是:“./templates_c”,也就是在PHP执行程序同一个目录下寻找该编译目录。如果你在Linux服务器上创建这个目录,你还需要修改此目录的权限,使它有写的权限。

$config_dir:设定用于存放模板特殊配置文件的目录,默认目录是:“./configs”,也就是在PHP执行程序同一个目录下寻找该配置目录。

$cache_dir:在启动缓存特性的情况下,这个属性所指定的目录中放置Smarty缓存的所有模板。默认目录是:”./cache”,也就是在PHP执行程序同一个目录下寻找该缓存目录。你也可以用你自定义的缓存处理函数来控制缓存文件,它将会忽略这项设置。同样如果你在Linux服务器上创建这个目录,你还需要修改此目录的权限,使它有写的权限。

为了系统安全和移植性考虑,建议不要将这些目录建立在PHP执行程序同一目录下,可以将它建立在PHP执行程序目录之外,如果已建立在PHP执行程序同一目录下,可以通过Apache做好目录限制访问工作。

2、建立相关配置文件

我们需要创建一个配置文件,来覆盖Smarty类的默认成员属性,并命名为main.php,保存在smarty目录下,以后哪个脚本需要使用Smarty,我们只要把main.php包含进来即可。

<ol class="dp-c">
<li class="alt"><span><span>  </span></span></li>
<li>
<span class="keyword">include</span><span>(</span><span class="string">"./smarty/libs/Smarty.class.php"</span><span>);  </span>
</li>
<li class="alt">
<span>define(</span><span class="string">'SMARTY_ROOT'</span><span>, </span><span class="string">'./smarty/tpls'</span><span>);  </span>
</li>
<li>
<span class="vars">$tpl</span><span> = </span><span class="keyword">new</span><span> Smarty();  </span>
</li>
<li class="alt">
<span class="vars">$tpl</span><span>->template_dir = SMARTY_ROOT.</span><span class="string">"/templates/"</span><span>;  </span>
</li>
<li>
<span class="vars">$tpl</span><span>->compile_dir = SMARTY_ROOT.</span><span class="string">"/templates_c/"</span><span>;  </span>
</li>
<li class="alt">
<span class="vars">$tpl</span><span>->config_dir = SMARTY_ROOT.</span><span class="string">"/configs/"</span><span>;  </span>
</li>
<li>
<span class="vars">$tpl</span><span>->cache_dir = SMARTY_ROOT.</span><span class="string">"/cache/"</span><span>;  </span>
</li>
<li class="alt">
<span class="vars">$tpl</span><span>->caching=1;  </span>
</li>
<li>
<span class="vars">$tpl</span><span>->cache_lifetime=60*60*24;  </span>
</li>
<li class="alt">
<span class="vars">$tpl</span><span>->left_delimiter = </span><span class="string">'<span>;  </span></span>
</li>
<li>
<span class="vars">$tpl</span><span>->right_delimiter = </span><span class="string">'}>'</span><span>;  </span>
</li>
<li class="alt"><span>?> </span></li>
</ol>
Copy after login

注释:

第1-8行:主要定义一个smarty对象,同时设定模板文件、编译文件、缓存文件、配置文件的存放目录,覆盖Smarty.class.php中的默认值。

第9-10行:设定开启缓存,同时设定缓存的有效时间为1天。

知识点:$caching用来设置是否开启缓存功能。默认值设为0或无效。你也可以为同一个模板设有多个缓存,当值为1或2时启动缓存。1告诉Smarty使用当前的$cache_lifetime变量判断缓存是否过期。2告诉Smarty使用生成缓存时的cache_lifetime值。建议在项目开发过程中关闭缓存,将值设置为0

第11-12行:设置smarty语言的左右结束符,我们知道大括号是smarty的默认定界符,但在和javascript、css等结合时可能会产生冲突,所以这里我们设定为。

3、建立一个模板文件

一般情况下在美工页面设计完毕后,双方的交集点是模版文件,双方约定后,程序员不需要花太大的精力在前台,这就是使用Smarty模板引擎进行开发的好处。

我们首先建立一个简单的模版文件,名为leapsoul.tpl,你可在html文件中加入smarty变量后将文件另存为tpl类型的文件。

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><span class="tag-name">html</span><span class="tag">></span><span> </span></span></span></li>
<li><span class="tag"><span class="tag-name">head</span><span class="tag">></span><span> </span></span></li>
<li class="alt"><span class="tag"><span class="tag-name">meta</span><span> </span><span class="attribute">http-equiv</span><span>=</span><span class="attribute-value">"Content-type"</span><span> </span><span class="attribute">content</span><span>=</span><span class="attribute-value">"text/html; charset=gb2312"</span><span class="tag">></span><span> </span></span></li>
<li><span class="tag"><span class="tag-name">title</span><span class="tag">></span><span>   </span></span></li>
<li class="alt"><span class="tag"><span>{ $title }</span><span class="tag">></span><span> </span></span></li>
<li>
<span class="tag"></span><span class="tag-name">title</span><span class="tag">></span><span>   </span>
</li>
<li class="alt">
<span class="tag"></span><span class="tag-name">head</span><span class="tag">></span><span>   </span>
</li>
<li><span class="tag"><span class="tag-name">body</span><span class="tag">></span><span>   </span></span></li>
<li class="alt"><span class="tag"><span>{ $content }</span><span class="tag">></span><span>   </span></span></li>
<li>
<span class="tag"></span><span class="tag-name">body</span><span class="tag">></span><span>   </span>
</li>
<li class="alt">
<span class="tag"></span><span class="tag-name">html</span><span class="tag">></span><span> </span>
</li>
</ol>
Copy after login

注释:在这个tpl文件中设定了title和content两个smarty变量,文件保存为leapsoul.tpl,同时将其保存在testsmartytplstemplates模板文件目录下。

4、建立应用程序文件

模版文件类似于一个表现层,在建立完模板文件后,我们需要一个应用程序去驱动表现层,应用程序文件定义为smarty.php。

<ol class="dp-c">
<li class="alt"><span><span>  </span></span></li>
<li>
<span class="keyword">include</span><span>(</span><span class="string">"smarty/main.php"</span><span>);  </span>
</li>
<li class="alt">
<span class="vars">$tpl</span><span>->assign(</span><span class="string">"title"</span><span>, </span><span class="string">"leapsoul.cn为你展示smarty模板技术"</span><span>);  </span>
</li>
<li>
<span class="vars">$tpl</span><span>->assign(</span><span class="string">"content"</span><span>, </span><span class="string">"leapsoul.cn通过详细的安装使用步骤为你展示smarty模板技术"</span><span>);  </span>
</li>
<li class="alt">
<span class="vars">$tpl</span><span>->display(</span><span class="string">"leapsoul.tpl"</span><span>);  </span>
</li>
<li><span>?> </span></li>
</ol>
Copy after login

注释:

在这段代码中我们主要用到smarty中的两个函数assign和display,assign你可以理解为为变量赋值,display主要是用来将网页输出。更多smarty函数今后会详细介绍。

其他说明

由于我们开启了缓存功能,有兴趣的朋友可以打开cache和templates_c,cache目录存放了这个模板的缓存文件,文件开头部分有缓存信息,如文件的生成时间和过期时间等,其他的和一般的HTML文件没有多大的区别,而templates_c存放了模板经过编译后的PHP执行文件。

至此一个简单入门的Smarty模板应用实例就算介绍完成了。

原文地址:http://www.leapsoul.cn/?p=405


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

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 Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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
1666
14
PHP Tutorial
1273
29
C# Tutorial
1253
24
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: 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 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 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.

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.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

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.

See all articles