简体   繁体   中英

What is the difference between an include, a partial, and/or a block in templating. EJS / ejs-locals

I have been using EJS with Express and Node.JS for a little while now and I think I understand the basics. I have experimented with the EJS include function which has allowed me to include one template inside of another. I have also used a library called ejs-locals which provides easy true layout support. One thing I noticed is that ejs-locals also defines partial and block functions which may be used with EJS. I cannot seem to find a good explanation on what the differences between these three concepts are. As far as I can tell they are general concepts to templating and not only EJS. It seems to me that both partial and block are still ways of including other template files, but how do they differ?

include : Simply takes the content from the given file and places it where your include statement is. In other words: The file has access to all variables defined in the file it has been included from.

partials : Pretty much the same as include , with the difference that the partial has only access to the variables that you pass to it when rendering.

blocks : In your layout you define locations where your blocks' content should be. In the file you render, you define contents for these blocks and define the layout you would like your block content to be rendered to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM