当前位置:编程学习 > 网站相关 >>

解析访问Smarty模板的过程

Smarty under the Hood
This section will clear any doubts you may have regarding the actual working of Smarty templates. Whenever the Smarty engine is called to render a template file, it compiles the template into native PHP code for the first time. Let's dissect a compiled template and take a detailed look at the template itself.
profiles.tpl:
1. <body>
2. {foreach item=student from=$students}
3. <table width="80%" border="1" cellspacing="0" cellpadding="4">
4. <tr>
5. <td width="38%" bgcolor="#CCCCCC" ><strong>Profile of {$student.nick}</strong></td>
6. <td width="62%"> </td>
7. </tr>
8. <tr>
9. <td>Name : </td>
10. <td>{$student.name}</td>
11. </tr>
12. <tr>
13. <td>Born</td>
14. <td>{$student.born}</td>
15. </tr>
16. <tr>
17. <td>Blood Group </td>
18. <td>{$student.blood_grp}</td>
19. </tr>
20. <tr>
21. <td>Email</td>
22. Advanced Templating
23. 76
24. <td>{$student.email}</td>
25. </tr>
26. </table><br/>
27. {/foreach}
28. </body>
In this template, we look through profiles of some students. Now take a look at the compiled version of this template.
%%EB^EB9^EB93DE89%%profiles.tpl.php:
1. <?php /* Smarty version 2.6.7, created on 2005-09-27 08:14:22
2. compiled from profiles.tpl */ ?>
3. <body>
4. <?php if (count($_from = (array)$this->_tpl_vars['students'])):
5. foreach ($_from as $this->_tpl_vars['student']):
6. ?>
7. <table width="80%" border="1" cellspacing="0" cellpadding="4">
8. <tr>
9. <td width="38%" bgcolor="#CCCCCC" ><strong>Profile of <?php echo $this->_tpl_vars['student']['nick']; ?>
10. </strong></td>
11. <td width="62%"> </td>
12. </tr>
13. <tr>
14. <td>Name : </td>
15. <td><?php echo $this->_tpl_vars['student']['name']; ?>
16. </td>
17. </tr>
18. <tr>
19. <td>Born</td>
20. <td><?php echo $this->_tpl_vars['student']['born']; ?>
21. </td>
22. </tr>
23. <tr>
24. <td>Blood Group </td>
25. <td><?php echo $this->_tpl_vars['student']['blood_grp']; ?>
26. </td>
27. </tr>
28. <tr>
29. <td>Email</td>
30. <td><?php echo $this->_tpl_vars['student']['email']; ?>
31. </td>
32. </tr>
33. </table><br/>
34. <?php endforeach; endif; unset($_from); ?>
35. </body>
 
This is pure PHP code, isn't it? The first two lines of this compiled template are simply a timestamp created by Smarty, which describes the version of the Smarty engine used to compile this template. The date of compilation is also indicated.
The third and fourth lines are PHP code that determines if anything is supplied to Smarty itself as an array in the students variable. If anything is found, it will extract each element of that array in the $_from variable and proceed through the loop. Again each element of this $_from is accessed via the $this->_tpl_vars['student'] variable. All variables passed to the Smarty engine by PHP script are stored in a global _tpl_vars[] array. This _tpl_vars[] is an internal global variable of the Smarty engine. Now you understand that compiled templates are nothing but pure PHP code. Hence they run much faster. Whenever a PHP script asks Smarty to compile a template, it first looks in the compiled templates directory and checks if a compiled version is available or not. If
Smarty finds a compiled version of that template, it will not bother to compile it again unless the original template code has been modified. For these internal processes Smarty is comparatively faster than many other template engines.
 
Compile Steps:
Lets discuss the steps by which a Smarty template is compiled:
1. Smarty applies prefilters (if any) over the whole template.
2. All built-in functions and tags are processed.
3. All custom modifiers are processed.
4. All custom block functions are processed. In this case if nested blocks are found then the deepest block is processed first, then its parent is processed and so forth until all the blocks are processed successfully.
5. Smarty applies postfilters (if any).
6. We get the compiled templates.

 

摘自 沙漠的蜗牛80
补充:Web开发 , 其他 ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,