Table of Contents
php—Smarty-1 (18),phpsmarty-118
Home Backend Development PHP Tutorial php—Smarty-1 (18),phpsmarty-118_PHP教程

php—Smarty-1 (18),phpsmarty-118_PHP教程

Jul 13, 2016 am 09:54 AM
smarty

php—Smarty-1 (18),phpsmarty-118

smarty

一、模板原理

1、  为什么使用模板

1)  场景:

for($i=0;$i

       echo ‘

’.$row[‘proname’].’

}

以上这种写法将html与php进行混编,不利于开发和维护

2)  场景:

我们现在开发了一个网站,经过一段时间的运营,客户对前台不满意,我们又开发一套界面,但运行之后,客户认为之前的样式比较好,经常更换网站样式

解决以上两个问题的最好办法,就是使用模板

2、  模板的具体实现

模板的实现其实就是字符串替换

1)  例1

模板文件:

 

PHP文件:

1)  例2:

Template类:

PHP代码:

 

上面的类文件中的代码,不够优化,我们可以使用比较流行的模板框架来实现自己的项目

二、Smarty简介

1、  什么是Smarty

Smarty是一个基于PHP开发的PHP模板引擎。它提供了逻辑与外在内容的分离。

2、  Smarty的特点

l  速度

l  编译型

l  缓存技术

l  插件技术

l  语句自由 if/elseif/else/endif

l  实时更新 :实时性要求比较高

3、  配置

l  http://smarty.php.net

l  /lib

l  Smarty 2.6       php4.0

l  Smarty 3.0       php5.0

 1 解压smarty,进入解压目录

Demo:案例

Libs:Smarty源代码

1)  进入libs目录,

Plugins:自定义插件目录

Sysplugins:系统插件目录

Debug.tpl :在Smarty中,有调试功能,可以观察变量的值 ,调试窗口模板

Smarty.class.php:Smarty核心类文件(入口文件)

SmartyBC.class.php :Smarty3.0在2.0的基础上进行了升级,如果想使用2.0的功能,可以引用这个入口文件

3)将libs目录复制到项目中,并重命名

三、 快速入门

1、  代码实现:

模板代码:

{$name}、{$age} :模板标记变量

程序代码:

2、  smarty工作原理:

l  smarty将php源文件,首先编译成中间文件

l  如果启用缓存,再根据编译文件生成缓存文件

l  之后的每次访问都会访问编译文件

l  如果启用缓存且有缓存文件而且没有过期,则直接访问缓存文件

Smarty第一次运行某个程序时,会将这个程序生成一个编译文件

以后,每次运行这个程序时,只要模板没有改变,就直接运行这个编译文件,如果模板改变了,就重新生成编译文件,再去运行。

编译是指针对模板的编译

3、  原理图:

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/993275.htmlTechArticlephp—Smarty-1 (18),phpsmarty-118 smarty 一、模板原理 1、 为什么使用模板 1) 场景: for($i=0;$i$count;$i++){ echo td.$row[proname]./td } 以上这种写法将...
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 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)

A preliminary study on the template engine Smarty in PHP A preliminary study on the template engine Smarty in PHP May 11, 2023 pm 05:15 PM

Nowadays, website development is inseparable from an important component-template engine. A template engine refers to a tool that combines page templates and data to generate HTML code with a specific format. In various website development frameworks, the template engine is an essential component, because the template engine can greatly reduce the duplication of code and improve the dynamics of the page. One of the most common and popular template engines is Smarty. Smarty is a DSL (DomainSpecif

How to use PHP and Smarty to achieve front-end and back-end separation development How to use PHP and Smarty to achieve front-end and back-end separation development Jun 25, 2023 pm 01:46 PM

In modern web development, the separation of front-end and back-end has become a very popular trend, which allows developers to better organize projects and improve the efficiency of project development. PHP and Smarty are two very commonly used technologies, which can be used to achieve front-end and back-end separation development. This article will introduce how to use PHP and Smarty to achieve front-end and back-end separation development. What is front-end and back-end separation development? In traditional web development, the front-end is mainly responsible for the presentation of the page and the logic of interaction with the back-end. The backend is mainly responsible for the business

How to use Smarty template engine in PHP development How to use Smarty template engine in PHP development Jun 27, 2023 pm 01:28 PM

As a PHP developer, using a template engine is a natural choice. Smarty is a popular template engine that provides a way to separate HTML/CSS/JavaScript from PHP code, allowing developers to better organize and manage projects. In this article, we will learn how to use Smarty template engine during PHP development. 1. Install Smarty Before, we must install Smarty. In this article we will use Composer to install

How to use PHP and the Smarty template engine How to use PHP and the Smarty template engine May 11, 2023 pm 03:33 PM

PHP is a powerful server-side scripting language that can be used to develop web applications. In the early days of web development, programmers used a lot of HTML and JavaScript code to develop web applications. However, this approach is difficult to maintain and manage because the HTML and JavaScript code can become very complex. To solve this problem, the Smarty template engine was created. Smarty is a template engine developed based on PHP for managing and generating W

Application of template engine Smarty in PHP development Application of template engine Smarty in PHP development Jun 14, 2023 pm 02:02 PM

With the rapid development and upgrading of the Internet, PHP, as a programming language widely used in Web application development, has gradually become a very popular programming language in the industry. However, in the PHP development process, traditional code implementation methods often lead to a decrease in code readability and maintainability. At this time, an efficient template engine becomes one of the solutions. Among many template engines, Smarty has become a popular choice among PHP developers due to its powerful functions and good performance. This article will detail

How to use Smarty with CakePHP? How to use Smarty with CakePHP? Jun 03, 2023 pm 03:10 PM

CakePHP is an open source PHP framework that provides rich features and tools to accelerate web application development. One of the powerful features is the template engine. By default, CakePHP uses PHP's native syntax for view rendering. However, sometimes we may want to use another template engine, such as Smarty. This article will introduce how to use Smarty in CakePHP. 1. What is Smarty? Smarty is a template-based PHP framework that

what are thinkphp and smarty what are thinkphp and smarty Jun 14, 2022 pm 05:56 PM

thinkphp is an open source lightweight PHP framework that is used to simplify enterprise application development and agile WEB application development; using ThinkPHP, developers can develop and deploy applications more conveniently and quickly. Smarty is a PHP template engine that can better help developers separate program logic and page display (separation of business logic and display logic), so that programmers can change the logic content of the program without affecting the page design of the front-end staff, and the front-end staff can re- Modifying the page will not affect the program logic of the program.

How to use Smarty SSTi How to use Smarty SSTi May 15, 2023 am 11:37 AM

Title description: The title provides an API for reading XFF headers. There is the word BuildWithSmarty at the bottom of the page. It can be determined that it is written with the Smarty engine. Basically, it can be determined that the page has the possibility of SSTi. Change the xff header from 127.0.0.1 to The following result appears on 127.0.0{1+2} ssti. There is no doubt that the final payload is of

See all articles