使用模板时难免要碰到模板嵌套的时候,大家都知道有两种方法可以实现,以下是个人早期的一些使用感受。
方法一、使用在父模板中使用{include file="child.tpl"}直接将子模板包含进来
优点:
1、有利于模块的划分和模板的重用。
2、嵌套层次不多的时候,模板的结构清晰,一眼过去就知道这个模板的内容和结构。
3、只需要一个smarty实例就能做完所有的事情,减少系统资源的占用。
不足:
1、子模板中的变量可能与父模板的变量发生冲突
2、多重嵌套的时候,变量名冲突的几率增大,为所有的变量赋值的难度也加大。
3、子模板的可操控性差,例如不能通过设置$cache_id, $compile_id,来得到多个结果。
4、扩展性差。如果需要添加新的模板的时候需要修改模板,同时还需要修改程序。
方法二、通过$smarty->fetch()将子模板的数据当成变量assign到父模板中
优点:
1、同样有利于模块的划分,模板的重用
2、模板的结构同样清晰
3、子模板作为单独的对象,可操控性强
4、扩展性较好
5、每个子模板都是单独的对象,不会出现变量名冲突的问题
不足:
1、如果子模板多的时候,需使用多个smarty实例来实现,PHP代码显得臃肿
2、占用较多的系统资源
3、......
在那个的时候,出于对性能的要求,以及在没有测试的情况下,想当然的认为include的性能一定就比fetch的性能要高,所有本人使用include这个方法,结果随着系统功能的不断的增加,感觉到越写越复杂,这有很大一部分原因是设计的问题,但是模板间的嵌套也是问题之一,于是回过头来考虑使用fetch这个方法。在重新做出决定之前对这个方法的性能做了测试,分别对单个模板的情况下和多个模板的情况进行测试
单个子模板测试:
有三个页面test1.php,test2.php,test3.php。这三个页面都输出同样的内容。test1.php的模板没有嵌套,test2.php的模板使用了一次include,test3.php使用fetch来实现子模板的嵌套。使用apache自带的ab测试访问各个页面所需要的平均时间(单位:ms)
第一次测试时,使用ab -n 10000 对以上三个页面进行10次,每次累计10000个访问,结果如下。
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 |
总计 | 47.719 | 53.647 | 54.243 | 1.124227247 | 1.136717031 |

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools