Home  >  Article  >  Backend Development  >  Practical tips for php smarty template engine

Practical tips for php smarty template engine

WBOY
WBOYOriginal
2016-07-25 08:53:30820browse
  1. {capture name="test"}
  2. {/capture}
  3. {$smarty.capture.test }
Copy code

Explanation: The content between {capture name="test"} and {/capture} is stored in the variable $test, which is specified by the name attribute. Access this variable through $smarty.capture.test in the template. If the name attribute is not specified, the function will use "default" as a parameter by default, which is similar to the clone method in Jquery.

Second, config_load tag config_load can directly read the contents of the file, thus eliminating the assign step. example: test.csv file:

  1. pageTitle = "config_load_test"
  2. bodyBgColor = "#eeeeee"
  3. img = "girl.jpg"
  4. width="100"
  5. height="100"
Copy the code

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

  1. {config_load file="test.csv"}
  2. {#pageTitle#}
Copy the code

Note: If a problem like Warning: Smarty error: unable to read resource occurs during the above process, please check whether your test.csv is placed in the smarty configuration directory. The default configuration directory is the configs directory. .

Three, php tag

After getting used to assigning, have you ever thought about writing PHP code directly in the template file? Although this is not recommended, what should you do when you have to do this due to business needs? Take a look at this example:

  1. {php}
  2. global $result;
  3. foreach($result as $key=>$value){
  4. echo "key=$key,value=>$value
    ";
  5. }
  6. {/php}
Copy code

Four, strip tag The function of the strip tag is to remove spaces and carriage returns within tags. I personally feel that this tag is quite useful. It can compress the final output HTML format. If you want to see the effect, just look at the source code of this site. Yes Not cool, hehe:

  1. {strip}
  2. strip
  3. php smarty strip compressed html output, www.phpernote .com See the source code effect
  • {/strip}
  • Copy the code

    Five, fetch tag The fetch tag has similar functions to the function file_get_contents in PHP. Both can read the contents of the file, and the read result can be assigned to a variable in the form of a string, as shown in the following use case:

    1. {fetch file="./aaaa.txt" assign="result"}
    2. {if is_array($result)}
    3. is array
    4. {else if}
    5. < ;b>not array
    6. {/if}
    Copy code

    Six, use constants

    Constants defined using define in php can be used directly in smarty templates Instructions: {$smarty.const.The constant name you defined}



    Statement:
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn