Six tips for the phpsmarty template engine

Source: Internet
Author: User
Tags smarty template
Several ignored but useful tag sorting methods in the phpsmarty template engine, including capture tag, config_load tag, php tag, strip tag, and fetch tag, and how to directly use the constants defined by define in the smarty template. The following describes the specific examples:

Capture Tag

Capture means capturing in Chinese. its function is to capture the data output from the template. when we need it, call it to get the purpose of capturing data. Example:

The code is as follows:


{Capture name = "test "}

{/Capture}

{$ Smarty. capture. test}

Note: content between {capture name = "test"} and {/capture} is stored in the variable $ test, which is specified by the name attribute. in the template, use $ smarty. capture. test accesses this variable. if the name attribute is not specified, the function uses "default" as the parameter by default, which is similar to the clone method in Jquery.

Config_load label

Config_load can directly read the content in the file, which saves the assign step, as shown in the following example:

Test.csv file:

The code is as follows:


PageTitle = "config_load_test"

BodyBgColor = "# eeeeee"

Img = "girl.jpg"

Width = "100"

Height = "100"

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

The code is as follows:


{Config_load file = "test.csv "}

{# PageTitle #}



Note: if such a problem occurs during the above process, Warning: Smarty error: unable to read resource. Please verify if your test.csv file is in the smarty configuration Directory. the default configuration directory is the configs directory.

Php labels

When you get used to assign, have you ever thought about writing php code directly in the template file? although this is not recommended, but sometimes I have to do this when I have to do this due to business needs? Let's take a look at the example below:

The code is as follows:

{Php}

Global $ result;

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

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

}

{/Php}

Strip tag

The strip tag removes spaces and carriage returns from the tag. I think this tag is useful and can be compressed to the final html format. if you want to see the effect, check the source code of this site to see if it is cool:

The code is as follows:


{Strip}

Strip

Php smarty strip compression html output, www.phpernote.com to view the source code effect

{/Strip}



Fetch label

The fetch tag is similar to the file_get_contents function in php. you can read the content in the file and assign the read result to a variable in the form of a string. The following is an example:

The code is as follows:


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

{If is_array ($ result )}

Is array

{Else if}

Not array

{/If}


Use constants

Constants defined by define can be directly used in the smarty template in php.

The usage is as follows:

The code is as follows:


{$ Smarty. const. constant name you defined}

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.