Smarty快速入门

xiaoxiao2021-02-28  49

1.smarty介绍

 

什么是smarty

PHP模板引擎HTMLSmarty是一个使用写出来的,它分离了逻辑代码和外在的内容,提供了一种易于管理和使用的方法,用来将原本与代码混杂在一起PHP代码逻辑分离。

 

smarty的优点

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

2.编译型:采用Smarty编写的程序在运行时要编译成一个非模板技术的PHP文件,这个文件采用了PHP与HTML混合的方式,在下一次访问模板时将WEB请求直接转换到这个文件中,而不再进行模板重新编译.

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

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

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

 

 

扩展 模板

DwooTemplateFXLtemplateSmarty

 

2.获取

 

http://www.smarty.net/download官方网址:

  

https://github.com/smarty-php/smarty/releases/tag/v3.1.30Smarty 3.x: PHP 5.2+

https://github.com/smarty-php/smarty/releases/tag/v2.6.30Smarty 2.x: PHP 4 or 5

 

3.安装

其实==>解压。

 

主目录结构分析

lib目录分析

 

 

<?php

//1.引入smart的类

require './libs/Smarty.class.php';

//2.实例化smarty的对象

$smarty = new Smarty;

//3.设置相关属性

//$smarty->force_compile = true;//强行编译

$smarty->debugging = true;//调试模式开启

$smarty->caching = true;//cache缓存

$smarty->cache_lifetime = 120;//cache的周期

$smarty->template_dir="./templates";//设置模板目录

$smarty->compile_dir="./templates_c";//设置相关的编译后的目录

//4.分配数据

$smarty->assign('title',"这个是标题");

$smarty->assign('content',"这个是内容");

 

 

//5.指定输出的模板文件./templates/index.html

 

$smarty->display('index.html');

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<title>{$title}</title>

</head>

<body>

{$content}

</body>

</html>

 

 

转载请注明原文地址: https://www.6miu.com/read-35825.html

最新回复(0)