How to use php functions in smarty

Source: Internet
Author: User
How to use php functions in smarty

  1. /**
  2. * Use php functions in the smarty template engine
  3. * By bbs.it-home.org
  4. */
  5. Require 'libs/Smarty. class. php ';
  6. $ Smarty = new Smarty;
  7. $ Smarty-> compile_check = true;
  8. $ Smarty-> debugging = true;
  9. $ Str1 = 'testtesttesttest ';
  10. $ Str2 = 'This is ';
  11. $ Str3 = 'script school bbs.it-home.org ';
  12. $ Str4 = 'This is four ';
  13. $ Smarty-> assign ('str1', $ str1 );
  14. $ Smarty-> assign ('str2', $ str2 );
  15. $ Smarty-> assign ('str3', $ str3 );
  16. $ Smarty-> assign ('str4', $ str4 );
  17. $ Smarty-> display ('testtest. tpl ');
  18. ?>
  19. {$ Str1 | strlen}

  20. {$ Str2 | strpos: 'Is '}

  21. {'Utf-8' | iconv: 'gb2312 ': $ str3}

  22. {$ Str4 | str_pad: 20: "-=": STR_PAD_LEFT}

Output result: 162

Test:-= this is four

Note: four variables are used to process 1, 2, 3, and 3 parameters that are not allowed. here we record that when the template calls the variables, only one parameter is, directly {$ str1 | function name}. when a function has two parameters, {first parameter | function name: Second parameter}. when there are three parameters, {first parameter | function name: Second parameter: third parameter}. when there are 4, 5, and parameters, and so on ,,,,,,,,,,,,

This is used during programming:

  1. {$ Name | str_ireplace: "$ name": $ arr

If you are interested, you can think about it.

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.