Table of Contents
Smarty
分析与总结
由此引申
Home Backend Development PHP Tutorial Smarty前端模板引擎

Smarty前端模板引擎

Jun 23, 2016 pm 01:42 PM

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 of this Website
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

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 ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Framework Security Features: Protecting against vulnerabilities. Framework Security Features: Protecting against vulnerabilities. Mar 28, 2025 pm 05:11 PM

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

Customizing/Extending Frameworks: How to add custom functionality. Customizing/Extending Frameworks: How to add custom functionality. Mar 28, 2025 pm 05:12 PM

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

See all articles