PHP首页投稿(暂停使用,暂停投稿)

PHP Day6:黄金搭档Smarty(基础部分)

2016-06-24  本文已影响121人  王中阳

数组取值

一维数组
array (  
   "0" => 'home',  
   '1' => 'who',  
   '2'=> 'tank',  
   '3'=> 'what'  
  );  
$this->tpl->assign("onearray", $this->onearray);  
{foreach from=$onearray kkey=k item=value }  
 一维key={$k}  一维value={$value}<br />  
{foreachelse}  
 nothing  
{/foreach}   

显示的结果是

一维key=0 一维value=home
一维key=1 一维value=who
一维key=2 一维value=tank
一维key=3 一维value=what
{section name=one loop=$onearray start=0 step=1}  
 index={$smarty.section.one.index},  
 index_prev={$smarty.section.one.index_prev},  
 index_next={$smarty.section.one.index_next},  
 first={$smarty.section.one.first},  
 last={$smarty.section.one.last},  
 iteration ={$smarty.section.one.iteration},  
 total={$smarty.section.one.total},  
 value={$onearray[one]}<br />  
{sectionelse}  
 nothing  
{/section} 

显示的结果是

index=0, index_prev=-1, index_next=1, first=1, last=, iteration =1, total=4, value=home
index=1, index_prev=0, index_next=2, first=, last=, iteration =2, total=4, value=who
index=2, index_prev=1, index_next=3, first=, last=, iteration =3, total=4, value=tank
index=3, index_prev=2, index_next=4, first=, last=1, iteration =4, total=4, value=what

常用函数

每一个smarty标签都需要用{}或者{% %}括起来,具体用什么形式可以在配置文件中指定。
但是标签的用法都是一样的。

{config_load file="colors.conf"}

{include file="header.tpl"}

{if $highlight_name}
    Welcome, <font color="{#fontColor#}">{$name}!</font>    
{else}
    Welcome, {$name}!
{/if}

{include file="footer.tpl"}

属性

{include file="header.tpl"}

{include file=$includeFile}

{include file=#includeFile#}

{html_select_date display_days=yes}

<SELECT name=company>
{html_options values=$vals selected=$selected output=$output}
</SELECT>

双引号里值的嵌入

SYNTAX EXAMPLES:
{func var="test $foo test"} <-- sees $foo
{func var="test $foo_bar test"} <-- sees $foo_bar
{func var="test $foo[0] test"} <-- sees $foo[0]
{func var="test $foo[bar] test"} <-- sees $foo[bar]
{func var="test $foo.bar test"} <-- sees $foo (not $foo.bar)
{func var="test `$foo.bar` test"} <-- sees $foo.bar

PRACTICAL EXAMPLES:
{include file="subdir/$tpl_name.tpl"} <-- will replace $tpl_name with value
{cycle values="one,two,`$smarty.config.myval`"} <-- must have backticks

Smarty的数学运算

{$foo+1}

{$foo*$bar}
     
{* some more complicated examples *}
     
{$foo->bar-$bar[1]*$baz->foo->bar()-3*7}

{if ($foo+$bar.test%$baz*134232+10+$b+10)}

{$foo|truncate:"`$fooTruncCount/$barTruncFactor-1`"}

{assign var="foo" value="`$foo+$bar`"}

Smarty变量

按照变量的来源分类


{$Name} 

{$Contacts[row].Phone}

<body bgcolor="{#bgcolor#}">  //#从配置文件中读取变量#

最常用的是从PHP中读数据

index.php:


$smarty = new Smarty;
$smarty->assign('firstname', 'Doug');
$smarty->assign('lastLoginDate', 'January 11th, 2001');
$smarty->display('index.tpl');

index.tpl:

Hello {$firstname}, glad to see you could make it.
<p>
Your last login was on {$lastLoginDate}.

OUTPUT:

Hello Doug, glad to see you could make it.
<p>
Your last login was on January 11th, 2001.

smarty组合调节器

对于同一个变量,你可以使用多个修改器。他们会按照从左到右的顺序依次组合使用。使用时必须使用‘|’作为他们之间的分隔符。

index.php:

$smarty = new Smarty;
$smarty->assign('articleTitle', 'Smokers are Productive, but Death Cuts Efficiency.');
$smarty->display('index.tpl');

index.tpl:
         
{$articleTitle}
{$articleTitle|upper|spacify}
{$articleTitle|lower|spacify|truncate}
{$articleTitle|lower|truncate:30|spacify}
{$articleTitle|lower|spacify|truncate:30:". . ."}


OUTPUT:

Smokers are Productive, but Death Cuts Efficiency.
S M O K E R S A R E P R O D U C T I V E , B U T D E A T H C U T S E F F I C I E N C Y .
s m o k e r s a r e p r o d u c t i v e , b u t d e a t h c u t s...
s m o k e r s a r e p r o d u c t i v e , b u t . . .
s m o k e r s a r e p. . .

smarty的内置函数

{* this will print literal delimiters out of the template *}

{ldelim}funcname{rdelim} is how functions look in Smarty!


OUTPUT:

{funcname} is how functions look in Smarty!
{literal}
    <script language=javascript>

    <!--
    function isblank(field) {
    if (field.value == '') 
    { return false; }
    else
    {
    document.loginform.submit();
    return true;
    }
    }
    // -->

    </script>
{/literal}
{* the loop variable only determines the number of times to loop.
 you can access any variable from the template within the section.
 This example assumes that $custid, $name and $address are all
 arrays containing the same number of values *}
{section name=customer loop=$custid}
    id: {$custid[customer]}<br>
    name: {$name[customer]}<br>
    address: {$address[customer]}<br>
    <p>
{/section}


OUTPUT:

id: 1000<br>
name: John Smith<br>
address: 253 N 45th<br>
<p>
id: 1001<br>
name: Jack Jones<br>
address: 417 Mulberry ln<br>
<p>
id: 1002<br>
name: Jane Munson<br>
address: 5605 apple st<br>
<p>
{section name=customer loop=$custid}
    {$smarty.section.customer.rownum} id: {$custid[customer]}<br>
    {/section}


    OUTPUT:

    1 id: 1000<br>
    2 id: 1001<br>
    3 id: 1002<br>
{section name=customer loop=$custid}
{$smarty.section.customer.index} id: {$custid[customer]}<br>
{/section}

There were {$smarty.section.customer.loop} customers shown above.

OUTPUT:

0 id: 1000<br>
1 id: 1001<br>
2 id: 1002<br>

There were 3 customers shown above.
    {section name=customer loop=$custid step=2} 
    {$smarty.section.customer.index} id: {$custid[customer]}<br>
    {/section}

    There were {$smarty.section.customer.total} customers shown above.

    OUTPUT:

    0 id: 1000<br>
    2 id: 1001<br>
    4 id: 1002<br>

    There were 3 customers shown above.

-{strip}{/strip}去除无用的空格

{* the following will be all run into one line upon output *}
{strip}
<table border=0>
    <tr>
        <td>
            <A HREF="{$url}">
            <font color="red">This is a test</font>
            </A>
        </td>
    </tr>
</table>
{/strip}


OUTPUT:

<table border=0><tr><td><A HREF="http://my.domain.com"><font color="red">This is a test</font></A></td></tr></table>

注意:

自定义函数

setup.conf
----------

emphstart = <b>
emphend = </b>
title = Welcome to {$company}'s home page!
ErrorCity = You must supply a {#emphstart#}city{#emphend#}.
ErrorState = You must supply a {#emphstart#}state{#emphend#}.


index.tpl
---------

{config_load file="setup.conf"}

{eval var=$foo}
{eval var=#title#}
{eval var=#ErrorCity#}
{eval var=#ErrorState# assign="state_error"}
{$state_error}

OUTPUT:

This is the contents of foo.
Welcome to Foobar Pub & Grill's home page!
You must supply a <b>city</b>.
You must supply a <b>state</b>.


一个敲代码,爱分享的人,我在这里!

来玩啊
上一篇 下一篇

猜你喜欢

热点阅读