smarty模板引擎中變數及變數修飾器用法執行個體,smarty模板_PHP教程

來源:互聯網
上載者:User

smarty模板引擎中變數及變數修飾器用法執行個體,smarty模板


本文執行個體講述了smarty變數及變數修飾器的應用。分享給大家供大家參考。具體如下:

模板檔案:temp.htm:
複製代碼 代碼如下:{config_load file="foo.conf"}
{$name.na1|cat:$name['na2']}
{$name['na1']|cat:'與'|cat:$name.na2}
{foreach from=$name item=na}
{$na}
{/foreach}
{$dog->leee()}{$dog->name}

{#pageTitle#}






dosomething 帥鍋

{$smarty.server.SERVER_NAME}

{$str|count_words}

-------常量--------

{$smarty.now}
{$smarty.const.MY_CONST}
{$smarty.template}
{$smarty.current_dir}
{$smarty.version}
{$smarty.ldelim|cat:$smarty.rdelim}

{$smarty.now|date_format:$config}
{$yesterday|date_format:'Y-m-d'}

{$string|default:'default變數修飾:smarty學習'}

{$str1|escape:'html'}
{$str2|escape:'mail'}

{$str1|indent|upper}


{$str1|nl2br}

{$str1|regex_replace:"/@\d{3}/":"ABC"}

{$str1|replace:"163":"sina"}

{$str1|spacify}


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

{$number|string_format:"%d"}


{$str3|strip:"|"}

去除包含在<>之間的字元:{$str3|strip_tags}

截取長度:{$str3|truncate:10:"...":true}

按長度換行:{$str3|wordwrap:30:"
"}

{append var='name' value="Bob" index="first"}
{append var='name' value="John" index="last"}
{$name.last}

{foreach from=$family item=home}
{foreach from=$home item=person}
{$person}
{/foreach}
{/foreach}
{$family[1].girl}

{assign var="name" value="張三丰"}
{$name}

php檔案:index.php
複製代碼 代碼如下:<?php
require_once('libs/Smarty.class.php');
$smarty = new Smarty();
$smarty->setTemplateDir($_SERVER['DOCUMENT_ROOT']."/php/templates/");
$smarty->setCompileDir($_SERVER['DOCUMENT_ROOT']."/php/templates_c/");
$smarty->setCacheDir($_SERVER['DOCUMENT_ROOT']."/php/cache/");
$smarty->caching = false;
$arr = array("na1"=>"帥鍋","na2"=>"美女");
$smarty->assign("name",$arr);

class Dog{
public $name;
public $age;
function leee(){
return $this->name."在幹嗎";
}
}
$dog = new Dog();
$dog->name="小狗";
$smarty->assign("dog",$dog);

//$str = "hello world,i am here. i love smarty!";
$str = "帥鍋";
$str1 = "新浪 And\n aassu@163.com";
$str2 = "aassu@163.com";
$smarty->assign("str",$str);
$smarty->assign("str1",$str1);
$smarty->assign("str2",$str2);
$smarty->assign("number",30.293934);
$smarty->assign("str3","akie abfal ,dooerw,show databases,desc table");

$config = "Y-m-d H:i:s";
$smarty->assign("config",$config);
$smarty->assign("yesterday",strtotime('-1 day'));
//常量
define("MY_CONST","百度");

//append成員方法的使用
$family = array("husband"=>"帥鍋","wife"=>"美女");
$famiadd = array("boy"=>"張三丰","girl"=>"王昭君");
$smarty->append("family",$family);
$smarty->append("family",$famiadd);
echo "

";
print_r($family);
$smarty->display("temp.htm");
?>

希望本文所述對大家的php程式設計有所協助。

http://www.bkjia.com/PHPjc/946753.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/946753.htmlTechArticlesmarty模板引擎中變數及變數修飾器用法執行個體,smarty模板 本文執行個體講述了smarty變數及變數修飾器的應用。分享給大家供大家參考。具體如下:...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.