smarty使用变量(从配置文件读取变量 +Smarty变量调节器)

来源:互联网 发布:山西 软件开发水平 编辑:程序博客网 时间:2024/05/21 10:43
 从配置文件读取变量

Smarty提供了配置文件解决界面的选择功能,并不一定要用到,根据实际

      情况选择使用

Apache----conf/httpd.conf

Php----- php.ini

Mysql---mysql.ini

Smarty----configs/*.conf

       $this->template_dir

       $this->compile_dir

       $this->configs

     不建议将以上目录设置到web跟文档目录下,安全

     

 

使用步骤

1)              smarty.class.php

   var $config_dir      =  'configs';

2)              编辑init.inc.php

$tpl->config_dir = ‘configs’;

3)到web跟文档目录之外位置新建文件configs

4)建立文件configs/foo.conf

5)编辑foo.conf文件内容

  #表示注释行

  自定义变量名=该变量的值

 6)打开*.html(tpl)模板文件

l      加载配置文件 ---使用smarty的内建函数 config_load

      <{cofig_load  file=”foo.conf”}>

      使用变量<{#来自于配置文件的变量名#}>

如果需要将内容放到不同模板文件中,定义节(局部变量)

  [节名称]

 

 

 

3)            smarty保留变量

l      Request variables

  PHP中的超全局变量数组

 $_GET

            第一种:

 url:http://localhost/b.php?page=10

             *.php

             $tpl->assign(“page”,$_GET[‘page’]);

             *.html(tpl)

             <{$page}>

      $_POST

      $_REQUEST

      $_ENV

      $_SESSION

      $_COOKIE

      $_SERVER

      $GLOBALS

      $_FILES---不用

使用保留变量显示超全局数组

 

例如:b.php

   $tpl->assign("page",$_GET['page']);

  a.html

   <{$page}>

  在地址栏….?page=10

 

  还可以例如:

   a.html

  <{$smarty.get.page}>

  在地址栏….?page=10

 

第二种:$smarty.超全局变量数组名.元素的下标名

 例如:foo.conf:

border=1

bgcolor=pink

a.    html:

<{config_load file="foo.conf" section="one"}>

<!--#和变量在一起不能有空格-->

    <table border="<{$smarty.config.border}>">

    <tr bgcolor="<{#bgcolor#}>">

          <td>

               123456abcd

          </td>

          <td>

               123456abcd

          </td>

          <td>

               123456abcd

          </td>

          <td>

               123456abcd

          </td>

    </tr>

</table>

 

[自定义局部变量]节

例如:foo.conf:

[one]

 a=11111111111111

a.html:

  <{config_load file="foo.conf" section="one"}>

  <{#a#}>

PHP中的预定义常量

__FILE__

__LINE__

__FUNCTION__

__CLASS__

__METHOD__

PHP_OS

PHP_VERSION

TRUE

FALSE

NULL

DIRECTORY_SEPARATOR

PATH_SEPARATOR

E_ERROR

E_WARNING

E_PASER

E_NOTICE

M_PI

 

第三部分:Smarty变量调节器

1、            变量调节器分隔符   |

作用:修饰变量的显示格式

<{$smarty.now}> 时间戳

<{$smarty.now|date_format:%......}>

 

例如:a.html

   <!--声明现在时间-->

<{$smarty.now|date_format:"%Y-%m-%d %H:%M:%S"}><br>

<{$smarty.now|date_format:"%H"}><br>

<{$smarty.now|date_format:"%D"}><br>

<{$smarty.now|date_format:"%Y"}><br>

<{$smarty.now|date_format:"%y"}><br>

<{$smarty.now|date_format:"%W"}><br>

<{$smarty.now|date_format:"%w"}><br>

 

<!--时间戳-->

<{$smarty.now}><br>

 

2、            capitalize:首字母大写

                   语法:<{$变量|capitalize}>

count_characters:统计字符数

                    语法:<{$变量|count_characters:参数}>

    参数:true 或false

 

date_format:时间格式化输出

                     语法:<{$变量|date_format:”字符串”:“字符串“}>

     参数:被连接的字符串

 

例如:b.php

$tpl->assign("title","this is php various");

 

 $tpl->assign("daxie","this is php varioussdfasdfsdafsdfsdafsd.

  sdafsafsdfsd.");

 

  $tpl->assign("xiaoxie","This Is Php Various");

 

  $tpl->assign("number","23.5787446");

 

  $tpl->assign("kongge","Grandmother of\neight makes\t hole in one.");

 

  $tpl->assign("biaoqian","Blind Woman Gets <font face=\"helvetica\">New Kidney</font> from Dad she Hasn't Seen in <b>years</b>.");

 

 

例如:a.html

首字母大写:

<{$title}><br>

<{$title|capitalize}><br>

 

       Cat:连接字符串

           语法:<{$变量|cat:”参数”}>

参数:被连接的字符串

           Count_paragraphs:统计段落数

          语法:<{$变量| Count_paragraphs }>

   例如:a.html

              连接字符串cat:

<{$title|cat:" abc"}><br>

 

例如:a.html

<!--统计字符数-->

<!--参数true控制空白是否当做字符数进行统计-->

统计字符数:

<{$title|count_characters}><br>

<{$title|count_characters:true}><br>

    

例如:a.html

        <!--统计段落数量--><br>

统计段落数量:

<{$daxie|count_paragraphs}><br>

 

例如:a.html

<!--统计计算句数--><br>

统计计算句数:

<{$daxie|count_sentences}><br>

 

例如:a.html

      <!--统计计算词数--><br>

统计计算词数:

<{$daxie|count_words}><br>

 

例如:a.html

<!--默认值--><br>

默认值:

<{$title|default:"no title"}><br>

<{$myTitle|default:"no title"}><br>

 

 

例如:a.html

<!--编码--><br>

编码:

<{$title|escape}><br>

<{$title|escape:"quotes"}><br>

<{$title|escape:"url"}><br>

 

 

例如:a.html

<!--缩进--><br>

缩进:<br>

<{$daxie|indent}><br>

<{$daxie|indent:10}><br>

<{$daxie|indent:1:"\t"}><br>

 

 

例如:a.html

<!--小写--><br>

小写:<br>

<{$xiaoxie}><br>

<{$xiaoxie|lower}><br>

 

 

 

例如:a.html

<!--换行符替换成 <br />]--><br>

换行符替换成br:<br>

<{$daxie}><br>

<{$daxie|nl2br}><br>

 

 

例如:a.html

<!--正则替换--><br>

正则替换:<br>

<{$daxie}><br>

<{$daxie|regex_replace:"/[\r\t\n]/":" "}><br>

 

 

 

例如:a.html

<!--替换--><br>

替换:<br>

<{$daxie}><br>

<{$daxie|replace:"Garden":"Vineyard"}><br>

<{$daxie|replace:" ":" "}><br>

 

 

 

例如:a.html

<!--插空--><br>

插空:<br>

<{$daxie}><br>

<{$daxie|spacify}><br><br>

<{$daxie|spacify:"^^"}><br>

 

 

 

例如:a.html

<!--字符串格式化--><br>

字符串格式化:<br>

<{$number}><br>

<{$number|string_format:"%.2f"}><br>

<{$number|string_format:"%d"}><br>

 

 

 

例如:a.html

<!--去除(多余空格)--><br>

去除(多余空格):<br>

<{$kongge}><br>

<{$kongge|strip}><br>

<{$kongge|strip:"&nbsp;"}><br>

 

 

 

例如:a.html

<!--去除Html标签--><br>

去除Html标签:<br>

<{$biaoqian}><br>

<{$biaoqian|strip_tags}><br>

 

 

例如:a.html

<!--截取--><br>

截取:<br>

<{$daxie}><br>

<{$daxie|truncate}><br>

<{$daxie|truncate:30}><br>

<{$daxie|truncate:30:""}><br>

<{$daxie|truncate:30:"---"}><br>

<{$daxie|truncate:30:"":true}><br>

<{$daxie|truncate:30:"...":true}><br>

 

 

 

例如:a.html

<!--行宽约束--><br>

行宽约束:<br>

<{$daxie}><br>

<{$daxie|wordwrap:30}><br>

<{$daxie|wordwrap:20}><br>

<{$daxie|wordwrap:30:"<br>\n"}><br>

<{$daxie|wordwrap:30:"\n":true}><br>

 

 

 

 

 

 

 

 

 

原创粉丝点击