• 红软基地:您身边最放心的安全下载站!
您所在的位置:首页 > 软件 > 编程开发 > 编程工具 > smarty模板

smarty模板

软件类型:
国外软件
软件语言:
英文
软件大小:
332 KB
软件授权:
免费软件
软件评级:
4
更新时间:
2016-06-16
应用平台:
WinXP, Win7, WinAll
软件简介

smarty模板是一个使用PHP写出来的模板引擎,是目前业界最著名的PHP模板引擎之一。它分离了逻辑代码和外在的内容,提供了一种易于管理和使用的方法,用来将原本与HTML代码混杂在一起PHP代码逻辑分离。简单的讲,目的就是要使PHP程序员同前端人员分离,使程序员改变程序的逻辑内容不会影响到前端人员的页面设计,前端人员重新修改页面不会影响到程序的程序逻辑,这在多人合作的项目中显的尤为重要。7TZ红软基地

smarty模板7TZ红软基地

smarty模板使用说明

一. 安装7TZ红软基地

下载最新版本的Smarty。解压下载的文件(目录结构还蛮复杂的)。接下来演示给大家一个安装实例,看过应该会举一反三的。7TZ红软基地

(1) 在根目录下建立了新的目录learn/,再在learn/里建立一个目录smarty/。将刚才解压缩出来的目录的libs/拷贝到smarty/里,再在smarty/里新建templates目录,templates里新建cache/,templates/,templates_c/, config/。7TZ红软基地

(2) 新建一个模板文件:index.tpl,将此文件放在learn/smarty/templates/templates目录下,代码如下:7TZ红软基地

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"此处DOCTYPE7TZ红软基地

声明不全,下午纠结了好一会,终于看到了,新手朋友们关注下">7TZ红软基地

<html>7TZ红软基地

<head>7TZ红软基地

<metahttp-equiv="Content-Type" content="text/html;charset=gb2312">7TZ红软基地

<title>Smarty</title></head>7TZ红软基地

<body>{#$hello#}</body>7TZ红软基地

</html>7TZ红软基地

新建index.php,将此文件放在learn/下:7TZ红软基地

<?php7TZ红软基地

require 'smarty/libs/Smarty.class.php';7TZ红软基地

$smarty = new Smarty();//设置各个目录的路径,这里是安装的重点7TZ红软基地

$smarty->template_dir ="smarty/templates/templates";7TZ红软基地

$smarty->compile_dir ="smarty/templates/templates_c";7TZ红软基地

$smarty->config_dir = "smarty/templates/config";7TZ红软基地

$smarty->cache_dir ="smarty/templates/cache";7TZ红软基地

//smarty模板有高速缓存的功能,如果这里是true的话即打开caching,但是会造成网页不立即更新的问题,当然也可以通过其他的办法解决7TZ红软基地

$smarty->caching = false;7TZ红软基地

$smarty->left_delimiter = "{#"; //重新定义边界,因为默认边界“{}“符,在html页面中嵌入js脚本文件编写代码段时使用的就是”{}“符,自定义边界符还可以是<{ }>, {/ /} 等7TZ红软基地

$smarty->right_delimiter = "#}";7TZ红软基地

$hello = "Hello World!";//赋值7TZ红软基地

$smarty->assign("hello",$hello);//引用模板文件7TZ红软基地

$smarty->display('index.tpl');?>7TZ红软基地

(3) 执行index.php就能看到Hello World!了。7TZ红软基地

二. 赋值7TZ红软基地

在模板文件中需要替换的值用大括号{}括起来,值的前面还要加$号。例如{$hello}。这里可以是数组,比如{$hello.item1},{$hello.item2}…7TZ红软基地

而PHP源文件中只需要一个简单的函数assign(var , value)。7TZ红软基地

简单的例子:7TZ红软基地

*.tpl:7TZ红软基地

*.php:7TZ红软基地

$hello[name]= “Mr. Green”;7TZ红软基地

$hello[time]=”morning”;7TZ红软基地

$smarty->assign(“exp”,$hello);7TZ红软基地

output:7TZ红软基地

Hello,Mr.Green!Good morning7TZ红软基地

三. 引用7TZ红软基地

网站中的网页一般header和footer是可以共用的,所以只要在每个tpl中引用它们就可以了。7TZ红软基地

示例:*.tpl:7TZ红软基地

{include file="header.tpl"}7TZ红软基地

{* body of template goes here *}7TZ红软基地

{include file="footer.tpl"}7TZ红软基地

软件下载地址
smarty模板
软件推荐
下载排行

精品软件

热门关键词

热门软件推荐