PHP template engine Smarty built-in functions and smarty Functions

Source: Internet
Author: User
Tags echo date php define php template variable scope smarty template

PHP template engine Smarty built-in functions and smarty Functions

This example describes the built-in functions of the PHP template engine Smarty. We will share this with you for your reference. The details are as follows:

Smarty's built-in functions: Smarty comes with some built-in functions, which are part of the template language. You cannot create custom functions with the same name as built-in functions or modify built-in functions.

The following describes the built-in functions in Smarty and provides examples:

The Smarty template engine initialization file init. inc. php and the main file index. php used in the instance

Init. inc. php

<? Php define ('root _ path', dirname (_ FILE _); // set the website ROOT directory require ROOT_PATH. '/libs/Smarty. class. php '; // load the Smarty template engine $ _ tpl = new Smarty (); // create an instance object $ _ tpl-> template_dir = ROOT_PATH. '/tpl/'; // specify the template directory $ _ tpl-> compile_dir = ROOT_PATH. '. /com/'; // specify the compilation directory $ _ tpl-> left_delimiter =' <{'; // re-specify the left delimiter $ _ tpl-> right_delimiter = '}>'; // re-specify the right delimiter?>

Index. php

<? Php require 'init. inc. php'; // introduce the template initialization file global $ _ tpl; $ _ tpl-> display ('index. tpl '); // introduce the template?>

1. capture

Attribute Type Required? Default Value Description
Name String No Default Data collection region name
Assign String No N/ Where is the data collection Region allocated to the variable name? [Pending]

/Tpl/index. tpl

<Html> 

2. config_load

Attribute Type Required? Default Value Description
File String Yes N/ Name of the configuration file to be included
Section String No N/ Name of the part to be loaded in the configuration file
Scope String No Local The data loading scope. The value must be local, parent, or global. local indicates that the variable scope is the current template. parent indicates that the variable scope is the current template and the parent template of the current template (call the template of the current template ). global indicates that the scope of the variable is all templates.
Global Boolean No No Indicates whether the loaded variables are globally visible, which is equivalent to scope = parent. Note: This attribute can be set when the scope attribute is specified, but this attribute is ignored in the template and the scope attribute prevails.
The config_load function is used to load variables from the configuration file. For more information about how to use the config_load function, see the previous example of using the PHP template engine Smarty configuration file in template variables.

3. include

Attribute Type Required? Default Value Description
File String Yes N/ Template file name to be included
Assign String No N/ This attribute specifies a variable to save the output of the template to be included
[Var...] [Var type] No N/ The local parameter passed to the template to be included. It is valid only in the template to be included.

The include function is used to include other templates in the current template. the variables in the current template are available in the included template. You must specify the file attribute, which specifies the template resource location. If the assign attribute is set, the variable name corresponding to this attribute is used to save the output of the template to be included, so that the output of the template to be included will not be displayed directly. See the following example:

/Tpl/index. tpl

{include file="header.tpl"}{* body of template goes here *}{include file="footer.tpl"}

4. if, elseif, else

The if statement in Smarty is as flexible and easy to use as the if statement in php, and several features are added to fit the template engine. if must appear in/if pairs. else and elseif clauses can be used.

You can use the following conditional modifiers: eq, ne, neq, gt, lt, lte, le, gte, ge, is even, is odd, is not even, is not odd, not, mod, div by, even by, odd by, = ,! =,>, <, <=,> =. When using these modifiers, they must be opened with an empty lattice with variables or constants.

The meanings of these modifiers are described below:

Condition Modifier Role description
Eq =
Ne ! =
Neq ! =
Gt >
Lt <
Lte <=
Le <=
Gte > =
Ge > =
Is even Even number
Is odd Odd or not
Is not even Is it not an even number?
Is not odd Is it not an odd number?
Not ! =
Mod Modulo
Div Can be divisible?
Even Whether the quotient is an even number
Odd Whether the operator is an odd number
&& And
| Or
() Parentheses change priority

5. ldelim and rdelim

The output separator, that is, braces "{" and "}". The template engine always tries to explain the content in braces. Therefore, if you need to output braces, use this method. See the following example:

/Tpl/index. tpl

<Html> 

6. literal

Data in the literal label area is processed as text, and all characters in the template are ignored. this feature is used to display javascript scripts that may contain characters such as braces. when the information is in the {literal} {/literal} tag, the template engine directly displays the information without analyzing them. In fact, according to the label style in all my examples (because in init. inc. the left and right delimiters have been reset in the php initialization file), rather than the default style of Smarty. This is basically not the case. For how to use this function, see the following example.

/Tpl/index. tpl

7. php

The php tag allows you to directly embed a php script in the template. The tag uses the content inside the tag as a PHP script for parsing and execution. See the following example.

/Tpl/index. tpl

<Html> 

8. strip

Web developers often encounter situations where spaces and carriage return affect HTML output. To get specific results, you have to run all the labels in the template. this problem is often encountered in difficult-to-understand or difficult-to-handle templates. Before the display, Smarty removes spaces at the beginning and end of the data in the {strip} {/strip} tag and returns the carriage return. This ensures that the template is easy to understand and does not have to worry about unnecessary spaces.

All right, there are so many built-in functions in the Smarty template engine. for the use of the two most important functions (foreach, foreachelse, section, and sectionelse) in the built-in functions, see the previous article titled Usage Analysis of the Smarty built-in functions foreach and foreachelse of the PHP template engine

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.