Method One, use the {include file= "CHILD.TPL"} in the parent template to include the child template directly in
Advantages:
1, is advantageous in the module division and the template reuse.
2, nesting level is not much, the structure of the template is clear, a look at the past to know the content and structure of the template.
3, only need one smarty instance to be able to complete all things, reduces the system resources occupation.
Insufficient:
1, the variable in the child template may conflict with the parent template's variable
2, multiple nesting time, variable name conflict probability increases, for all the variables of the difficulty of assigning value also increased.
3, the control of the child template is poor, for example, can not set $cache_id, $compile _id, come to multiple results.
4, poor scalability. You need to modify the template when you need to add a new template, and you need to modify the program.
Method Two, by $smarty->fetch () the child template's data as a variable assign into the parent template
Advantages:
1, also conducive to the division of the module, the reuse of templates
2, the structure of the template is also clear
3, the child template as a separate object, can be manipulated strong
4. Good expansibility
5, each child template is a separate object, does not appear the problem of variable name conflict
Insufficient:
1, if the sub-template many times, need to use multiple smarty instances to achieve, PHP code appears bloated
2, occupy more system resources
3 、......
At that time, for performance requirements and without testing, take it for granted that the performance of include must be higher than the fetch performance, all of which I use include this method, the result as the system function unceasingly increases, the feeling writes more complex, This is a big part of the problem with design, but nesting between templates is also one of the problems, so go back and consider using the fetch method. Test the performance of this method before making a new decision, test the case of a single template and multiple templates separately
Single Child template test:
There are three pages test1.php,test2.php,test3.php. These three pages all output the same content. test1.php templates are not nested, test2.php templates use a include,test3.php to use fetch to implement the nesting of the child templates. Average time required to access individual pages by using Apache's self-Test AB (units: MS)
The first test, using Ab-n 10000 on the above three pages 10 times, each cumulative 10,000 visits, the results are as follows.
Ab-n 10000 |
test1.php |
test2.php |
test3.php |
Test2/test1 |
Test3/test1 |
1 |
5.178 |
5.442 |
6.097 |
1.050984936 |
1.177481653 |
2 |
5.253 |
5.972 |
6.027 |
1.136874167 |
1.147344375 |
3 |
5.223 |
6.039 |
5.987 |
1.156232051 |
1.146276087 |
4 |
5.533 |
5.997 |
6.02 |
1.083860474 |
1.08801735 |
5 |
5.557 |
6.308 |
6.03 |
1.135144862 |
1.085117869 |
6 |
5.248 |
6.002 |
5.998 |
1.14367378 |
1.142911585 |
7 |
5.211 |
5.933 |
6.003 |
1.138553061 |
1.151986183 |
8 |
5.303 |
6.031 |
6.048 |
1.137280784 |
1.140486517 |
9 |
5.213 |
5.923 |
6.033 |
1.136197967 |
1.15729906 |
Total |
47.719 |
53.647 |
54.243 |
1.124227247 |
1.136717031 |
The second test, using Ab-n 100000 on the above three pages 1 times, each cumulative 100,000 visits, the results are as follows.
ab-n 100000
span= "2" width= "a",
test1.php |
test2.php |
test3.php |
test2/test1 |
< Span>test3/test1 |
1 |
5.723 |
7.874 |
8.55 |
1.375851826 |
1.493971693 |
The first test data, test1 and test2 fluctuation is more obvious, test3 is relatively stable, this phenomenon makes me feel very strange. From the average data, the test results are still in the expected, test1 performance is best, test2 more than Test1 spent 12.42% of the time, test3 more than the test1 cost 13.67% of the time, but the gap between Test2 and Test3 is not big, Only about 1.3% of the difference.
The results of the second Test, three pages of the execution time than for 1:1.37:1.49, strange that this time the volatility is even greater ...
Multiple Child template tests:
There are also three pages test1.php,test2.php,test3.php. These three pages all output the same content. test1.php templates are not nested, test2.php templates use 10 times include to embed 10 child templates, test3.php use 10 fetch to embed 10 child templates. Average time required to access individual pages by using Apache's self-Test AB (units: MS)
Because of the time, I only conducted a ab-n 50000 test, the data is as follows
Ab-n 50000 |
test1.php |
test2.php |
test3.php |
Test2/test1 |
Test3/test1 |
1 |
5.68 |
6.054 |
6.028 |
1.06584507 |
1.061267606 |
Although the test methods and tools may not be very good, but through the comparison of these data, in the actual application of what kind of method, I think we should be in the heart.
Deficiencies and errors are also pointed out, I hope that we can discuss more exchanges.