Smarty3.1(一)windows下安装与使用

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的输出



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值