"Smarty Project source Code" simulation of Smarty template file parsing process

Source: Internet
Author: User
Tags smarty template

[PHP]View Plaincopy
  1. <?php
  2. Class myminismarty{
  3. //template file storage path
  4. var $template _dir="./templates/";
  5. //Compile file, the name format of the compiled file is tentatively: Com_ corresponding tpl.php
  6. var $complie _dir="./templates_c";
  7. //Template variable array: Store values for all template variables
  8. var $tpl _vars=Array ();
  9. //Here we mainly simulate two methods
  10. //Parameter 1-template variable parameter 2: value of template variable
  11. function Assign ($tpl _var,$val =null) {
  12. if ($tpl _var!=") {
  13. $this->tpl_vars[$tpl _var]=$var;
  14. }
  15. }
  16. //write display here
  17. //Parameter 1-template filename to display
  18. function Display ($tpl _file) {
  19. path to//template file
  20. $tpl _file_path=$this->template_dir.$tpl _file;
  21. //Naming and path of compiled files
  22. $complie _file_path=$this->complie_dir."Com_". $tpl _file.  ". php";
  23. //Determine if the current template file exists
  24. if (! File_exists ($tpl _file_path)) {
  25. //returns False if the current template file does not exist
  26. return false;
  27. }
  28. ///See if there are any compiled files, if there is no compiled file, or if the template file is modified more than the build time of the compiled file, you need to recompile
  29. if (! File_exists ($comlie _file_path) | | filemtime ($tpl _file_path) >filemtime ($complie _file_path)) {
  30. //Get the contents of a template file
  31. $tpl _file_content=file_get_contents ($tpl _file_path);
  32. //Here Our core is how to convert TPL to PHP file
  33. $pattern =Array (
  34. //1.\{-Escape {Opening parenthesis 2.\s*-represents one or more spaces 3.\$-escape $ symbol 4.\}-escape} closing parenthesis
  35. '/\{\s*\$ ([a-za-z][a-za-z0-9]*) \s*\}/i '
  36. );
  37. $replace =Array (
  38. ' <?php echo $this->tpl_vars["${1}"]?> '
  39. );
  40. //replace similar {$title} with <?php echo $this->tpl_vars["title"]?, return the replaced string
  41. $new _str=preg_replace ($pattern,$replace,$tpl _file_content);
  42. //Compile file generation: Writes the contents of the regular replacement template file to the compiled file
  43. file_put_contents ($complie _file_path,$new _str);
  44. }
  45. //If there is a compilation file and the template file is not changed, the compilation file is introduced directly
  46. include $complie _file_path;
  47. }
  48. }
  49. ?>


Note: Objects can also be used as template variables

Case:

PHP Files:

$smarty =new smarty ();
$var 1 = "simple string";
$var 2=new object name ();
$var 3=array (Table of contents:);
$smarty->assign ("Var1", $var 1);
$smarty->assign ("Var2", $var 2);
$smarty->assign ("Var3", $var 3);

Template files:

Remove the Var1 value: {$var 1}<br/>
Remove the Var2 value: {$var 2-> property or method}
Remove Var3 value: {$var 3[subscript]}

"Smarty Project source Code" simulation of Smarty template file parsing process

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.