Smarty function library 1

Source: Internet
Author: User
Tags foreach php template require reserved smarty template

Smarty function library
What is smarty and its installation

Smarty is a php template engine that separates logical programs from external content and provides an easy-to-manage method.

Smarty requires the web server to run php4.0.6 or later.

The smarty library file is required for smarty installation. You can go to the official website http://smarty.php.net download. I have 2.6.7.

I talked about a lot of installation tutorials online, but I did not succeed. So I directly copied the entire directory name to the directory where the website is located, and then opened
Http: // website path/smarty/demo/index. php. The installation is successful.
Basic syntax

All smarty labels are added with delimiters. in smarty, all contents other than delimiters are static. When smarty encounters a template tag, it will try to explain them and then output them in an appropriate way.

By default, they are {and}, but they are customizable. The custom method is:
$ Smarty-> left_delimiter = '<! --{';
$ Smarty-> right_delimiter = '} --> ';

1. Notes

Template comments are surrounded by "*", for example, {* this is a comment *}
The smarty annotation will not be output. It is an internal annotation of the template.

2. Variables
The template variable starts with $ and can contain numbers, letters, and underscores.
The config_file variable is an exception and must be referenced using the # variable name # method.

3. Functions
The smarty tag outputs a variable or calls a function.
Functions and their attributes in the delimiters will be processed and output. For example:
{Funcname attr1 = "val" attr2 = "val "}.
Funcname indicates the function name, such as include, attr1, attr2 indicates the attribute name, and val indicates the value corresponding to the attribute.

Both built-in and custom functions have the same syntax in the template.
Built-in functions will work within smarty, such as {if}, {section}, and {strip}. They cannot be modified.
UDFs work through the plug-in mechanism. They are additional functions and can be modified at will.
For example, {html_options} and {html_select_date}

4. Attributes
Static values do not require quotation marks, and Boolean values (true or false) do not require quotation marks. They can be true, on, yes, false, off, or no. except string. variables are also used, and they cannot be enclosed in quotation marks.

5. Insert a variable into the string
Only a combination of numbers, letters, underscores, and [] can be recognized. For complex forms, use ''to separate variables. For example, {func var = "test $ foo. the variable $ foo can only be identified in bar test "}, and $ foo must be recognized. bar, which must be written as {func var = "Test' $ foo. bar 'test "} format.

6. Display methods of reserved words
In the template, if you want to display some reserved words of smarty as the content of the language, such as the Display separator, the default method is {}: {ldelim }, {rdelim} or {$ smarty. ldelim}, {$ smarty. rdelim} or put {} in {literal }.. {/literal} intermediate output.
Retention variable of smarty

{$ Smarty} reserved variables can be used to access some special template variables.
The following are all.

Page request variable
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
Variable regulator
The variable regulator is used for variables, user-defined functions, and strings.
You can use the '|' symbol and the regulator name to apply the regulator.
The behavior of a variable regulator is determined by the value of the parameter.
Parameters are separated by the ':' symbol.
If you use the variable regulator to adjust the array variables, the result is that each value of the array is adjusted. If you want the regulator to adjust the entire array, you must add the @ symbol before the regulator name.
Example: {$ articleTitle | @ count} (this will be the number in the output $ articleTitle array)

Capitalize
Capital the first word of all words in the variable. The parameter value is boolean to determine whether a word with a number is capitalized. Not capitalized by default

Count_characters
Calculates the number of characters in the variable value. The parameter value boolean determines whether to calculate the number of spaces. Space is not calculated by default.

Cat
Connect the parameter values in cat to the specified variable. The default value is null.

Count_paragraphs
Calculates the number of paragraphs in a variable.

Count_sentences
Calculates the number of sentences in a variable.

Count_words
Calculate the number of words in a variable

Date_format
Date format

The first parameter controls the date format.
If the data passed to date_format is empty, the second parameter is used as the default time.

% A-the day of the week

% A-full writing of the day of the week

% B-abbreviated month

% B-full write of the month

% C-date and time 06/12/05 11:15:10

% C-century time

% D-number of the month (from 01 to 31)

% D-same as % m/% d/% y

% E-the number of months. If it is a singular number, add a space (from 1 to 31) to the front)

% G-century

% G-century [century, 9999]

% H-same as % B

% H-24 hours (from 00 to 23)

% I-12 hours (from 01 to 12)

% J-the day of the year (from 001 to 366)

% K-24 hours, with a space in front of a single number (from 0 to 23)

% L-12 hours, with a space in front of a single number. (range 1 to 12)

% M-month (range 01 to 12)

% M-minute

% N-linefeed

% P-display 'am' or 'PM 'in the morning or afternoon'

% R-a.m. Or p. m. Format time

% R-24 hour time

% S-second

% T-tab

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.