PHP template Engine Smarty Internal function detailed _php example

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

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

Smarty built-in functions: Smarty with some built-in functions, built-in functions are part of the template language, users can not create names and built-in functions like the same custom functions, nor can modify the built-in functions.

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

Smarty template engine initialization file init.inc.php and master files used in the instance index.php

init.inc.php

<?php
  define (' Root_path ', DirName (__file__));//Set the 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 template directory
  $_tpl->compile_dir = Root_path. '. /com/'; Re-specify the compilation directory
  $_tpl->left_delimiter = ' <{';//re-assign left delimiter
  $_tpl->right_delimiter = '}> ';//re-assign right delimiter
?>

index.php

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

1, capture

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

/tpl/index.tpl

 
 

2, Config_load

Property type whether you must Default Value Description
File String Yes N/A Name of the configuration file to include
Section String No N/A Name of the part to be loaded in the configuration file
Scope String No Local The scope for loading data must be local, parent, or global. Local description The scope of the variable is the current template. Parent describes the scope of the variable as the current template and the parent template of the current template (the template that invokes the current template). Global describes the scope of the variable as 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, whichever is the scope attribute.
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 previous example of how the PHP template engine smarty configuration file is used in template variables.

3, include

Property 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 A local parameter passed to the template to be included, valid only 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 corresponding to the property is used to save the output of the template to be included, so that the output to include the template 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. If must appear in pairs of/if. You can use the else and elseif clauses.

You can use the following conditions to modify words: 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, = =,!=, >, <, <=, >=. You must use these modifiers with a variable or constant in an empty space.

The following are descriptions of the meanings that these modifiers represent:

Condition modifiers Function description
eq ==
Ne !=
Neq !=
Gt >
Lt <
Lte <=
Le <=
Gte >=
Ge >=
is even Whether even
is odd Whether Odd
is not even is not an even number
is not odd is not an odd number
Not !=
MoD Model 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 output delimiters, which are curly braces "{" and "}". The template engine always tries to interpret the contents of curly braces, so use this method if you need to output curly braces. Take a look at the following example:

/tpl/index.tpl

 
 

6, literal

The data in the literal label area will be treated as text, and the template will ignore all the character information inside it. This attribute is used to display JavaScript scripts that might contain character information such as curly braces. When this information is in the {literal}{/literal} tab, the template engine will not parse them and display it directly, in fact, according to the label style in all my examples (since the left and right delimiters have been reset in the init.inc.php initialization file) instead of The default style of Smarty is basically not the case. For the use of this function, consider the following example

/tpl/index.tpl

 
 

7, PHP

The PHP tag allows you to embed the PHP script directly in the template, which will parse the contents of the tag inside as a PHP script. Take a look at the following example

/tpl/index.tpl

 
 

8, strip

WEB developers encounter multiple occurrences of spaces and carriage returns that affect HTML output, so you have to run all the tags in the template to get a specific result. This problem is often encountered in templates that are difficult to understand or are difficult to handle. Smarty removes the trailing space and carriage return of any data in the {Strip}{/strip} tag before it is displayed. This ensures that the template is easy to understand and does not have to worry about extra spaces causing the problem.

Well, the built-in functions in the Smarty template engine are summed up so much that the use of two of the most important functions in the built-in function (Foreach,foreachelse, section,sectionelse) can be referenced in the previous article " PHP template engine smarty built-in function Foreach,foreachelse usage analysis "

More about PHP Interested readers can view the site topics: "Smarty Template Primer Tutorial", "PHP Template Technology Summary", "PHP based on PDO Operation Database Skills summary", "PHP operation 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.