Examples of common methods in Smarty _php example

Source: Internet
Author: User
Tags datetime php session reserved smarty template

The common methods in Smarty are summarized in this paper. Share to everyone for your reference. Specifically as follows:

1. Use of the index and iteration of foreach in Smarty

. Index contains the current array index, starting at zero.

Index example

{* The header block is output every five rows *}
{* Every five elements output once head block *}
<table>
{foreach from= $items key=myid item=i Name=foo}
  {if $smarty. Foreach.foo.index% 5 = 0}
   <tr ><th>Title</th></tr>
  {/if}
  <tr><td>{$i .label}</td></tr>
{/foreach}
</table>

Iteration contains the current number of cycles, which, unlike index, increases by 1 per cycle starting at 1.

Iteration and Index Examples

{* This'll output 0|1, 1|2, 2|3, ... etc *}
{* This example will output 0|1, 1|2, 2|3, ... Wait *}
{foreach from= $myArray item=i Name=foo}
{$smarty. foreach.foo.index}|{ $smarty. Foreach.foo.iteration},
{/foreach}

2. Nested use of section in Smarty

Example 1:

$bookmarks = Array (' 0 ' => array (' name ' => ' n1 ', ' url ' => ' url2 '), ' 1 ' => array (' name ' => ' n21 ', ' url ' => ' url ));
$categories = Array (' 0 ' => array (' cate_id ' => ' N1 ', ' cate_name ' => ' url2 '), ' 1 ' => array (' cate_id ' => ' N21 '), ' Cate_name ' => ' url22 '));
{Section name= ' BM ' loop= $bookmarks}
 Name: $bookmarks [bm].name
 URL: $bookmarks [bm].url
  {section name= ' cate ' loop= $categories [BM]}
     $ categories[bm][cate].cate_id
     $categories [bm][cate].cate_name
  {/section}
{/section}
:::

Example 2:

$lists = Array ();
For (...) {
  $oneList [' dateTime '] = Date ("y-m-d");
  $oneList [' detaillist '] = Array ();
  For (...) {
    $oneList [' detaillist '] [$j] [' count '] = $onecout;
    $oneList [' detaillist '] [$j] [' title '] = $onetitle;
  }
  $lists [] = $oneList;
}::::
{section
name=loop loop= $lists}
  {$lists [loop].datetime}
  { Section name=loop2 loop= $lists [loop]["Detaillist"]}
    {$lists [loop][' Detaillist '][loop2].title}}
    {$lists [ loop]["Detaillist"][loop2].count}
  {/section}
{/section}

3. Other commonly used keywords

<{section loop= $varName [, start= $start, step= $setp, max= $max, $show =true]}>

The name of the name:section, do not add $;
$loop: The variable to loop, to the extent that you want to use assign to manipulate the variable.
$start: The subscript that starts the loop. Default is 0;
$step: Increment of subscript in each cycle;
$show: Boolean type. Decide whether to display this piece. The default is true;

The properties of <{section}>;

Index: Loop subscript. Default is 0;
Index_prev: The last value of the current subscript, which defaults to-1;
Index_next: The next value for the current subscript, defaults to 1;
First: Whether or not to be the second cycle;
Last: Whether it is the final loop;
Iteration: number of cycles;
RowNum: Current line number, iteration alias;
Loop: The last loop number. Number of cycles of section;
Show: whether to display;

<{section loop= $News}>
   News number: <{$News [loop].newid}><br>
   news content: <{$News [Loop].newtitle} ><br>
<{sectionelse}>
   I am sorry
<{/section}>

If usage:

{if $list [row].name eq ' 1}
  ] Week 1
{ElseIf $list [row].name== "2"}
  Week 2
{else}
  default
{/if}

4. Smarty System Variables

{* Displays the page value ($_get) Http://www.example.com/index.php?page=foo *} in the URL
{$smarty. Get.page}
{* displays ' page ' variable from a form ($_post[' page ']) *}
{$smarty. Post.page}
{* Displays the value of the COOKIE variable ' username ' ($_cookie[' username ']) *}
{$smarty. Cookies.username}
{* Display server variable ' server_name ' ($_server[' server_name ']) *}
{$smarty. Server.server_name}
{$smarty. Server.php_self}
{$smarty. Server.script_name}
{* Display system environment variable ' PATH ' *}
{$smarty. Env.path}
{* Display PHP session variable ' ID ' ($_session[' ID ']) *}
{$smarty. Session.id}
{* Display variable ' username ', whether from get/post/cookies/server/env *}
{$smarty. Request.username}
$smarty} reserved variables can be used to access some special template variables, the following are all page request variables.

Here are examples of access to page request variables such as get,post,cookies,server,enviroment and session variables. For example {$smarty. server.server_name} Gets the server variable, {$smarty. Env.path} obtains the system environment variable path,{$smarty. Request.username} Get get/post/ Cookies/server/env the composite variable.

{$smarty. Now} variable is used to access the current timestamp.
The output can be formatted with the Date_format adjuster. For example {$smarty. Now|date_format: "%y-%m-%d%h:%m:%s"}

{$smarty. Const}
You can access the PHP constants directly. For example {$smarty. Const._my_const_val}

{$smarty. Capture}
You can pass {capture}. The output of the {/CAPTURE} structure interception can be accessed using the {$smarty} variable.

{$smarty. config}
{$smarty} variable can access the config variable that has already been loaded.

For example, {$smarty. Config.foo} can represent {#foo #}.

{$smarty. Section}, {$smarty. foreach}
{$smarty} variables can access the properties of the ' section ' and ' foreach ' loops.
{$smarty. Template}
Displays the name of the template that is currently being processed.
{$smarty. Version}
Displays the version of the Smarty template
{$smarty. Ldelim}
Show Left Separator
{$smarty. Rdelim}

Show right delimiter $smarty} reserved variables can be used to access some special template variables, the following are all page request variables.

Here are examples of access to page request variables such as get,post,cookies,server,enviroment and session variables. For example {$smarty. server.server_name} Gets the server variable, {$smarty. Env.path} obtains the system environment variable path,{$smarty. Request.username} Get get/post/ Cookies/server/env the composite variable.

{$smarty. Now} variable is used to access the current timestamp.
The output can be formatted with the Date_format adjuster. For example {$smarty. Now|date_format: "%y-%m-%d%h:%m:%s"}
{$smarty. Const}

You can access the PHP constants directly. For example {$smarty. Const._my_const_val}
{$smarty. Capture}
You can pass {capture}. The output of the {/CAPTURE} structure interception can be accessed using the {$smarty} variable.
{$smarty. config}
{$smarty} variable can access the config variable that has already been loaded.
For example, {$smarty. Config.foo} can represent {#foo #}.
{$smarty. Section}, {$smarty. foreach}
{$smarty} variables can access the properties of the ' section ' and ' foreach ' loops.
{$smarty. Template}
Displays the name of the template that is currently being processed.
{$smarty. Version}
Displays the version of the Smarty template
{$smarty. Ldelim}
Show Left Separator
{$smarty. Rdelim}
Show Right Separator

I hope this article will help you with the PHP program design based on Smarty template.

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.