Smarty Template variable Regulator usage Analysis _php Example

Source: Internet
Author: User
Tags html tags php code php template smarty template

This paper describes the usage of smarty template variable adjuster. Share to everyone for your reference, specific as follows:

In Smarty, how do you modify text and variables? Of course, you can use the PHP function to process text, and then through the Assign () method to assign to the template, in fact, Smarty provides a variable regulator can easily handle the text, the syntax is as follows:

{$var |modifier1|modifier2|modifier3| ...}

Description: 1. No matter how many functions are applied, they should be separated by |.

Note: 2. The parameters are separated by ":", such as:

{$str |truncate:40: "..."}
The length of the intercept string is 40 and ... End.

Variable mediation functions commonly used in Smarty templates

Member Method name

Description

Capitalize

Capitalize the first letter of all words in a variable, and the parameter value is Boolean-determined

Word with number is uppercase, default is not written

Count_characters

Calculates the number of characters in a variable value, and the Boolean value of the parameter determines whether it counts

Count the number of spaces, default does not count spaces

Cat

The parameter value in cat is connected to the given variable, and the default is null.

Count_paragraphs

Calculate the number of paragraphs in a variable

Count_sentences

Calculate the number of sentences in a variable

Count_words

Calculate the number of words in a variable

Date_format

Date format, first parameter control date format, if passed to Date_format

The data is empty, the second parameter will be used as the default time

Default

Set a default value for an empty variable, or when the variable is empty or unassigned,

Replaces output by a given default value

Escape

For HTML transcoding, URL transcoding, conversion of single quotes on variables without transcoding,

Hex transcoding, hex beautification, or JavaScript transcoding.

The default is HTML transcoding

Indent

Indents a string in each line, and the first parameter specifies how many characters to indent,

The default is four characters, and the second parameter specifies what character the indentation replaces

Lower

Lowercase the variable string

Nl2br

All line breaks will be replaced with <BR/> functionality is the same as the NL2BR () function in PHP

Regex_replace

To find and replace regular expressions, you must have two parameters, and parameter 1 is

Change the regular expression, parameter 2 uses what text string to replace

Replace

Simple search and replace strings, must have two parameters, parameter 1 is

The string to be replaced, parameter 2 is the text to replace

Spacify

Inserts a space or other string between each character of the string.

A parameter represents a string that will be inserted between two characters, and the default is a space

String_format

is a method of formatting floating-point numbers, such as a decimal number, using the sprintf

Syntax formatting. Parameters are required to specify the format used. %d

Indicates that an integer is displayed,%.2f indicates that two floating-point numbers are intercepted

Strip

Replaces all duplicate spaces, newline, and tab to a single or specified string.

If there is a parameter, the specified string

Strip_tags

Remove all HTML tags

Truncate

Intercepts a length of characters from the beginning of a string by default of 80

Upper

Change variable to uppercase




The PHP code example is as follows:

Capitalize (first letter capital):

<{$a |capitalize}>

Count_characters (character count):

<{$b |count_characters}>

Cat (Connection string):

<{$c |cat:world}>

count_paragraphs (number of calculated segments)----(line break \ n):

<{$d |count_paragraphs}>

Count_sentences (number of sentences counted):

<{$e |count_sentences}>

Count_words (number of words counted):

<{$f |count_words}>

Date_format (format date):

<{$smarty. Now|date_format: "%y-%m-%d%h:%i:%s"}>

Default value:

<{$biaoti |default: "No Biaoti"}>

Escape (encoding):

<{$dz |escape}>

Indent (first line indent):

<{$in |indent}>
<{$in |indent:1: "T"}>

NL2BR newline character replaced by:

<{$con |nl2br}>

Regex_replace Regular Replacement

<{$h |regex_replace: "/[\r\t\n]/": "<br/>"}>

Replace (replacement)

<{$h |replace: "Hello": "Hi"}>

Remind:

Often in the development of time is not enough to meet the demand, that can directly write these variable regulator, in fact, the so-called variable regulator is a function , here we build a, first in the Plugins folder inside the establishment of a PHP file, naming must be in accordance with his specifications.

modifier.capita.php

function Smarty_modifier_capita ($string) {
//functional naming is also a canonical return
 Strtoupper (substr ($string, 0, 1)). Strtolower (substr ($string, 1));
}

Template file Invocation:

<{$a |capita}>

More interested in smarty related content readers can view the site topics: "Smarty Template Primer Tutorial", "PHP Template Technology Summary", "PHP based on PDO Operation Database Skills summary", "PHP Operations and Operator Usage Summary", "PHP Network Programming Skills Summary", " Introduction to PHP Basic Grammar, "Introduction to PHP object-oriented programming", "PHP string (String) Usage Summary", "Php+mysql Database Operations Tutorial" and "PHP common database Operation Skills Summary"

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.