Heim  >  Artikel  >  php教程  >  PHP 中使用 Smarty 之二:配置文件在模板变量中的使用

PHP 中使用 Smarty 之二:配置文件在模板变量中的使用

WBOY
WBOYOriginal
2016-06-13 10:45:12714Durchsuche

 

配置文件在模板中的作用是:给前端设计页面定义变量,主要控制的是模板的外观,与PHP 程序无关。

        使用步骤:

        1、使用$tpl->configs_dir="目录"  //指定配置文件存放的目录;

        2、在模板中要使用 加载f配置文件,如果有区域的话,可以使用section="区域" 来指定区域

              设置区域的目的是:为了不同的文件调用不同区域的配置文件变量。

              在配置文件中是通过“[区域名称]”来指定区域的,其他没有指定区域的变量均为共有变量,即每一个页面都可以使用。

        3、在指定的目录下建立配置文件。

        下面通过一个实例来演示,实例思路:主文件index.php 调用模板文件index.tpl,在index.tpl 中设置配置文件变量(与PHP 程序无关)

init.inc.php Smart模板引擎初始化文件

 

   define('ROOT_PATH', dirname(__FILE__));         //网站根目录 

   require ROOT_PATH.'/libs/Smarty.class.php'; //引入Smart 模板引擎 

   $_tpl = new Smarty();                   //初始化一个对象 

   $_tpl->template_dir = ROOT_PATH.'/tpl/'; //重新设置网站的模板目录 

   $_tpl->compile_dir = ROOT_PATH.'./com/'; //重新设置网站的编译文件目录 

   $_tpl->config_dir = ROOT_PATH.'/configs/';   //重新设置网站的配置文件目录 

   $_tpl->left_delimiter = '

   $_tpl->right_delimiter = '}>';                //重新设置网站的右定界符 

?> 

 

index.php

 

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

   global $_tpl; 

    $_tpl->display('index.tpl');  //载入模板文件 

?> 

 

index.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"> 

   

 

         

       

配置文件在模板变量中的使用 

   

   

 

      

 

             

 

                

 

                

 

                

 

                 

 

             

 

             

 

                

 

                

 

                

 

                

 

             

 

             

 

                 

 

                

 

                

 

                

 

             

 

             

 

                

 

             

 

      

aaaa aaaa aaaa aaaa
aaaa aaaa aaaa aaaa
aaaa aaaa aaaa aaaa
 

                        区域变量的显示: 

                       
 

                       
 

                       
 

                

 

  

     

 

 

/configs/view.conf 配置文件

border=2

tabw=600

tabh=500

bgcolor=yellow

align=center

 

[one]

colspan=4

aa=one section

 

[two]

bb=two section

 

[three]

cc=three section

 

执行结果,如图所示:

摘自:Lee.的专栏

Stellungnahme:
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
Vorheriger Artikel:php求两个文件相对路径Nächster Artikel:PHP的回调方法