PHP template engine Smarty built-in functions, detailed smarty functions _php tutorials

Source: Internet
Author: User
Tags echo date modifiers php template smarty template

PHP template engine Smarty built-in functions, detailed smarty functions


This example describes the PHP template engine smarty built-in functions. Share to everyone for your reference, as follows:

Smarty built-in functions: Smarty comes with built-in functions, which are part of the template language, and users cannot create custom functions with the same name and built-in functions, or modify the built-in functions.

The following is a description of the built-in functions in Smarty and examples:

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

init.inc.php

<?php  define (' Root_path ', DirName (__file__));//Set site root directory  require Root_path. ' /libs/smarty.class.php '; Load Smarty template engine  $_tpl = new Smarty ();//Create an Instance object  $_tpl->template_dir = Root_path. ' /tpl/'; Re-specify the template directory  $_tpl->compile_dir = Root_path. /com/'; Re-specify the compilation directory  $_tpl->left_delimiter = ' <{';//Reassign left delimiter  $_tpl->right_delimiter = '}> ';//reassign right delimiter? >

index.php

<?php  require ' init.inc.php ';//introduction of template Initialization file  global $_tpl;  $_tpl->display (' Index.tpl '); Introducing Template?>

1. Capture

Properties type whether you must Default Value Description
Name String No Default Data Acquisition Area Name
Assign String No N/A Where is the data acquisition area assigned to the variable name[be deciphered]

/tpl/index.tpl

 
  Capture   
 
     <{capture name= "foo"}>     here is the contents of the capture function, which is not displayed by default.   
 
   <{/capture}>   <{$smarty .capture.foo}>

2, Config_load

Properties type whether you must Default Value Description
File String Yes N/A The name of the configuration file to include
Section String No N/A Name of the section to be loaded in the configuration file
Scope String No Local The scope of the load data must be local, parent, or global. Local indicates that the variable is scoped to the current template. The parent indicates that the variable is scoped to the current template and to the parent template of the current template (the template that invokes the current template). Global indicates that the variable is scoped to all templates.
Global Boolean No No Indicates whether the loaded variable is globally visible, equivalent to Scope=parent. Note: When the scope property is specified, the property can be set, but the template ignores the property value and is subject to the scope property.
The Config_load function is used to load variables from the configuration file, and for the use of the Config_load function, refer to the example of how the configuration file of the PHP template engine smarty used in template variables in the previous article.

3. Include

Properties type whether you must Default Value Description
File String Yes N/A Template file name to include
Assign String No N/A This property specifies a variable to hold the output of the template to be included
[var ...] [Var type] No N/A Local parameters passed to the template to be included, only valid in the template to be included

The Include function is used to include other templates in the current template, and the variables in the current template are available in the included template. You must specify the file property, which indicates the location of the template resource. If the Assign property is set, the variable name of the property is used to hold the output of the template to be included so that the output of the template to be included is not displayed directly. Take a look at 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 adds several features to fit the template engine. The if must appear in pairs of/if. You can use the else and elseif clauses.

The following conditions can be used to modify the word: 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, you must use a null to open the variable or constant.

The meanings of these modifiers are described below:

Conditional modifiers Function description
eq ==
Ne !=
Neq !=
Gt >
Lt <
Lte <=
Le <=
Gte >=
Ge >=
is even Whether even
is odd is odd
is not even is not an even number
is not odd is not an odd number
Not !=
MoD Die-finding
Div by Whether it can be divisible
Even by Whether the quotient is even
Odd by Whether the quotient is odd
&& And
|| Or
() Parentheses Change Precedence

5, Ldelim and Rdelim

Used for the output delimiter, which is the curly brace "{" and "}". The template engine always tries to interpret the contents in curly braces, so use this method if you need to output curly braces. Take a look at the following example:

/tpl/index.tpl

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

6, literal

The data in the literal label area is treated as text, and the template ignores all character information inside it. This feature is used to display JavaScript scripts that may contain character information, such as curly braces. When this information is in the {literal}{/literal} tab, the template engine will not parse them, but directly display them, actually follow the label style in all my examples (because the left delimiter and the right delimiter have been reset in the init.inc.php initialization file) instead of The default style of Smarty, which basically does not produce this situation. For the use of this function, see the following example

/tpl/index.tpl

 
  Literal  <{literal}>    <{/literal}>

7. php

PHP tags allow you to embed a PHP script directly in the template, which parses the contents of the tag as a PHP script. Take a look at the following example

/tpl/index.tpl

 
  Php  <{php}>    echo Date ("y-m-d h:i:s");  <{/php}>  
 
  

8, strip

Web developers repeatedly encounter spaces and carriage returns that affect the HTML output, so that you have to run all the tags in the template in order to get specific results. This problem is typically encountered in difficult-to-understand or difficult-to-handle templates. Smarty the first and last spaces and carriage returns of the data in the {Strip}{/strip} tag before the display. This ensures that the template is easy to understand and does not cause problems with unnecessary whitespace.

Well, Smarty the built-in function in the template engine first summarizes so much, the use of the two most important functions in the built-in function (Foreach,foreachelse, section,sectionelse) can refer to the previous article " PHP template engine smarty Foreach,foreachelse usage analysis of built-in functions

More about PHP related content readers can view the topic: "Smarty Template Primer Basic Tutorial", "PHP Template Technology Summary", "PHP based on PDO Operation Database Skills summary", "PHP operation and operator Usage Summary", "PHP Network Programming Skills Summary", " PHP Basic Grammar Introductory tutorial, PHP Object-oriented Programming primer, PHP string usage Summary, PHP+MYSQL database Operations Primer and PHP Common database operations Tips Summary

It is hoped that this article will be helpful to everyone based on smarty template PHP program design.

Articles you may be interested in:

    • PHP template engine Smarty built-in variable mediator usage
    • PHP template engine smarty custom variable mediator usage
    • Analysis of the usage of reserved variables in PHP template engine Smarty
    • PHP template engine Smarty built-in function foreach,foreachelse usage analysis
    • PHP template Engine Smarty Example of how a configuration file is used in template variables
    • Example of how variables are used in PHP template engine Smarty
    • Smarty how the template engine obtains data from PHP
    • thinkphp How to use the Smarty template engine
    • A method of generating random numbers in the PHP template engine smarty and an explanation of the math function
    • Summary of cache Usage for PHP template engine Smarty
    • 6 Tips for PHP smarty template engine
    • [PHP] Template engine Smarty Introduction
    • PHP template engine Smarty built-in functions section,sectionelse usage

http://www.bkjia.com/PHPjc/1119971.html www.bkjia.com true http://www.bkjia.com/PHPjc/1119971.html techarticle PHP template engine smarty built-in functions, smarty functions in detail This article describes the PHP template engine smarty built-in functions. Share to everyone for your reference, as follows: Smarty built -in ...

  • 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.