Six smarty tips

Source: Internet
Author: User
Tags smarty template

Several ignored but useful labels in the php smarty template engine, including the 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:

{capture name="test"}{/capture}<div class="image">{$smarty.capture.test}</div>

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:

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:

{config_load file="test.csv"}

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:

{php}global $result;foreach($result as $key=>$value){    echo "key=$key,value=>$value<br>";}{/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:

{Strip} <div> <font color = "red"> strip </font> <span> <div> php smarty strip compresses html output, view Source Code at www.phpernote.com </div> </span> </div> {/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:

{fetch file="./aaaa.txt" assign="result"}{if is_array($result)}<b>is array</b>{else if}<b>not array</b>{/if}

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

The usage is as follows:

{$ Smarty. const. Constant name you defined}
Articles you may be interested in
  • Tips for using MVC in php
  • Eight typical Linux operating system applications
  • Smarty variable operator Summary
  • PHP 10 very useful advanced application skills
  • Smarty template retention variable Summary
  • Improve database performance secret SQL optimization skills
  • Smarty logical operator number Summary
  • How to Use variables in smarty include file

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.