The basic syntax of smarty template design for PHP notes

Source: Internet
Author: User
Tags comments html tags numeric types of functions smarty template

1, comments in the template
each smarty template file is developed in conjunction with the syntax of the web foreground language (XHTML, CSS, and JavaScript) combined with the Smarty engine. In addition to adding some smarty syntax in the template to handle program logic, the other web foreground development languages used are the same as the original, and the annotations are unchanged. If you use comments in a foreground language such as HTML or JavaScript in a template file, users can see the comments by browsing the source code of the Web page. Smarty also provides us with a syntax for annotations in the template, surrounded by the bounding mark "{*" and "*}" are the annotation content, can include one or more lines, and will not be seen when the user browses the page source code, it is only the internal annotation of the template. The following is a valid Smarty comment:
{* This is a comment *}                  //Template comments are surrounded by the * number, it will not appear in the final output of the template file
2, templates in the variable application
for PHP programmers, as long as the data is allocated to the template to use, Regardless of the type of data, all use the Assign () method of the Smarty object to complete. Although the syntax for using various types of variables in the template is similar to that of PHP, it is more complex to design various types of data when designing a template for artists who are unfamiliar with PHP syntax. It should be noted that in the Smarty template the variable presets are domain-wide. That means you just have to allocate it once, and if you assign more than two times, the content of the variable will be the final assignment. Even if we load an external child template in the main template, the same variable in the template will be substituted, so that we don't have to do a parse action on the child template. The
use some complex variables in a template
template variable starts with a dollar sign $, which can contain numbers, letters, and line drawings, which is similar to a PHP variable. You can refer to an array of numeric or non-numeric indexes, but you can also refer to object properties and methods.
There are two formats for accessing an associative array in a template, which can refer to an indexed array using PHP native syntax, and you can use a period "." The array key value is followed by a reference to an associative array variable assigned from PHP. Accessing the associative array variable looks like this:

The code is as follows Copy Code

Smarty "dot" syntax

{$Contacts. Fax}
{$Contacts. Email}
{$Contacts. Phone.home}
{$contacts. phone[0]}

PHP-style syntax

{$Contacts [fax]}
{$Contacts [' email ']}
{$contacts [' phone '] [' Home ']}
{$contacts [' phone '][0]}

In general, by traversing each element of the output array in a template, you can do so through a foreach or section statement provided in Smarty, which focuses on an element that outputs an array separately in the template. Using PHP-assigned object variables in a template, you can access the members of the PHP assignment through the '-> ' symbol followed by the specified property name or method, and the way you access the object in PHP is exactly the same as the member. In addition, the object's method chain (coherent operation of the object) is also implemented in Smarty 3.

The code is as follows Copy Code

{$person->name}
{$person->say ()}

The object chain approach introduced in Smarty 3:

The code is as follows Copy Code

{$object->method1 ($x)->method2 ($y)}

To apply an expression in a template
Smarty3 supports expressions in almost every place, and if security policy allows, expressions can even contain PHP functions, object methods and properties, and simple applications like the following:

The code is as follows Copy Code
{$x + $y}
{$foo = strlen ($bar)}
{Assign var = foo value = $x + $y}
{$foo [$x +3]}

Embedded variables in double quotes
A variable embedded in double quotes can be recognized in the Smarty template, as long as the variable contains only numbers, letters, bottom lines, or brackets []. For other symbols (period, object-related, and so on) this variable must be wrapped in two inverted quotes "'" (This symbol and "~" on the same key). Examples that are used are as follows:
{func the variable with an inverted quotation mark when var= ' Test ' $foo. Bar ' Test '}//embedded in an object
Variable variables in the pattern
In Smarty3, the mechanism of PHP variable variable is introduced, that is, the template variable name itself can be an expression. The use of variable variables is as follows:

The code is as follows Copy Code
$foo _{$bar}
$foo _{$x + $y}

3. function application in Template
In template design, there are only two kinds of smarty syntax: One is a variable, the other is a function. The SMARTY3 provides the ability to call PHP's system functions and custom functions directly in the template, but how many PHP functions does the graphic artist know? Although calling functions directly in a template is exactly the same as the form invoked in PHP, it is not recommended.
If you refer to a PHP function directly using the template variable symbol, the function should have a return value. This way if the programmer to develop templates to use is also more appropriate, but if the art to call PHP functions, it is best to press Smarty2 in the way, the first function to register as a Smarty plug-in, so that the function of PHP become smarty tag form, So that the art can be HTML tag syntax format to invoke the PHP function, which is not too familiar with the PHP syntax of the art is still very necessary.
There are three types of functions commonly used in Smarty: function, block function, variable modifier. Let's take a look at two types of smarty functions.
Function
The use of Smarty functions is very similar to HTML independent element tags, smarty function names are the same as HTML tag names, and the arguments passed by the Smarty function are equivalent to the attributes of HTML tags, which is a Smarty function invocation method that is provided for artists familiar with HTML. As shown below:

The code is as follows Copy Code
Use HTML tag formatting in a template
<input type= "text" name= "username" value= "admin" >
Using the format of the Smarty function in a template
{funcname attr1= "Vall" attr2= "Val2" attr3= "Val3"}

The function funcname and its properties within the delimiter "{}" are processed and output, and the function funcname called in the example above can be either smarty built-in functions or custom PHP functions (plug-ins) that exactly match the parameter format passed by the function and the property usage of the HTML tag. Use the example shown below:

The code is as follows Copy Code
{config_load file= "colors.conf"}//Call Smarty built-in config_load function load configuration file colors.conf

{include file= "HEADER.TPL"}//call Smarty built-in include function contains header template file Header.tpl

Block function
Block functions in Smarty are also a form of functions, except that smarty functions are equivalent to HTML independent tags, while smarty block functions are equivalent to HTML closed tag elements. and HTML comparisons are shown below:

The code is as follows Copy Code
Use the format of Smarty block functions in a template
{blockname attr1= "Vall" attr2= "Val2"}
Content
{/blockname}

The Smarty block function needs to end the tag to close the ' {Blockname} ... {/blockname} ", in the execution of the contents of the block back to the function blockname, and combined with the behavior of the property to process and output. The use of Smarty block functions in templates is shown in the following example:

  code is as follows copy code
{nocache}
& nbsp;    {$smarty. Now|date_format}
{/nocache}
 
{if $highlight _name}
        welcome,{$name}!
{Else}
       welcome,{$name}!
{/if}

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.