Modular DRY Code with Partials
Partials
In EJS, partials are reusable components or sections of a view template that can be included or "rendered" within other templates. They allow you to modularize your views and avoid duplicating code across multiple pages.
To make the pages more modular in the views/partial/
folder there are two EJS
files, one for the top (head.ejs
) and one for the bottom (footer.ejs
) of the document.
Open the view template films.ejs
and replace the code before <div class="container">
with:
Similarly, include the footer partial using replacing the </body></html>
with:
Other pages can have their distinct content between the header and footer sections.
Challenge: Open the view src/views/filmDetails.ejs
and repeat the use of the partials above to see the concept in action.
By using partials, you can now reuse the header and footer code across multiple pages by simply including them in the appropriate view templates. This approach helps keep your code DRY (Don't Repeat Yourself) and makes it easier to maintain and update the common sections of your HTML layout.
Next: Details Pages with EJS