Detailed Explanation of Smarty Template Syntax

  • 2021-12-13 07:34:28
  • OfStack

This article illustrates the Smarty template syntax. Share it for your reference, as follows:

All template labels are surrounded by separators, which default to "{" and "}".

Template comments

Template comments are surrounded by asterisks, and asterisks on both sides are surrounded by delimiters "{}", such as {smarty *}

Unlike smarty comments, which do not appear in the final output of the template file, html comments are visible in the page source code, while smarty comments cannot.

The following simple code slice {smarty} is not visible in the source code, but the source code is visible.


<html>
<head>
<tltle>smarty</tltle>
<body>
{*smarty*}
smarty
<!--smarty-->
</body>
</head>
</html>

Variable preliminary

Template variables start with a dollar sign and can contain numbers letters and underscores much like php. (Note: The profile variable is a variable that starts without a dollar sign and can contain numbers, letters, and underscores, much like php. (Note: The profile variable is a variable that does not use a dollar sign but is surrounded by a # (# hashmarks #), or a variable in the form of $smarty. config.)

config is not explained here, but the following are examples of variables:


{$a+$b}      // The simplest way to deal with variables 
{$a={$b}+1}   // Label nested label 
{$array[$a]}  // Variable for array index 
{$assign var=fun value=$a+$b}  // Variable assignment 
{$fun="{a}"}  // Quotation marks apply labels 
{$assign var=fun value=[1,2,3]}
{$assign var=fun value=[1,2,[3,4]]}
{$assign var=fun value=[1=>'one',2=>'tow']}    // Array definition 
{$fun=stlen($a)}
{fun=function($a,$b)}     // Used as a function parameter 
{$array.a=1}
{$array.a.b.c($array[a][b][c])=1}  // Array index assignment 
{$object->method1($x)->method2($y)}   // Object chain 

Insert variables in double quotation marks

1. Smarty can recognize variables embedded in double quotation marks as long as the variable contains only numbers, letters, underscores, and brackets [].
2. For symbols such as periods, arrays, and object references, this variable must be enclosed in two reverse quotation marks.
3. Smarty3 adds double quotation marks to support Smarty tags. This is useful in situations where you need to include adjuster variables, plug-ins, and php function return values.


{function var="test $foo test"}       //  Recognition  $foo
{func var="test $foo_bar test"}       //  Recognition  $foo_bar
{func var="test `$foo[0]` test"}     //  Recognition  $foo[0]
{func var="test `$foo[bar]` test"}   //  Recognition  $foo[bar]
{func var="test $foo.bar test"}       //  Recognition   $foo ( Unrecognized  $foo.bar)
{func var="test `$foo.bar` test"}    //  Recognition  $foo.bar
{func var="test `$foo.bar` test"|escape} // Regulator outside quotation marks 
{func var="test {$foo|escape} test"}   //  The regulator is in quotation marks 
{func var="test {time()} test"}        // PHP  Function recognition 
{func var="test {counter} test"}      // Label recognition 

Function

Every smarty tag outputs one variable or calls some kind of function. Within the delimiter, the function (enclosed by the 1-like delimiter '{}') and its properties (also within the delimiter) will be processed and output. For example: {function (function name) attr1 (variable name) = "val" (variable value) attr2= "val"}.


{config_load file="colors.conf"}
{include file="header.tpl"}
{if $highlight_name}
  Welcome, <font color="{#fontColor#}">{$name}!</font>
{else}
  Welcome, {$name}!
{/if}
{include file="footer.tpl"}

1. Both built-in functions and custom functions have the same syntax in templates.
2. Built-in functions will work inside smarty, such as {if}, {section}, and {strip}, and cannot be modified.
3. Custom functions work through plug-in mechanism, and they are additional functions. As long as you like, you can modify it at will, or you can add it yourself. Such as {html_options} and {html_select_date}.

Attribute

Most functions have their own properties to specify or modify their behavior, and the properties of smarty functions are much like those in HTML. Quotation marks are not required for static values, but quotation marks are recommended for strings. You can use ordinary smarty variables or variables with adjusters as attribute values, and they don't need quotation marks. You can even use php function return values and complex expressions as attribute values.

Math

Mathematical operations can be directly applied to variable values.


{$foo+1}
{$foo*$bar}
{$foo|truncate:"`$fooTruncCount/$barTruncFactor-1`"}
{assign var="foo" value="`$foo+$bar`"}

Ignore smarty syntax

It is necessary to ignore the parsing of some statement segments by Smarty. A typical case is javascript or Css code embedded in a template. The reason is that these languages use symbols similar to the Smarty default delimiters' {'and'} '1.

Method: A good habit to avoid this is to separate your javascript/css code into a separate file and link it to the template using the html method. Doing so also helps browsers cache scripts. If you want to embed Smarty variables and methods into javascript/css, see the following application.

In the Smarty template, if the '{' and '}' curly braces contain spaces, the entire {} content will be ignored. You can set the Smarty class variable $auto_literal=false to cancel this rule.


<script>
// the following braces are ignored by Smarty  The contents of the following curly braces will be Smarty Ignore 
// since they are surrounded by whitespace  Because they have spaces in them 
function foobar {
alert('foobar!');
}
// this one will need literal escapement  The following remains literal output 
{literal}
function bazzy {alert('foobar!');}
{/literal}
</script>

{literal}…{/literal} Block is used to ignore the parsing of template syntax, and you can also use {idelim} , {rdelim} Label or {smarty.Idelim} , {smarty.Idelim} , {smarty.rdelim} Variable to ignore individual curly braces (translation: the latter two methods are mainly used to output left and right curly braces in the template).

For more readers interested in Smarty, please check the topic of this site: "smarty template introduction basic tutorial", "PHP template technology summary", "PHP database operation skills summary based on pdo", "PHP operation and operator usage summary", "PHP network programming skills summary", "PHP basic syntax introduction tutorial", "php object-oriented programming introduction tutorial", "php string (string) usage summary", "php+mysql database operation introduction tutorial" and "php common database operation skills summary"

Hope that this article is based on smarty template PHP programming help.


Related articles: