PHP smarty Template engine Practical tips

Source: Internet
Author: User
Tags smarty template
    1. {Capture name= "test"}
    2. {/capture}
    3. {$smarty. Capture.test}
Copy Code

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

Second, the config_load tag config_load can directly read the contents of the file, this can be omitted assign this step. Example: Test.csv file:

    1. PageTitle = "Config_load_test"
    2. Bodybgcolor = "#eeeeee"
    3. img = "Girl.jpg"
    4. Width= "100"
    5. height= "100"
Copy Code

The above Test.csv file can be referenced in the template Index.tpl file as follows:

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

Note: If such a problem occurs in the above process Warning:smarty error:unable to read resource, please check that your test.csv is not placed in the Smarty configuration directory, the default configuration directory is the Configs directory.

Third, PHP tags

Accustomed to assign, have you ever thought, in the template file directly write PHP code, although it is not recommended to do so, but sometimes forced to do so by the needs of the business need to do so? Take a look at the following example:

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

Four, the role of strip tag strip tag is to remove the space inside the label and enter, I feel this label is still very useful, you can compress the final output of the HTML format, want to see the effect of the words, see the source code on the site to know, is not very cool, hehe:

    1. {Strip}
    2. Strip
    3. PHP smarty Strip compressed HTML output, www.phpernote.com see the source effect
    4. {/strip}
Copy Code

Five, the fetch tag fetch tag and PHP function File_get_contents function is similar, can read the contents of the file, and can be read out in the form of a string to assign to a variable, such as the following use case:

    1. {Fetch file= "./aaaa.txt" assign= "Result"}
    2. {if Is_array ($result)}
    3. is array
    4. {Else If}
    5. Not array
    6. {/if}
Copy Code

VI, using constants

Using define defined constants in PHP can be used directly in the Smarty template: {$smarty. Const. The constant name you defined}

  • Related Article

    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.