PHP template engine Smarty built-in functions

Source: Internet
Author: User
Tags php define php template smarty template
This article mainly introduces the usage of the PHP template engine Smarty built-in functions, and analyzes common built-in functions, definitions, and usage of smarty in the form of instances, for more information about the Smarty built-in functions of the PHP template engine, see the example in this article. 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

 Capture   
 <{Capture name = "foo"}> the content in the capture function is not displayed by default. <{/Capture}>
 <{$ Smarty. capture. foo}>

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, p 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
P 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

 Ldelim and rdelim<{Ldelim}> funcname <{rdelim}> is a function in Smarty.
 

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

 Literal<{Literal}>

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.