javascript Jade中include和block的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14170537/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 20:54:17  来源:igfitidea点击:

Difference between include and block in Jade

javascriptnode.jsexpresspug

提问by LuckyLuke

What is the the difference between blocks and using include when you are creating Jade templates? When do you use one over the other?

在创建 Jade 模板时,块和使用 include 之间有什么区别?你什么时候用一个?

回答by Golo Roden

A block is a placeholder. Its content comes from another jade file. An include is a placeholder, too. Its content also comes from another jade file.

块是占位符。它的内容来自另一个玉文件。包含也是一个占位符。它的内容也来自另一个玉文件。

So far, both are equal.

到目前为止,两者都是平等的。

But:

但:

include embeds a complete file. The including file defines which file is being included. Hence include is fine for outsourcing parts such as a footer or a header, which are always loaded the same way.

include 嵌入一个完整的文件。包含文件定义了要包含的文件。因此,include 适用于外包部分,例如页脚或页眉,它们总是以相同的方式加载。

A block just defines a placeholder in the top file. Which content is included is not defined by this file, but by a sub-file. So, control over what is included is being reversed.

一个块只是在顶级文件中定义了一个占位符。包含哪些内容不是由这个文件定义的,而是由一个子文件定义的。因此,对所包含内容的控制正在被逆转。

With an include, A says: Import B. With a block, B says: Here is content for a placeholder, and please use file A and its placeholders to fill in my content.

使用包含,A 说:导入 B。使用块,B 说:这是占位符的内容,请使用文件 A 及其占位符填写我的内容。

include means top-down, blocks mean bottom-up.

include 表示自上而下,block 表示自下而上。

Additionally, one file may include several blocks.

此外,一个文件可能包含多个块。

When to use what:

什么时候用什么:

  • It's common to define the overall layout of a site in a master page with blocks.
  • The single pages contain the content for each individual file, reference the master file and fill its blocks.
  • Subviews in either the master or the page are being set up using includes.
  • 在带有块的母版页中定义站点的整体布局是很常见的。
  • 单个页面包含每个单独文件的内容,引用主文件并填充其块。
  • 母版或页面中的子视图正在使用包含设置。

Does that help?

这有帮助吗?