eta-dev / eta

Embedded JS template engine for Node, Deno, and the browser. Lighweight, fast, and pluggable. Written in TypeScript
https://eta.js.org
MIT License
1.41k stars 65 forks source link

[Question] What is the pratical difference between Layout and Partial? #258

Closed carloszimm closed 1 year ago

carloszimm commented 1 year ago

Hi! From the docs, I couldn't grasp the idea behind having those two features :( I understand that partials are meant to be reusable components. Is layout the same? It's a better way to organize the "layout" in page/view? What's the advantages over EJS (why this is a compelling feature)? What's the data scope between template and layout? I think a complete example with the main features would clarify a little bit (I couldn't find any under 'Resources/Tutorials and Articles'. Any enlightenment is welcome :)

nebrelbug commented 1 year ago

@carloszimm great question!

A partial is a reusable component that you can include multiple places. Think of a button or navbar.

A layout is a template that's automatically applied to the "outside" of your current template. It's nice if you want to wrap multiple templates in the same common elements. For example, I could have two files ("about.eta" and "index.eta") that both use the "layout.eta" template. The "layout.eta" template would wrap the two files in html, main, and body tags and add links to scripts and CSS files.

carloszimm commented 1 year ago

@nebrelbug nice! I think I got it :) About the data in the it variable, is it only populated with the body field or it has access to all data in scope? For instance, in EJS (ETA as well), partials have access to the parent view (or you could pass the data through the include). Or is layout something a little more static?

nebrelbug commented 1 year ago

The layout will have access to the body field, but also the rest of it :)

carloszimm commented 1 year ago

Alright! Now I see its full advantage (write less repetitive template/partial parts) :D Closing the issue/question...