Home  >  Article  >  Backend Development  >  How to use php functions in smarty

How to use php functions in smarty

WBOY
WBOYOriginal
2016-07-25 08:55:291077browse
  1. /**
  2. * Use php functions in 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 a';
  11. $str3='Script Academy 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}

  23. < ;/html>
Copy the code

Output results: 16 2

Test: -=-=-=-=this is four

Instructions: Use four variables to handle 1, 2, 3, and 4 unavailable parameters respectively. Here is a record. When calling variables in the template, when there is only one parameter, just {$str1|function name}. When there are two functions When there are three 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,,,parameters, and so on,,,,,,,,,,,,

When programming, I used this:

  1. {$name|str_ireplace:"$name":$arr
Copy code

I would like to share it by the way, friends who are interested can think about it.



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