Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
257 views
in Technique[技术] by (71.8m points)

javascript - Difference between include and block in Jade

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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.

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

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...