Home > Backend Development > PHP Tutorial > Summary of common methods in smarty, summary of smarty examples_PHP tutorial

Summary of common methods in smarty, summary of smarty examples_PHP tutorial

WBOY
Release: 2016-07-13 09:44:51
Original
845 people have browsed it

A summary of commonly used methods in smarty, a summary of smarty examples

The examples in this article summarize the commonly used methods in smarty. Share it with everyone for your reference. The details are as follows:

1. The use of foreach index and iteration in Smarty

.index contains the current array index, starting from zero.

index example

{* The header block is output every five rows *}
{* 每五行输出一次头部区块 *}
<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>

Copy after login

.iteration contains the current number of loops, which is different from index. It starts from 1 and increases by 1 each time it loops.

Iteration and index examples

{* this will output 0|1, 1|2, 2|3, ... etc *}
{* 该例将输出0|1, 1|2, 2|3, ... 等等 *}
{foreach from=$myArray item=i name=foo}
{$smarty.foreach.foo.index}|{$smarty.foreach.foo.iteration},
{/foreach}

Copy after login

2. Nested use of sections in smarty

Example 1:

$bookmarks = array('0' => array('name'=> 'n1', 'url'=>'url2'), '1' => array('name'=> 'n21', 'url'=>'url22'));
$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}
::::

Copy after login

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}

Copy after login

3. Other commonly used keywords

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

name: the name of the section, no need to add $;
$loop: The variable to be looped. Use assign to operate on this variable.
$start: The index to start the loop. Default is 0;
$step: the increment of subscript in each loop;
$show: boolean type. Decide whether to display this area. Default is true;

<{section}> attributes;

index: loop index. Default is 0;
Index_prev: the previous value of the current index, the default is -1;
Index_next: The next value of the current index, the default is 1;
First: Whether it is the first cycle;
Last: Whether it is the last loop;
Iteration: number of loops;
Rownum: current row number, alias of iteration;
Loop: The last loop number. Number of loops for Section;
Show: Whether to display;

<{section loop=$News}>
   新闻编号:<{$News[loop].newID}><br>
   新闻内容:<{$News[loop].newTitle}><br>
<{sectionelse}>
   I am sorry
<{/section}>

Copy after login

if usage:

{if $list[row].name eq "1"}
  星期1
{elseif $list[row].name=="2"}
  星期2
{else}
  默认
{/if}

Copy after login

4. smarty system variables

{* Display the page value in the URL ($_GET) http://www.example.com/index.php?page=foo *}
{$smarty.get.page}
{* Display the "page" variable from a form ($_POST['page'])*}
{$smarty.post.page}
{* Display the value of 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", regardless of 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.

The following are examples of accessing page request variables such as get, post, cookies, server, environment and session variables. For example, {$smarty.server.SERVER_NAME} obtains server variables and {$smarty.env.PATH} obtains system environment variables. path,{$smarty.request.username} obtains the composite variable of get/post/cookies/server/env.

{$smarty.now} variable is used to access the current timestamp.
You can use the date_format adjuster to format the output. For example {$smarty.now|date_format:"%Y-%m-%d %H:%M:%S"}

{$smarty.const}
You can access PHP constants directly. For example {$smarty.const._MY_CONST_VAL}

{$smarty.capture}
The output captured through the {capture}..{/capture} structure can be accessed using the {$smarty} variable.

{$smarty.config}
The {$smarty} variable can access the loaded config variable.

For example {$smarty.config.foo} can represent {#foo#}.

{$smarty.section}, {$smarty.foreach}
The {$smarty} variable has access to properties of 'section' and 'foreach' loops.
{$smarty.template}
Displays the name of the currently processed template.
{$smarty.version}
Show version of 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.

The following are examples of accessing page request variables such as get, post, cookies, server, environment and session variables. For example, {$smarty.server.SERVER_NAME} obtains server variables and {$smarty.env.PATH} obtains system environment variables. path,{$smarty.request.username} obtains the composite variable of get/post/cookies/server/env.

{$smarty.now} variable is used to access the current timestamp.
You can use the date_format adjuster to format the output. For example {$smarty.now|date_format:"%Y-%m-%d %H:%M:%S"}
{$smarty.const}

You can access PHP constants directly. For example {$smarty.const._MY_CONST_VAL}
{$smarty.capture}
The output captured through the {capture}..{/capture} structure can be accessed using the {$smarty} variable.
{$smarty.config}
The {$smarty} variable can access the loaded config variable.
For example {$smarty.config.foo} can represent {#foo#}.
{$smarty.section}, {$smarty.foreach}
The {$smarty} variable has access to properties of 'section' and 'foreach' loops.
{$smarty.template}
Displays the name of the currently processed template.
{$smarty.version}
Show version of smarty template
{$smarty.ldelim}
Show left separator
{$smarty.rdelim}
Show right separator

I hope this article will be helpful to everyone’s PHP programming based on smarty templates.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1044860.htmlTechArticleA summary of common methods in smarty, a summary of smarty examples. This article summarizes the common methods in smarty. Share it with everyone for your reference. The details are as follows: 1. The index and i of foreach in Smarty...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template