发布于 2014-12-14 12:57:34 | 200 次阅读 | 评论: 0 | 来源: PHPERZ

这里有新鲜出炉的精品教程,程序狗速度看过来!

Smarty模板引擎

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


本文为大家讲解的是在windows下安装并使用smarty模板引擎,smarty是一个比较流行的php模板引擎,说白了并没有什么安装一说,只是教大家如何在php中引用,感兴趣的同学参考下.

smarty简介

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

smarty安装:

(1)下载Smarty

(2)解压复制libs到你的网站根目录,如htdocs/smarty/libs,想改其他名字也可

(3)在smarty文件夹下建立所需的各种文件

    cache                     缓存目录

    configs                   配置参数目录

    templates             模板目录,如index.tpl

    templates_c         模板与脚本编译文件目录,实际上用户访问的文件

    index.php             用户脚本文件,可自己命名

1、第一种安装方式------------直接在index.php文件里引用安装

在index.php里加入以上代码

require_once 'libs/Smarty.class.php';
$smarty = new Smarty();

$smarty->assign("hello", "hello,world");
$smarty->display('index.tpl');

在templates里建立文件index.tpl,名字可自定义,内容

<!doctype html>

<html lang="en">

<head>

<meta charset="UTF-8">

<title>Smarty的安装使用</title>

</head>

<body>

<h1>{$hello}</h1>

</body>

</html>

执行localhost/smarty/index.php,可看见hello,world的输出

2、第二种安装方式------------在smarty目录下添加配置文件smarty_inc.php

<?php

require_once 'libs/Smarty.class.php';

$smarty = new Smarty();

?>

在index.php文件里引用

require_once 'smarty_inc.php';

$smarty->assign("hello", "hello,world");
$smarty->display('index.tpl');

在templates里建立文件index.tpl,名字可自定义,内容

<!doctype html>

<html lang="en">

<head>

<meta charset="UTF-8">

<title>Smarty的安装使用</title>

</head>

<body>

<h1>{$hello}</h1>

</body>

</html>

执行localhost/smarty/index.php,同样可看见hello,world的输出

3、第三种安装方式------------在smarty目录下添加配置文件smarty_ini.php

<?php
require_once 'libs/Smarty.class.php';
class mySmarty extends Smarty {


function __construct()
{


// Class Constructor.
// These automatically get set with each new instance.


parent::__construct();


$this->setTemplateDir('templates/');
$this->setCompileDir('templates_c/');
$this->setConfigDir('configs/');
$this->setCacheDir('cache/');
}
}
?>

在index.php文件里引用

require_once 'smarty_ini.php';
$smarty = new mySmarty();

$smarty->assign("hello", "hello,world");
$smarty->display('index.tpl');

在templates里建立文件index.tpl,名字可自定义,内容

<!doctype html>

<html lang="en">

<head>

<meta charset="UTF-8">

<title>Smarty的安装使用</title>

</head>

<body>

<h1>{$hello}</h1>

</body>

</html>

执行localhost/smarty/index.php,同样可看见hello,world的输出



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务