Table of Contents
smarty模板技术,smarty模板
Home php教程 php手册 smarty模板技术,smarty模板

smarty模板技术,smarty模板

Jun 13, 2016 am 08:46 AM
smarty

smarty模板技术,smarty模板

一、什么是smarty?
smarty是一个使用php写出来的模板php模板引擎,它提供了逻辑与外在内容的分离,简单的讲,目的就是要使用php程序员同美工分离,使用的程序员改变程序的逻辑内容不会影响到美工的页面设计,美工重新修改页面不会影响到程序的程序逻辑,这在多人合作的项目中显的尤为重要。

二、smarty优点:
1. 速度:采用smarty编写的程序可以获得最大速度的提高,这一点是相对于其它的模板引擎技术而言的。

2. 编译型:采用smarty编写的程序在运行时要编译成一个非模板技术的php文件,这个文件采用了phphtml混合的方式,在下一次访问模板时将web请求直接转换到这个文件中,而不再进行模板重新编译(在源程序没有改动的情况下)

3. 缓存技术:smarty选用的一种缓存技术,它可以将用户最终看到的html文件缓存成一个静态的html页,当设定smartycache属性为true时,在smarty设定的cachetime期内将用户的web请求直接转换到这个静态的html文件中来,这相当于调用一个静态的html文件。

4. 插件技术:smarty可以自定义插件。插件实际就是一些自定义的函数。

5. 模板中可以使用if/elseif/else/endif。在模板文件使用判断语句可以非常方便的对模板进行格式重排。

三、不适合使用smarty的地方:

1. 需要实时更新的内容。例如像股票显示,它需要经常对数据进行更新,这类型的程序使用smarty会使模板处理速度变慢。

2. 小项目。小项目因为项目简单而美工与程序员兼于一人的项目,使用smarty会丧失php开发迅速的优点。

四、smarty安装及配置:

下载smarty的安装包解压后,把libs文件夹放在网站根目录下,然后创建几个文件夹
templates       存放模板文件

templates_c    存放编译后的文件

configs           存放配置文件

cache             存放缓存文件
再创建初始化文件smarty.init.php

<span><?<span>php
    </span><span>include</span> "./libs/Smarty.class.php";<span>//</span><span>包含Smarty类库所在的文件</span>
    <span>$smarty</span>=<span>new</span> Smarty();<span>//</span><span>创建一个Smarty类的对象$smarty</span>
    <span>$smarty</span>->template_dir="./templates/";<span>//</span><span>设置所有模板文件存放目录</span>
    <span>$smarty</span>->compile_dir="./templates_c/";<span>//</span><span>设置所有编译过的模板文件存放目录</span>
    <span>$smarty</span>->config_dir="./configs/";<span>//</span><span>设置模板中特殊配置文件存放的目录</span>
    <span>$smarty</span>->cache_dir="./cache/";<span>//</span><span>设置存放Smarty缓存文件的目录</span>
    <span>$smarty</span>->caching=1;<span>//</span><span>设置开启Smarty缓存模板功能</span>
    <span>$smarty</span>->cache_lifetime=60*60*24;<span>//</span><span>设置模板缓存有效时间为1天</span>
    <span>$smarty</span>->left_delimiter='<{';<span>//</span><span>设置模板语言中的左结束符</span>
    <span>$smarty</span>->right_delimiter='}>';<span>//</span><span>设置模板语言中的右结束符</span>
?></span>
Copy after login

五、smarty引擎运行机制:

1、新建模板文件index.tpl及项目中所需的配置文件my.conf,配置文件也可后期补充

<span><span><</span><span>{config_load </span><span>file</span><span>="../configs/my.conf"</span><span>}</span><span>></span><span><!--</span><span> 加载配置文件 </span><span>--></span>
<span><</span><span>html</span><span>></span>
    <span><</span><span>head</span><span>></span>
        <span><</span><span>meta </span><span>charset</span><span>="utf-8"</span><span>></span>
        <span><</span><span>title</span><span>><</span><span>{$title</span><span>}</span><span>></</span><span>title</span><span>></span>
    <span></</span><span>head</span><span>></span>
    <span><</span><span>body </span><span>bgcolor</span><span>="<{#bgColor#}>"</span><span>></span>
        <span><</span><span>{$content</span><span>}</span><span>></span>
    <span></</span><span>body</span><span>></span>
<span></</span><span>html</span><span>></span></span>
Copy after login

2、新建程序入口文件index.php,引入控制器文件,传值并分配变量,并显示模板index.tpl

<span><?<span>php
    </span><span>//</span><span>引入smarty.init.php</span>
    <span>include</span> 'smarty.init.php'<span>;
    </span><span>$smarty</span>->assign("title","我的第一个文件标题"<span>);
    </span><span>$smarty</span>->assign("content","我的第一个文件内容"<span>);
    </span><span>$smarty</span>->display("index.tpl"<span>);
</span>?></span>
Copy after login

3、运行index.php,通过smarty控制器文件会生成一个php格式的编译文件,当缓存机制未开启时,浏览器会读取这个编译文件并最终显示出来。当开启缓存机制时,smarty控制器会生成一个静态HTML页面,即缓存文件com_index.tpl,这样浏览器读取性能更高。具体缓存过程详见smarty详细使用教程第9点

 

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)

Hot Topics

Java Tutorial
1657
14
PHP Tutorial
1257
29
C# Tutorial
1230
24
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