php 什么是模板语言?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4026597/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is a templating language?
提问by Nandini Bhaduri
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?
我在某处读到 PHP 是一种模板语言。什么是模板语言?是什么让 PHP 成为其中之一?其他模板语言是什么?
采纳答案by Will Hartung
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).
一些模板语言是成熟的通用语言,例如 PHP、ASP.NET 和 Java 的 JSP。其他的则更有限,专门为模板设计,例如 Velocity 和 FreeMarker(这两个实用程序都适用于 Java)。
Many word processors, such as Microsoft Word, have their own templating capabilities, commonly referred to as "Mail Merge".
许多文字处理器,例如 Microsoft Word,都有自己的模板功能,通常称为“邮件合并”。
回答by NikiC
A templateing 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.
关于 PHP:由于 PHP 本身可以插入到 HTML 中,因此它可以用作模板语言。另一方面,PHP 非常冗长,尤其是在未启用短标签的情况下。
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.
Twig是一个快速且功能丰富的模板引擎。我会推荐它超过smarty。它提供了更多功能(最显着的是模板继承)并且速度更快。
回答by Fosco
PHP isn't necessarily a templating language, but it can kind of pass that test due to the way it is interpreted.
PHP 不一定是一种模板语言,但由于它的解释方式,它可以通过该测试。
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.
读取 PHP 文件时,仅查看 PHP 块标记(<?php
和?>
)内的代码,其余部分将传递到输出。所以一个 .html 文件可以被 PHP 解释器处理,什么都不会发生,HTML 将被输出。
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.
如果 HTML 文件的某些区域有 PHP 标记和代码,它会被解释并可能在这些位置输出一些数据。这可以被认为是一个模板。通常是有一个输出层的想法,然后有与之配套的动态内容。您可以有一个产品页面模板,然后根据一些输入变量,将单个产品详细信息/图像/等填充到模板中。
There are actual template engines for PHP (Smarty for one.. http://www.smarty.net/) that you can take a look at.
有实际的 PHP 模板引擎(Smarty for one.. http://www.smarty.net/),您可以看看。
I've never been a fan of them, but a lot of people find success with it.
我从来都不是他们的粉丝,但很多人通过它找到了成功。
回答by joe
You might read on Smarty 3.
您可能会阅读 Smarty 3。
http://www.smarty.net/v3_overview
http://www.smarty.net/v3_overview
eg. it has template inheritance now
例如。它现在有模板继承