Six smarty tips

Source: Internet
Author: User
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.

1. 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:

View copy print?
  1. {Capture name = test}
  2. {/Capture}
  3. {$ Smarty. capture. test}
{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.

2. config_load label

Config_load can directly read the content in the file, which saves the assign step.

View copy print?
  1. Test.csv:
  2. PageTitle = "config_load_test"
  3. BodyBgColor = "# eeeeee"
  4. Img = "girl.jpg"
  5. Width = "100"
  6. Height = "100"
  7. Index. tpl:
  8. {Config_load file = "test.csv "}
  9. {# PageTitle #}
test.csv:pageTitle = "config_load_test"bodyBgColor = "#eeeeee"img = "girl.jpg"width="100"height="100"index.tpl:{config_load file="test.csv"}{#pageTitle#}

In the above process, if the following problem occurs: Warning: Smarty error: unable to read resource, please verify if your test.csv file is in the configuration directory of smarty. the default configuration directory is configs.

View copy print?
  1. /**
  2. * The directory where config files are located.
  3. *
  4. * @ Var string
  5. */
  6. Var $ config_dir = 'configs ';
    /**     * The directory where config files are located.     *     * @var string     */    var $config_dir      =  'configs';

3. Use of literal labels

For web development, JavaScript and jquery code will inevitably be written. In js and jquery, there will be symbols like {}. will smarty understand it as a php variable? If you do not add the literal label, smarty will certainly understand the variable, and it will not, for example:

View copy print?
  1. {Literal}
  2. Function getAbsLeft (e ){
  3. Var l = e. offsetLeft;
  4. While (e = e. offsetParent) l + = e. offsetLeft;
  5. Return l;
  6. }
  7. Function getAbsTop (e ){
  8. Var t = e. offsetTop;
  9. While (e = e. offsetParent) t + = e. offsetTop;
  10. Return t;
  11. }
  12. {/Literal}
{literal}function getAbsLeft(e){ var l=e.offsetLeft; while(e=e.offsetParent)l+=e.offsetLeft; return l;}   function getAbsTop(e){ var t=e.offsetTop; while(e=e.offsetParent)t+=e.offsetTop; return t;}{/literal}

4. php labels

When you get used to assign, have you ever thought about writing php code directly in the template file? I think sometimes you must think about it. For example:

View copy print?
  1. {Php}
  2. Global $ result;
  3. Foreach ($ result as $ key => $ value ){
  4. Echo "key = $ key, value => $ value
    ";
  5. }
  6. {/Php}
{php}global $result;foreach($result as $key=>$value){echo "key=$key,value=>$value
";}{/php}

5. strip tag

Strip labels remove spaces and carriage return characters from the labels. I think that mobile phone developers must use them, because spaces in the full corner may lead to disorder throughout the page, even a blank page. The mobile phone screen is small, and it is estimated that there is a low possibility of using smarty.

View copy print?
  1. {Strip}
  2. Strip
  3. {/Strip}
{strip} strip{/strip}

6. fetch label

The file_get_contents in the root php of the fetch tag can read the content in the file and is a string.

View copy print?
  1. {Fetch file = "./aaaa.txt" assign = "result "}
  2. {If is_array ($ result )}
  3.  Is array
  4. {Else if}
  5.  Not array
  6. {/If}

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.