什么是模板语言?

发布于 2024-09-29 08:36:01 字数 63 浏览 0 评论 0原文

我在某处读到 PHP 是一种模板语言。到底什么是模板语言?是什么让 PHP 成为其中之一?其他模板语言还有哪些?

I was reading somewhere that PHP is a templating language. What is exactly a templating language? What makes PHP one? What are the other templating languages?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

无所谓啦 2024-10-06 08:36:01

模板语言背后的前提是该语言“嵌入”到其他一些主文档中。具体来说,在普通文档中,文档的总大小主要是文档源而不是模板语言。

考虑两个人为的示例:

print "This is a simple document. It has three lines."
print "On the second line is my name: " + firstName
print "This is the third line."

vs

This is a simple document. It has three lines.
On the second line is my name: $firstName
This is the third line.

您可以在第一个示例中看到,该语言包装了文档文本。在第二个示例中,文档文本是最常见的,只有一点点代码。

一些模板语言是成熟的通用语言,例如 PHP、ASP.NET 和 Java 的 JSP。其他的则更加有限,专门为模板设计,例如 Velocity 和 FreeMarker(都是 Java 实用程序)。

许多文字处理程序(例如 Microsoft Word)都有自己的模板功能,通常称为“邮件合并”。

The premise behind a template language is that the language is "embedded" within some other master document. Specifically, on your average document, the total size of the document is mostly document source than template language.

Consider two contrived examples:

print "This is a simple document. It has three lines."
print "On the second line is my name: " + firstName
print "This is the third line."

vs

This is a simple document. It has three lines.
On the second line is my name: $firstName
This is the third line.

You can see in the first example, the language wraps the document text. In the second example, the document text is the most prevalent, with just a little bit of code.

Some template languages are full blown general purpose languages, such as PHP, ASP.NET, and Java's JSP. Others are more limited designed specifically for templating, such as Velocity and FreeMarker (both utilities for Java).

Many word processors, such as Microsoft Word, have their own templating capabilities, commonly referred to as "Mail Merge".

仙女 2024-10-06 08:36:01

模板语言基本上是一种允许定义占位符的语言,这些占位符稍后应被替换以实现设计。显然,现代模板语言不仅支持占位符,还支持设计网页时经常需要的循环和条件。有些甚至支持更先进但仍然有用的技术,例如模板继承、宏和沙箱。

关于 PHP:由于 PHP 本身可以插入 HTML,因此可以用作模板语言。另一方面,PHP 相当冗长,尤其是在未启用短标签的情况下。

Twig 是一个快速且功能丰富的模板引擎。我会推荐它而不是聪明的。它提供了更多功能(最显着的是模板继承)并且速度更快。

A templating language basically is a language which allows defining placeholders that should later on be replaced for the purpose of implementing designs. Obviously modern template languages not only support placeholders, but also loops and conditions which are often necessary for designing a web page. Some even support more advanced but still useful techniques like template inheritance, macros and sandboxing.

About PHP: As PHP itself might be interpolated into HTML it can be used as a template language. On the other hand PHP is pretty verbose, especially if short tags are not enabled.

A fast and feature rich template engine is Twig. I would recommend it over smarty. It offers more features (most notably template inheritance) and is faster.

听,心雨的声音 2024-10-06 08:36:01

PHP 不一定是一种模板语言,但由于其解释方式,它可以通过该测试。

读取 PHP 文件时,仅查看 PHP 块标记(?>)内的代码,其余部分将传递到输出。因此,.html 文件可以由 PHP 解释器处理,不会发生任何事情,只是输出 HTML。

如果 HTML 文件的某些区域具有 PHP 标签和代码,它将被解释并可能在这些位置输出一些数据。这可以被认为是一个模板。通常它的想法是拥有一个输出层,然后拥有与之相伴的动态内容。您可以有一个产品页面模板,然后根据某些输入变量,仅将单个产品详细信息/图像/等填充到模板中。

PHP 有实际的模板引擎(Smarty 就是其中之一。http://www.smarty.net/)你可以看一下。

我从来都不是他们的粉丝,但很多人都通过它获得了成功。

PHP isn't necessarily a templating language, but it can kind of pass that test due to the way it is interpreted.

When the PHP file is read, only code within PHP block tags (<?php and ?>) is looked at, the rest is passed to the output. So a .html file could be processed by the PHP interpreter, and nothing would happen, the HTML would be output.

If some of the areas of the HTML file had PHP tags and code, it would be interpreted and likely output some data in those locations. This could be considered a template. Usually it's the idea of having an output layer, and then having dynamic content that goes with it. You could have a product page template, and then based on some input variable, populate just a single products details/images/etc into the template.

There are actual template engines for PHP (Smarty for one.. http://www.smarty.net/) that you can take a look at.

I've never been a fan of them, but a lot of people find success with it.

自在安然 2024-10-06 08:36:01

您可以阅读 Smarty 3。

http://www.smarty.net/v3_overview

例如。它现在有模板继承

You might read on Smarty 3.

http://www.smarty.net/v3_overview

eg. it has template inheritance now

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文