Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the tips of php smarty template engine

Shulou Source: shulou.com Published: 2022-06-03 08:06:10 11月15日 Update

This article focuses on "what are the tips of php smarty template engine". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what are the tips of php smarty template engine?"

Capture tag

Capture means grab in Chinese, and its function is to grab the data output from the template. When we need it, we call it to get the purpose of grabbing the data. Examples are as follows:

The copy code is as follows:

{capture name= "test"}

{/ capture}

{$smarty.capture.test}

Description: the content between {capture name= "test"} and {/ capture} is stored in the variable $test, which is specified by the name attribute. Access the variable through $smarty.capture.test in the template. If the name property is not specified, the function defaults to "default" as an argument, similar to the clone method in Jquery.

Config_load tag

Config_load can read the contents of the file directly, thus omitting the step of assign, as shown in the following example:

Test.csv file:

The copy code is as follows:

PageTitle = "config_load_test"

BodyBgColor = "# eeeeee"

Img = "girl.jpg"

Width= "100"

Height= "100"

The above test.csv file can be referenced in the template index.tpl file as follows:

The copy code is as follows:

{config_load file= "test.csv"}

{# pageTitle#}

Note: if there is such a problem in the above process Warning: Smarty error: unable to read resource, please check to see if your test.csv is placed in the smarty configuration directory. The default configuration directory is the configs directory.

Php tag

When you get used to assign, have you ever thought about writing php code directly in the template file, which is not recommended, but sometimes what should you do when you have to do so due to the needs of the business? Look at the following example:

The copy code is as follows:

{php}

Global $result

Foreach ($result as $key= > $value) {

Echo "key=$key,value= > $value

"

}

{/ php}

Strip tag

The function of the strip tag is to remove the spaces in the tag and enter. I think this tag is quite useful. It can compress the final output html format. If you want to see the effect, just take a look at the source code of this site, isn't it cool? hehe:

The copy code is as follows:

{strip}

Strip

Php smarty strip compresses html output, www.phpernote.com to see the effect of source code

{/ strip}

Fetch tag

The fetch tag has the same function as the function file_get_contents in php. It can read the contents of the file and assign the read result to a variable in the form of a string, as shown in the following use case:

The copy code is as follows:

{fetch file= ". / aaaa.txt" assign= "result"}

{if is_array ($result)}

Is array

{else if}

Not array

{/ if}

Use constant

Constants defined with define in php can be used directly in smarty templates

The method of use is as follows:

The copy code is as follows:

{$smarty.const. The constant name you defined}

At this point, I believe you have a deeper understanding of "what are the tips of the php smarty template engine?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Code tags templates files content variables engines techniques constants methods directories outputs functions examples functions attributes effects data time learning Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple OPPO Reno Shulou Tech Info macOS Shulou Information