Several examples of smarty templates

Source: Internet
Author: User
Tags smarty template

I. Use of assign and display methods and several variable conditioners

Header ("Content-type:text/html;charset=utf-8");
Loading Smarty engine files
Include "./smarty/smarty.class.php";

Connect to database, extract related data
$title = "Smarty template engine";
$content = "Smarty template engine Smarty template engine Smarty template engine";

Instantiate Smarty
$smarty = new Smarty;

Assigning variables to template files by assign method
$smarty->assign (' title ', $title);
$smarty->assign (' content ', $content);
Assigning an array variable to a template uses
$person = Array (' unsolicited ', 33);
$smarty->assign (' person ', $person);

Assigning an object variable to a template uses
Class person{
Public $name;
Public $age;
}
$per = new Person;
$per->name = ' Fai night ';
$per->age = 555;

$smarty->assign (' per ', $per);

/** variable Adjuster
* The so-called variable adjuster is actually a variable modifier, that is, the variables assigned by PHP to "decorate", that is, to do the relevant formatting operations! And then the output!
* Different regulators have different names, can do different functions, and different regulators have their own different parameters!
{$ variable name | Regulator Name: Parameter 1: Parameter 2: Parameter 3 ...}
*
* such as Date_format
* Function: Format the timestamp! There are many parameters, but the main parameter is the one that represents the day of the month.
*{$ variable name (timestamp) |date_format: '%y-%m-%d%h:%m:%s '}
*/
$time = time ();
$smarty->assign (' time ', $time);

Remove the relevant tags such as JS and HTML! Strip_tags
$str = ' <b> six-channel </b> ';
$smarty->assign (' str ', $STR);

/**
*nl2br
*{$ Variable name | nl2br}
* Role: The string in the \ n conversion to <br/> Sign, so in the display of the real implementation of the effect of line-wrapping!
*/

$str 1 = "Guangzhou \ Tianhe District";
$smarty->assign (' str1 ', $str 1);

/**truncate
*{$ variable name |truncate: string length: ' ... '}
* Function: Only the number of characters before the interception, the back with ... Replace!
*/
$str 2 = "Guangzhou Tianhe District Guangzhou Tianhe District Guangzhou Tianhe District Guangzhou Tianhe District";
$smarty->assign (' str2 ', $str 2);

Replace the markup in the template with the display method and output
$smarty->display ('./templates/index1.tpl ');

INDEX1.TPL file

<title> Testing </title>
<body>
{* Here is the comment content, not displayed on the browser side *}
{$title}{$content}

{* Template tag is an array *}
Name: {$person [0]}<br/>
Age: {$person [1]}

{* Template tag is an object *}
Name: {$per->name}<br/>
Age: {$per->age}<br/>

{* variable adjuster date_format format as follows *}
Variable adjuster date_format:{$time |date_format: '%y-%m-%d%h:%m:%s '}<br/>

{* variable adjuster strip_tags format as follows *}
{$str |strip_tags}<br/>

{* variable adjuster NL2BR format as follows *}
{$str 1|nl2br}<br/>

{* variable adjuster truncate format as follows *}
{$str 2|truncate:10: ' ... '}<br/>
</body>

Second, foreach traversal array test and if ElseIf example

Header ("Content-type:text/html;charset=utf-8");
Loading Smarty engine files
Include "./smarty/smarty.class.php";
Instantiate Smarty
$smarty = new Smarty;

$arr = Array (' name ' = ' Glow Night ',
' Age ' = ' 333 ',
' Home ' = ' moon ',
' Son ' = ' six '
);
$smarty->assign (' arr ', $arr);

$day = 4;
$smarty->assign (' Day ', $day);
Replace the markup in the template with the display method and output
$smarty->display ('./templates/foreach.tpl ');

FOREACH.TPL file

<title>foreach Traversal Array Test </title>
<body>
{*
{foreach from= array name item= element's value name key= element's key name Name= name}
{Foreachelse}
{/foreach}

Main features: Traversal for array elements!
From: which array element to traverse (required option)
Item: Each time the result of the traversal is put into the variable item (required option)
Key: Name
Name: A name for this foreach traversal

*}
{foreach from= $arr item= ' value ' key= ' key ' name= ' name1 '}
{$key}=>{$value}<br/>
{/foreach}


{if $day ==1}
Week 1
{ElseIf $day ==2}
Week 2
{ElseIf $day ==3}
Week 3
{ElseIf $day ==4}
Week 4
{ElseIf $day ==5}
Week 5
{Else}
Weekend
{/if}

</body>

Several examples of smarty templates

Related Article

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.