php中Smarty模板初体验
下面介绍一下Smarty模板引擎的特性:
1. 速度:采用Smarty编写的程序可以获得最大速度的提高,这一点是相对于其它的模板引擎技术而言的。
2. 编译型:采用Smarty编写的程序在运行时要编译成一个非模板技术的PHP文件,这个文件采用了PHP与HTML混合的方式,在下一次访问模板时将WEB请求直接转换到这个文件中,而不再进行模板重新编译(在源程序没有改动的情况下)
3. 缓存技术:Smarty选用的一种缓存技术,它可以将用户最终看到的HTML文件缓存成一个静态的HTML页,当设定Smarty的cache属性为true时,在Smarty设定的cachetime期内将用户的WEB请求直接转换到这个静态的HTML文件中来,这相当于调用一个静态的HTML文件。
4. 插件技术:Smarty可以自定义插件。插件实际就是一些自定义的函数。
5. 模板中可以使用if/elseif/else/endif。在模板文件使用判断语句可以非常方便的对模板进行格式重排。
使用Smarty模板版本Smarty-3.0.8,解压后文件目录如下:
于是开始了我的Smarty之旅喽——
Step 1
在服务器网页文件夹中新建一个smartytest文件夹,只取libs目录中的文件,复制到smartytest文件夹下,更名为smarty
Step 2
在test中新建目录templates,并在该目录下新建四个文件夹cache、configs、templates、templates_c,建成的文件夹形式如下图
Step 3
写一个配置文件,通过它可以实现与Smarty的连接,而且把它写成单独的文件可以在写不同页面时重复写相同的代码(当然也可以把它写成类形式,便于自定义),这里我把它文件名定为config.php
复制代码 代码如下:
//获取当前文件夹所在的绝对路径 H:\wamp\www\smartytest\
define('SMARTY_PATH',substr(dirname(__FILE__),0,-9));
//获取templates文件夹的绝对路径 H:\wamp\www\smartytest\templates
define('TEMPLATES_PATH',SMARTY_PATH.'templates/');
require SMARTY_PATH.'smarty/Smarty.class.php';
$smarty = new Smarty;
//定义目录路径
$smarty->template_dir = TEMPLATES_PATH.'templates/';
$smarty->complile_dir = TEMPLATES_PATH.'templates_c/';
$smarty->config_dir = TEMPLATES_PATH.'configs/';
$smarty->cache_dir = TEMPLATES_PATH.'cache/';
//定义左右结束符 {% 和 %}
$smarty->left_delimiter = '{%';
$smarty->right_delimiter = '%}';
//关闭缓存
$smarty->caching = false;
//关闭调试
$smarty->debugging = false;
?>
Step 4
写一个简单的模板文件命名为index.tpl,放到templates\templates目录下
复制代码 代码如下:
{%$hello%}
Step 5
写一个PHP文件,命名为index.php,放在templates文件目录下
复制代码 代码如下:
require 'config.php';
$smarty->assign('hello','Hello Word');
$smarty->display('index.tpl');
?>
现在文件目录为
Step 6
测试文件:
总结:使用Smarty模板过程中也遇见了问题,比如Smarty的目录可以自己设置,自定义性较强,因此对Smarty的教程有很多版本(我在书上看到一个版本,百度百科也有另一种),结果两个都参考就不明白怎么放了,最后还是使用百度百科的那种(也就是上文所说);其次在百度百科中的代码是复制过来的,结果在使用时出现了syntax error, unexpected T_VARIABLE错误,看着代码都很对啊,就是出错,最后查找原因,原来是网页中的全角空格所致,所以在网页上复制的代码的莫名错误最好的办法就是把空格都去了重新写;注意在写PHP文件时hello没有前边的$符号,而在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

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

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

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,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
