Smarty template engine variable and variable modifier usages instance _php instance

Source: Internet
Author: User
Tags modifier smarty template

The application of smarty variable and variable modifier is described in this paper. Share to everyone for your reference. Specifically as follows:

Template file: temp.htm:

Copy Code code as follows:
{config_load file= "foo.conf"}
{$name. Na1|cat: $name [' Na2 ']}
{$name [' na1 ']|cat: ' and ' |cat: $name. Na2}
{foreach from= $name Item=na}
{$na}
{/foreach}
{$dog->leee ()} {$dog->name}
<script>
{literal}
function foobar{
Alert (' foobar! ');
}
{/literal}
</script>
<title>{#pageTitle #}</title>
<body bgcolor= "{#bodyBgColor #}" >
<table border= "{#tableBorderSize #}" bgcolor= "{#tableBgColor #}" >
<tr bgcolor= "{#rowBgColor #}" >
<td>dosomething</td>
<td> 帅锅 </td>
</tr>
</table>
{$smarty. Server.server_name}
{$str |count_words}
-------Constants--------<br>
{$smarty. now}<br/>{$smarty. const.my_const}<br/>{$smarty. template}<br/>{$smarty. Current_dir} <br/>{$smarty. version}<br/>{$smarty. Ldelim|cat: $smarty. Rdelim}
{$smarty. Now|date_format: $config}
{$yesterday |date_format: ' y-m-d '}
{$string |default: ' default variable modifier: Smarty learning '}
{$str 1|escape: ' HTML '}<br/>{$str 2|escape: ' Mail '}
<p>{$str 1|indent|upper}</p>
{$str 1|NL2BR}
{$str 1|regex_replace: "/@\d{3}/": "ABC"}<br/>
{$str 1|replace: "163": "Sina"}<br/>
{$str 1|spacify}<br/>
{$number |string_format: "%.2f"}<br/>
{$number |string_format: '%d '}<br/>
{$str 3|strip: "|"} <br/>
To remove the characters contained between <>: {$str 3|strip_tags}<br/>
Intercept length: {$str 3|truncate:10: "...": true}<br/>
Wrapping by Length: {$str 3|wordwrap:30: "<br/>"}
{Append var= ' name ' value= ' Bob ' index= ' a '
{Append var= ' name ' value= ' John ' index= ' last '}
{$name. last}<br/>
{foreach from= $family item=home}
{foreach from= $home Item=person}
{$person}
{/foreach}
{/foreach}
{$family [1].girl}
{Assign var= "name" Value= "Zhang San Feng"}
{$name}
</body>

PHP File: index.php

Copy Code code as follows:
<?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" => "Handsome Pot", "Na2" => "Beauty");
$smarty->assign ("name", $arr);

Class dog{
Public $name;
Public $age;
function Leee () {
return $this->name. " Doing ";
}
}
$dog = new Dog ();
$dog->name= "Puppy";
$smarty->assign ("Dog", $dog);

$str = "Hello world,i am here." I love smarty! ";
$STR = "Shuai pan";
$str 1 = "<a href= ' http://www.sina.com/' > Sina </a> and\n aassu@163.com";
$str 2 = "aassu@163.com";
$smarty->assign ("str", $STR);
$smarty->assign ("str1", $str 1);
$smarty->assign ("str2", $str 2);
$smarty->assign ("number", 30.293934);
$smarty->assign ("Str3", "Akie abfal <a;fa>,dooerw,show databases,desc table");

$config = "Y-m-d h:i:s";
$smarty->assign ("config", $config);
$smarty->assign ("Yesterday", Strtotime ('-1 day '));
Constant
Define ("My_const", "Baidu");

Use of Append member methods
$family = Array ("husband" => "handsome Pot", "wife" => "Beauty");
$famiadd = Array ("Boy" => "Zhang San Feng", "Girl" => "Wang Zhaojun");
$smarty->append ("Family", $family);
$smarty->append ("Family", $famiadd);
echo "<pre>";
Print_r ($family);
$smarty->display ("temp.htm");
?>

I hope this article will help you with your PHP program design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.