Table of Contents
Django模板特点
总结
Home php教程 php手册 Django的模板实现方式和PHP模板以及前端模板的区别

Django的模板实现方式和PHP模板以及前端模板的区别

Jun 06, 2016 pm 07:53 PM
django php front end the difference study accomplish Way template

在学习Django模板之前我以为也和很多php模板引擎一样,是通过缓存文件输出,执行模板中的python代码来实现的。读完发现居然和前端模板相似,用了正则。 为了更好的体会设计的哲学,我同时参考了tornato、drupal、codeigniter、aceTemplate、underscore中的模

在学习Django模板之前我以为也和很多php模板引擎一样,是通过缓存文件输出,执行模板中的python代码来实现的。读完发现居然和前端模板相似,用了正则。

为了更好的体会设计的哲学,我同时参考了tornato、drupal、codeigniter、aceTemplate、underscore中的模板设计。

Django模板特点

  Django的模板设计时有个重要的假设是:“写模板的不懂python”。因此你只要搞告诉他基本的逻辑结构(判断、循环、输出变量等)的写法就行了。同时也避免了程序员将业务逻辑写道模板里。解析模板的时候和前端模板非常类似,也是用正则提取除所有的代码部分,然后顺序解析(解析的时候根据实际情况构造作用域)成html。

  php中模板实现得比较简单,主要是通过缓冲输出流来实现的。模板中使用的就是正常的php代码。这主要也是因为php文件把不在php标记中的文本都直接输出的特性。所以php的模板就本上都是能直接执行php代码的。

  这里可以看出两种完全不同的设计思路,Django是主动对模板使用者设限,想当于自己实现了一个小的模板语言。而tornato、php模板和前端模板则是让尽量让开发者减少学习成本,同时获得极大的自由。不过前端模板让开发者获得极大自由的主要原因是html和前端逻辑几乎都是统一个人写的。

2.支持模板嵌套

3.支持模板继承

 

总结

以下几点是我觉得有意思的:

1. 无论哪种模板实现,其实本身和html没有一毛钱关系。

2. 要论速度,肯定是像php这种直接缓存输出流的最快。毕竟省了模板编译这一步。tornato生成python语句之后如果也是封装成函数,那么应该是可以缓存的,没有考证。

3. Django模板实现的方式也许和整个框架的设计理念是相关的,我还没有看完,不妄下评论。不过至少在书里面说起清楚了为什么这么做。

刚开始学python和Django,有写的不对的地方感谢读者指正。

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

Video Face Swap

Video Face Swap

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

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)

What is Cross-Site Request Forgery (CSRF) and how do you implement CSRF protection in PHP? What is Cross-Site Request Forgery (CSRF) and how do you implement CSRF protection in PHP? Apr 07, 2025 am 12:02 AM

In PHP, you can effectively prevent CSRF attacks by using unpredictable tokens. Specific methods include: 1. Generate and embed CSRF tokens in the form; 2. Verify the validity of the token when processing the request.

How to set password protection for export PDF on PS How to set password protection for export PDF on PS Apr 06, 2025 pm 04:45 PM

Export password-protected PDF in Photoshop: Open the image file. Click "File"> "Export"> "Export as PDF". Set the "Security" option and enter the same password twice. Click "Export" to generate a PDF file.

Explain strict types (declare(strict_types=1);) in PHP. Explain strict types (declare(strict_types=1);) in PHP. Apr 07, 2025 am 12:05 AM

Strict types in PHP are enabled by adding declare(strict_types=1); at the top of the file. 1) It forces type checking of function parameters and return values ​​to prevent implicit type conversion. 2) Using strict types can improve the reliability and predictability of the code, reduce bugs, and improve maintainability and readability.

How can you prevent a class from being extended or a method from being overridden in PHP? (final keyword) How can you prevent a class from being extended or a method from being overridden in PHP? (final keyword) Apr 08, 2025 am 12:03 AM

In PHP, the final keyword is used to prevent classes from being inherited and methods being overwritten. 1) When marking the class as final, the class cannot be inherited. 2) When marking the method as final, the method cannot be rewritten by the subclass. Using final keywords ensures the stability and security of your code.

The Future of PHP: Adaptations and Innovations The Future of PHP: Adaptations and Innovations Apr 11, 2025 am 12:01 AM

The future of PHP will be achieved by adapting to new technology trends and introducing innovative features: 1) Adapting to cloud computing, containerization and microservice architectures, supporting Docker and Kubernetes; 2) introducing JIT compilers and enumeration types to improve performance and data processing efficiency; 3) Continuously optimize performance and promote best practices.

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP's Current Status: A Look at Web Development Trends PHP's Current Status: A Look at Web Development Trends Apr 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 and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 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.

See all articles