search
HomeBackend DevelopmentPHP TutorialSmarty前端模板引擎

Smarty

Smarty可以说是我接触PHP这门语言用到的第一个开源框架,当时在Smarty的帮助下,很好地完成了项目的开发,也很好地遵循了MVC的模式。但是到了后来,慢慢地发现,很多人都很熟悉Smarty,但是都不愿意使用它。大概的原因在于:慢。

当初以为Smarty很神奇,到后来发现也是通过PHP来实现的。再到后来别人反映使用Smarty会影响性能,所以也就想深入了解一下它的源代码,看下它是怎么实现的,是不是真的会慢?

其实,Smarty只是用PHP做了一个中间层,来把自定义的一些模板标签再转换成PHP语言,这里面涉及到语法树模式和PHP代码的自动生成。然而,计算机的艺术正在于此:任何的问题,都可以通过一个中间层来实现完成,但也会因此带来性能问题。所以,正是这一层中间层,影响了性能。但同时Smarty也努力通过缓存来填补这块的空缺。但对于好的项目分层、分离前端和后端,Smarty在开发实践中确实有好的作用,这就需要项目在人力成本和服务器成本之间做下权衡吧。

UML静态结构 - 部分

Smarty确实对外表现得很优秀,但是,Smarty里面的结构和代码层次,就个人看来,有些凌乱。以下是部分的UML结构图,其他待补充。


分析与总结

同样,由于当时未能实时纪录,这里罗列一些关键类:

Smarty_Internal_CompileBase(编译的标签,如:循环、赋值、中断等)

Smarty_CacheResource(缓存,如:key-value的缓存、自定义缓存等,这里应该还有引用的缓存)

_smarty_parsetree(语法树解析器,包括:文本、标签、代码等)

Smarty_Template_Source、Smarty_Resource(各种资源:代码、文件包含、字符串、编译/非编译)

由此引申

增加了中间层来实现对模板的解析,会影响性能,但这对人性化开发提供了很好的支撑。更为重要的是,模板引擎这个概念有很多其他框架也可以看到引用。如果觉得Smarty慢或者不符合自己的项目要求,则可以自己实现一套模板引擎规则的解析。例如ThinkPHP对模板的支持,更多信息请参见:http://doc.thinkphp.cn/manual/view.html。

这里可以有一个更深层次的转换,即对语法树模式的使用,这应该会涉及到特定领域语言DSL(更多信息可以看下这本书:《特定领域语言》)。它的作用是通过我们熟悉的语言来实现一些高难度的事情。比如我们觉得对于前端HTML开发人员使用PHP语言来输出数据是件痛苦的事情,那么我们提供类似的标签给他们使用。

再深一层,比如我们(PHP开发人员)觉得用C/C++来开发PHP的扩展是件痛苦或者很高难度的事情,我们可以使用zephir来编写。

这里稍微说一下zephir,zephir是由phalcon(关于phalcon开源框架,后面会说到,不得不说,这是一个非常优秀的开源框架!)团队提供的一种可以用来开发PHP扩展的语言,官方文档请见:http://zephir-lang.com/index.html。它的机制也是通过自己的解析器对zephir的代码转换成C的代码,从而实现PHP扩展开发。

之前,我试着体验了一下zephir,感觉还不错,以下是当时一个尝试示例:[Zephir开发实践]用Zephir编写PHP扩展实践  http://my.oschina.net/u/256338/blog/284540






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
PHP in Action: Real-World Examples and ApplicationsPHP in Action: Real-World Examples and ApplicationsApr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP: Creating Interactive Web Content with EasePHP: Creating Interactive Web Content with EaseApr 14, 2025 am 12:15 AM

PHP makes it easy to create interactive web content. 1) Dynamically generate content by embedding HTML and display it in real time based on user input or database data. 2) Process form submission and generate dynamic output to ensure that htmlspecialchars is used to prevent XSS. 3) Use MySQL to create a user registration system, and use password_hash and preprocessing statements to enhance security. Mastering these techniques will improve the efficiency of web development.

PHP and Python: Comparing Two Popular Programming LanguagesPHP and Python: Comparing Two Popular Programming LanguagesApr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

The Enduring Relevance of PHP: Is It Still Alive?The Enduring Relevance of PHP: Is It Still Alive?Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP's Current Status: A Look at Web Development TrendsPHP's Current Status: A Look at Web Development TrendsApr 13, 2025 am 12:20 AM

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

PHP vs. Other Languages: A ComparisonPHP vs. Other Languages: A ComparisonApr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP vs. Python: Core Features and FunctionalityPHP vs. Python: Core Features and FunctionalityApr 13, 2025 am 12:16 AM

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHP: A Key Language for Web DevelopmentPHP: A Key Language for Web DevelopmentApr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MantisBT

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.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment