php smarty模板引擎怎么用?-PHP问题

资源魔 20 0

php smarty模板引擎的用法:起首到Smarty的民间网站下载并装置;而后对Smarty类库中的成员进行设置;最初基于变量对Smarty的一切拜访便可。

php smarty模板引擎的用法:

1、概述:

Smarty 是 PHP 泛滥模板引擎中的一个,它是依据 PHP 编写的一个类库。

Smarty 的优点:

一、优化网站拜访速率;

二、网页前端设计以及顺序的别离;

2、Smarty 的装置

一、需求到 Smarty 的民间网站 http://www.smarty.net/download.php 下载最新的 Smarty 版本,比方下载的版本为:Smarty-2.6.18.tar.tar;

二、解压 Smarty-2.6.18.tar.tar 紧缩包,会发现都不少文件以及文件夹,除了了 libs 文件夹外,其余的全副删除了,都不用;

三、当挪用 Smarty 模板引擎时,应先应用 PHP 的 require 语句载入 libs/Smarty.class.php 这个文件。

3、Smarty 类库的默许设置

require 进 Smarty.class.php 文件后,假如需求对 Smarty 类库中的成员进行设置,有两种办法:一种是间接正在 Smarty.class.php 文件中修正;一种是正在初始化类库之落后行从新指定,普通应用后者。上面对 Smarty 类库中的成员属性进行阐明:

一、$template_dir:设置网站中的模板文件寄存的目次,默许目次是 templates

二、$compile_dir:设置网站中编译文件寄存的目次,默许目次是 templates_c

三、$config_dir:界说用于寄存模板非凡设置装备摆设文件的目次,默许是 configs

四、$left_delimiter:用于模板中的左完结符变量,默许是 '{'

五、$right_delimiter:用于模板中的右完结符变量,默许是 '}'

4、变量的应用:

Smarty 中一切的拜访都是基于变量的,上面经过一个实例来进行阐明。

实例思绪:主文件经过引入模板初始化设置装备摆设文件(init.inc.php)以及一个类,并对模板中的变量进行赋值显示。

起首,设置 init.inc.php 文件,作为 Smarty 模板的初始化设置装备摆设文件

init.inc.php
<?php
  define('ROOT_PATH', dirname(__FILE__)); //界说网站根目次
  require ROOT_PATH.'/libs/Smarty.class.php'; //载入 Smarty 文件
  $_tpl = new Smarty();      //实例化一个工具
  $_tpl->template_dir = ROOT_PATH.'/tpl/'; //从新设置模板目次为根目次下的 tpl 目次
  $_tpl->compile_dir = ROOT_PATH.'./com/'; //从新设置编译目次为根目次下的 com 目次
  $_tpl->left_delimiter = '<{';   //从新设置左定界符为 '<{'
  $_tpl->right_delimiter = '}>';    //从新设置左定界符为 '}>'
?>

主文件 index.php

<?php
  require 'init.inc.php'; //引入模板初始化文件
  require 'Persion.class.php'; //载入工具文件
  global $_tpl;
  $title = 'This is a title!';
  $content = 'This is body content!';
  /*
  * 1、从 PHP 中调配给模板变量;
  * 静态的数据(PHP从数据库或文件,和算法天生的变量)
  * 任何类型的数据均可以从PHP调配过去,次要包罗以下
  * 标量:string、int、double、boolean
  * 复合:array、object
  *   NULL
  * 索引数组是间接经过索引来拜访的
  * 联系关系数组,没有是应用[联系关系下标]而是应用 . 下标的形式
  * 工具是间接经过->来拜访的
  * */
  $_tpl->assign('title',$title);
  $_tpl->assign('content',$content); //变量的赋值
  $_tpl->assign('arr1',array('abc','def','ghi'));  //索引数组的赋值
  $_tpl->assign('arr2',array(array('abc','def','ghi'),array('jkl','mno','pqr'))); //索引二维数组的赋值
  $_tpl->assign('arr3',array('one'=>'111','two'=>'222','three'=>'333')); //联系关系数组的赋值
  $_tpl->assign('arr4',array('one'=>array('one'=>'111','two'=>'222'),'two'=>array('three'=>'333','four'=>'444'))); //联系关系二维数组的赋值
  $_tpl->assign('arr5',array('one'=>array('111','222'),array('three'=>'333','444'))); //联系关系以及索引夹杂数组的赋值
  $_tpl->assign('object',new Persion('小易', 10)); //工具赋值
  //Smarty 中数值也能够进交运算(+-*/^……)
  $_tpl->assign('num1',10);
  $_tpl->assign('num2',20);
  $_tpl->display('index.tpl');
?>

主文件 index.php 的模板文件 index.tpl(放置正在/tpl/目次下)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title><{$title}></title>
  </head>  <body>
    变量的拜访:<{$content}>
    <br />
    索引数组的拜访:<{$arr1[0]}> <{$arr1[1]}> <{$arr1[2]}>
    <br />
    索引二维数组的拜访: <{$arr2[0][0]}> <{$arr2[0][1]}> <{$arr2[0][2]}> <{$arr2[1][0]}> <{$arr2[1][1]}> <{$arr2[1][2]}>
    <br />
    联系关系数组的拜访:<{$arr3.one}> <{$arr3.two}> <{$arr3.three}>
    <br />
    联系关系二维数组的拜访:<{$arr4.one.one}> <{$arr4.one.two}> <{$arr4.two.three}> <{$arr4.two.four}>
    <br />
    联系关系以及索引夹杂数组的拜访:<{$arr5.one[0]}> <{$arr5.one[1]}> <{$arr5[0].three}> <{$arr5[0][0]}>
    <br />
    工具中成员变量的拜访:<{$object->name}> <{$object->age}>
    <br />
    工具中办法的拜访:<{$object->hello()}>
    <br />
    变量的运算:<{$num1+$num2}>
    <br />
    变量的夹杂运算:<{$num1+$num2*$num2/$num1+44}>
    <br />
  </body>
</html>

Persion.class.php

<?php
  class Persion {
   public $name; //为了拜访不便,设定为public
   public $age;
   //界说一个结构办法
   public function __construct($name,$age) {
     $this->name = $name;
     $this->age = $age;
   }
   //界说一个 hello() 办法,输入名字以及春秋
   public function hello() {
     return '您好!我叫'.$this->name.',往年'.$this->age.'岁了。';
   }
 }
?>

执行后果:

变量的拜访:This is body content!
索引数组的拜访:abc def ghi
索引二维数组的拜访: abc def ghi jkl mno pqr
联系关系数组的拜访:111 222 333
联系关系二维数组的拜访:111 222 333 444
联系关系以及索引夹杂数组的拜访:111 222 333 444
工具中成员变量的拜访:小易 10
工具中办法的拜访:您好!我叫小易,往年10岁了。
变量的运算:30
变量的夹杂运算:94

相干学习保举:PHP编程从入门到通晓

以上就是php smarty模板引擎怎样用?的具体内容,更多请存眷资源魔其它相干文章!

标签: php php教程 smarty php故障解决 php使用问题

抱歉,评论功能暂时关闭!