Examples of common methods in smarty and summary of smarty instances-PHP Tutorial

Source: Internet
Author: User
Tags php session
Examples of common methods in smarty and summary of smarty instances. Examples of common methods in smarty, and examples of smarty summary examples in this article summarize common methods in smarty. Share it with you for your reference. Details are as follows: 1. Summary of common methods in the Smarty foreach index and I smarty, and summary of the smarty instance

This example summarizes common methods in smarty. Share it with you for your reference. The details are as follows:

1. Use of indexes and iteration of foreach in Smarty

. Index contains the current array index, starting from scratch.

Index example

{* The header block is output every five rows *} {* The header block is output once per five lines *}
 
 
  
  
{Foreach from = $ items key = myId item = I name = foo} {if $ smarty. foreach. foo. index % 5 = 0}
  
  {/If} 
   {/Foreach} 
  
Title
{$ I. label}

. Iteration contains the number of current cycles. different from index, the number of cycles increases by 1 from 1.

Iteration and index examples

{* This will output 0 | 1, 1 | 2, 2 | 3 ,... etc *} {* this example will output 0 | 1, 1 | 2, 2 | 3 ,... and so on *} {foreach from = $ myArray item = I name = foo} {$ smarty. foreach. foo. index} | {$ smarty. foreach. foo. iteration}, {/foreach}

2. use of section nesting 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}::::

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 common keywords

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

Name: section name. $ is not required;
$ Loop: the variable to be cyclic. To the extent, use assign to operate the variable.
$ Start: subscript of the start loop. The default value is 0;
$ Step: increment of the lower mark in each cycle;
$ Show: boolean type. Determines whether to display this block. The default value is true;

<{Section}> attributes;

Index: The subscript of the loop. The default value is 0;
Index_prev: the previous value of the current base object. the default value is-1;
Index_next: the next value of the current underlying object. the default value is 1;
First: whether it is the first next loop;
Last: whether it is the last loop;
Iteration: number of cycles;
Rownum: current row number, alias of iteration;
Loop: The Last loop number. Number of cycles of a Section;
Show: whether to display;

<{Section loop = $ News}> News No.: <{$ News [loop]. newID}>
News content: <{$ News [loop]. newTitle}>
<{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

{* Display the page Value ($ _ GET) http://www.example.com/index.php in the URL? Page = foo *}
{$ Smarty. get. page}
{* Display the "page" variable from a form ($ _ POST ['Page']) *}
{$ Smarty. post. page}
{* Display the value of the COOKIE variable "username" ($ _ COOKIE ['username']) *}
{$ Smarty. cookies. username}
{* Display the SERVER variable "SERVER_NAME" ($ _ SERVER ['server _ name']) *}
{$ Smarty. server. SERVER_NAME}
{$ Smarty. server. PHP_SELF}
{$ Smarty. server. SCRIPT_NAME}
{* Display the system environment variable "PATH "*}
{$ Smarty. env. PATH}
{* Display the 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.

The following are examples of access 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 path of the system environment variable {$ smarty. request. username} obtains the compound variable of get/post/cookies/server/env.

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

{$ Smarty. const}
You can directly access PHP constants. for example, {$ smarty. const. _ MY_CONST_VAL}

{$ Smarty. capture}
You can use the {$ smarty} variable to access the output captured by the {capture}... {/capture} structure.

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

For example, {$ smarty. config. foo} indicates {# foo #}.

{$ Smarty. section}, {$ smarty. foreach}
The {$ smarty} variable can be used to access the attributes of the 'core' and 'foreach' loops.
{$ Smarty. template}
Displays the name of the currently processed template.
{$ Smarty. version}
Display the version of the smarty Template
{$ Smarty. ldelim}
Show left separator
{$ Smarty. rdelim}

The show right separator $ smarty} reserved variables can be used to access some special template variables. The following are all page request variables.

The following are examples of access 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 path of the system environment variable {$ smarty. request. username} obtains the compound variable of get/post/cookies/server/env.

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

You can directly access PHP constants. for example, {$ smarty. const. _ MY_CONST_VAL}
{$ Smarty. capture}
You can use the {$ smarty} variable to access the output captured by the {capture}... {/capture} structure.
{$ Smarty. config}
The variable {$ smarty} can access the loaded config variable.
For example, {$ smarty. config. foo} indicates {# foo #}.
{$ Smarty. section}, {$ smarty. foreach}
The {$ smarty} variable can be used to access the attributes of the 'core' and 'foreach' loops.
{$ Smarty. template}
Displays the name of the currently processed template.
{$ Smarty. version}
Display the version of the smarty Template
{$ Smarty. ldelim}
Show left separator
{$ Smarty. rdelim}
Show right separator

I hope this article will help you design php programs based on the smarty template.

Examples in this article summarize common methods in smarty. Share it with you for your reference. Details: 1. the index and I... of foreach in Smarty...

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.