20150210--smarty1-02

Source: Internet
Author: User
Tags smarty template

three , Design articles 1 , Smarty Notes

Basic syntax:

{* Comment content *}

Example code:

2, Smarty the variables in 1) variables allocated from PHP (normal variables, arrays, objects )

Basic syntax:

$smarty->assign ();

Example code:

demo02.php

Demo02.html

Operating effect:

2 ) to read the variable from the configuration file

Basic syntax:

In the HTML static template page, you can invoke the following method:

{config_load file= ' config file name '}

{#配置文件中的标签名称 #}

Example code:

The effect is as follows:

3) Create in template file

Basic syntax:

Defined

{Assign var= ' name ' value= ' value '}

{$name = ' Zhangsan '}

Call

{$name}

Example code:

The effect is as follows:

3. reserved variables in the Smarty

$smarty. Get.page equivalent to $_get[' page ')

$smarty. Post.page equivalent to $_post[' page ')

$smarty. Cookies.username $_cookies[' username ']

$smarty. Server.server_name $SERVER [' server_name ']

$smarty. Env.path gets the path path to the environment variable in the system

$smarty. Session.id $_session[' id ']

$smarty. Request.username $_request[' username ']

{$smarty. Now}: Gets the current system time

{$smarty. Const}: Get Constants in PHP

{$smarty. Capture}: Get capture tag captured content

{$smarty. config}: Read configuration file information {$smarty. Config.title}

{$smarty. Section}: Get section loop information

{$smarty. Template}: Gets the static template name of the current call

{$smarty. Current_dir}: Gets the current path

{$smarty. Version}: Gets the version number of the current Smarty

{$smarty. Ldelim} = {Ldelim}: Gets the left delimiter in the current Smarty

{$smarty. Rdelim} = {Rdelim}: Gets the right delimiter in the current Smarty

Example code:

Demo03.html

Operating effect:

4. Variables Regulator ( variable formatting tool) 1) What is a variable adjuster

A: variable adjuster is mainly to format the variable

Basic syntax:

{variable | regulator: Parameter 1: Parameter 2: ...} 2 ) commonly used variable conditioners

{$var |capitalize}: Capitalize first letter

{$var |count_characters:true}: Calculates the length of a character, the second argument is true: Calculates a space

{$var |count_paragraphs}: Count the number of paragraphs

{$var |count_sentences}: Calculating the number of sentences

{$var |count_words}: Number of words calculated

{$var |cat:var2}: Connection string

{$var |date_format: "%y%m%d"}: Time formatting Y year m D Day h hour m minute s seconds

{$var |default: ' Value '}: Set default value

{$var |escape}: transcoding, mainly for HTML code

{$var |indent:10: "*"}: Character indent

{$var |lower}: Convert to lowercase

{$var |nl2br}: Convert \ n to BR label

{$var |regex_replace: "/[\t\n]/": ""}: Regular replacement

{$var |replace: "AA": "BB"}: Character substitution

{$var |spacify: "^^"}: intervening spaces (between characters and characters intervening spaces)

{$var |string_format: '%d '}: String formatting%s formatted as String%d to Integer

{$var |strip: "*"}: Remove extra spaces

{$var |strip_tags}: Removing HTML tags

{$var |truncate:30: "..."}: Intercept characters

{$var |upper}: Convert to Uppercase

{$var |wordwrap:30: "<br>"}: Line width constraint (only how many characters can be displayed per line)

Example code:

Example 1:

Example 2:

Example 3:

Example 4:

5. Variables Regulator Combination Application

{$var |capitalize|truncate:30: "..." |replace: "AA": "BB"}

A: Capitalize the first letter and intercept 30 characters and use ... and replace the AA in the string with BB 6, built-in function 1)capture

Basic syntax

{Capture name= ' var '}

Content

{/capture}

{$smarty. Capture.var}

Function: Captures a piece of content but does not output this time

Example code:

Operating effect:

2) Config_load

Basic syntax

{config_load file= ' profile name ' section= ' section '}

{#变量名称 #}

{$smarty. config. Variable name}

Example code:

Operating effect:

3) foreach

Basic syntax:

Foreach,foreachelse

{foreach from= array

key= Key

Name= Name

item= content}

{Foreachelse}

{/foreach}

Function: Implements the traversal output of an array (similar to foreach)

Parameter description:

From: Array to traverse

Item: The system automatically saves the traversed results to the item option each time it is traversed

Name: Name the Foreach Loop

Key: Keys for saving loops

Foreachelse: When the array to be traversed is empty, the system enters the current statement segment

Example 1: Traversing a one-dimensional array

Sample code

Effect:

Example 2: Two-dimensional array traversal

Example 3: Use of key value key

Example 4:foreachelse

Description: When the array is empty, the system automatically turns to the Foreachelse statement

Additional Properties in foreach

$smarty. Foreach.name.index @index Get the current loop index

$smarty. Foreach.name.iteration @iteration The first cycles (iterations)

$smarty. Foreach.name.first @first when the first loop, the condition is true

$smarty. Foreach.name.last @last when the last loop, the condition is true

$smarty. foreach.name.total @total total number of times to get loops

Example 5: Parameter name demo

The effect is as follows:

Example 6: Achieving an interlaced color effect

Example code:

Operating effect:

4) Include

Basic syntax

{include file= "to include the template file name" assign= "var" [var ...]}

Features: Include template files

Parameter description:

File: The name of the template files to include

Variable name = form of variable value

Schematic diagram:

For our project: because the head and tail of the project are mostly the same, so we can separate the whole project, we just need to do the middle part.

Example code:

Operating effect:

5) include_php

Basic syntax:

{include_php file= "php file path" Once=true}

Features: Introduction of external PHP files

Once: The specified number of citations is 1, boolean,true: The current file can only be introduced once, the default is True

Note: This label is deprecated in smarty3.0 and can be used if you want to use the Smarty2 portal file

6. Insert

Basic syntax

{Insert Name= ' function name ' variable = value of variable}

Function: allows us to introduce PHP functions directly on the Smarty template page

Example code:

demo10.php Code

demo10.html Code

Note: When using the Insert method, be aware that the function you create must precede the Insert_ prefix, and that the passed arguments are appended to the function's formal parameters as an array.

Job: Complete the paging program via Php+smarty

20150210--smarty1-02

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.