Heim > php教程 > php手册 > Hauptteil

PHP 中使用 Smarty 之一:变量的使用

WBOY
Freigeben: 2016-06-13 10:45:09
Original
800 Leute haben es durchsucht

 

  Smarty 中所有的访问都是基于变量的,下面通过一个实例来进行说明。

        实例思路:主文件通过引入模板初始化配置文件(init.inc.php)和一个类,并对模板中的变量进行赋值显示。

        首先,设置init.inc.php 文件,作为Smarty 模板的初始化配置文件

init.inc.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

 

   require 'init.inc.php'; //引入模板初始化文件 

   require 'Persion.class.php';  //载入对象文件 

  

   global $_tpl; 

   $title = 'This is a title!'; 

   $content = 'This is body content!'; 

  

   /*

   * 一、从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/目录下)

 

ttp://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

http://www.w3.org/1999/xhtml"> 

   

 

         

       

 

      

 

       变量的访问: 

      
 

       索引数组的访问:  

      
 

       索引二维数组的访问:  

      
 

       关联数组的访问:  

      
 

       关联二维数组的访问:  

      
 

       关联和索引混合数组的访问:  

      
 

       对象中成员变量的访问:name}> age}> 

       
 

       对象中方法的访问:hello()}> 

      
 

       变量的运算: 

      
 

       变量的混合运算: 

      
 

     

 

 

 

Persion.class.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

摘自:lee 的专栏

Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Empfehlungen
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!