Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What are the Smarty template functions

Shulou Source: shulou.com Published: 2022-06-01 16:09:57 10月11日 Update

This article is to share with you about the Smarty template function, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article. Let's take a look at it.

Recently, I used Smarty template engine in my work and sorted out some template functions used.

Assume that the delimiter of smarty is {}.

Get the value of server $_ GET from the template:

{$smarty.get. Parameter name}

Get the value of server $_ POST from the template:

{$smarty.post. Parameter name}

Get the value of server $_ REQUEST from the template:

/ / request can get the value of get or post. {$smarty.request. Parameter name}

Get the value of server $_ SESSION from the template:

{$smarty.session. Parameter name}

Get the value of server $_ COOKIE from the template:

{$smarty.cookies. Parameter name}

Get the value of server $_ SERVER from the template:

{$smarty.server. Parameter name}

Convert the timestamp to Y-m-d H:i:s in the template:

{$Parameter name | date_format:'%Y-%m-%d% HRV% MRO% S'}

Convert the string to lowercase in the template:

{$Parameter name | lower}

Convert the string to uppercase in the template:

{$Parameter name | upper}

Convert the first letter of the string to uppercase in the template:

{$Parameter name | capitalize}

Add characters to the template after the string:

{$parameter name | cat:' string'}

Set the default value in the template if the variable is empty or does not exist:

{$parameter name | default:' string'}

The length of the string in the template is more than 20 and is hidden behind. Connection:

{$Parameter name | truncate:23: "...": true}

Convert the HTML in the string to HTML encoding in the template:

{$Parameter name | escape:html}

Convert the HTML in the string to URL encoding in the template:

{$Parameter name | escape:url}

Replace characters with regular expressions in the template:

{$Parameter name | regex_replace: "/ string 1 /": "string 2"}

Use Replace to replace characters in the template:

{$parameter name | replace: "string 1": "string 2"}

Formatted numbers in the template are of type Float (rounding retains two decimal places)

{$Parameter name | string_format: "% .2f"}

The formatted number in the template is of type Int (if there is a decimal place, it will be dropped)

{$Parameter name | string_format: "% d"}

If the {} of js conflicts with the smarty delimiter:

(1) modify the delimiter to (2) to include the JS code between {literal} {/ literal}. These are the Smarty template functions, and the editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Tags: Templates parameters characters strings lieutenant service function delimiter uppercase decimal number method more format knowledge article type coding work practical Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Linux Shulou Information Huawei macOS