PHP smarty Template engine 6 tips _php Tips

Source: Internet
Author: User
Tags smarty template

The following article will be analyzed in a specific example:

Capture label

Capture's Chinese meaning is crawl, its function is to crawl the data of the template output, when we need it, call it to get the purpose of fetching data. The following example:

Copy Code code as follows:

{Capture name= "test"}

{/capture}

<div class= "image" >

{$smarty. Capture.test}

</div>

Description: The content between {capture Name= "test"} and {/capture} is stored in the variable $test, which is specified by the Name property. The variable is accessed through $smarty. Capture.test in the template. If the Name property is not specified , the function defaults to use "default" as a parameter, similar to the Clone method in jquery.

Config_load Label

Config_load can read the contents of a file directly so that you can skip the assign step, such as using the following example:

Test.csv file:

Copy Code code as follows:

PageTitle = "Config_load_test"

Bodybgcolor = "#eeeeee"

img = "Girl.jpg"

Width= "100"

height= "100"

The template Index.tpl file can refer to the above Test.csv file as follows:

Copy Code code as follows:

{config_load file= "test.csv"}
<title>{#pageTitle #}</title>
<body bgcolor= "{#bodyBgColor #}" >

</body>

Note: If such a problem arises 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, and the default configuration directory is the Configs directory.

PHP tags

When you get used to assign, have you ever thought about writing PHP code directly in the template file, though it's not recommended, but sometimes it's forced to do so when the business needs to do it? Take a look at the following example:

Copy Code code as follows:
{PHP}

Global $result;

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

echo "key= $key,value=> $value <br>";

}

{/php}

Strip label

Strip label function is to remove the space in the label and enter, I feel this tag is very useful, you can compress the final output of the HTML format, want to see the effect of the words, look at the source code on the site to know, is not cool, hehe:

Copy Code code as follows:

{Strip}

<div>

<font color= "Red" >strip</font>

<span>

<div>php smarty Strip compressed HTML output, www.phpernote.com see source Effect </div>

</span>

</div>

{/strip}



Fetch label

The fetch tag is similar to the function file_get_contents in PHP, and can read the contents of the file and assign the read result as a string to a variable, such as the following use case:

Copy Code code as follows:

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

{if Is_array ($result)}

<b>is array</b>

{Else If}

<b>not array</b>

{/if}


Using constants

Constants defined in PHP using define can be used directly in the Smarty template

Use the following methods:

Copy Code code as follows:

{$smarty. Const. The constant name you define}


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.