Basic Includes
Includes let you break your templates into reusable pieces. Instead of repeating the same HTML everywhere, you put it in one file and include it wherever you need.
<%- include('partials/header') %>
Page Content
<%- include('partials/footer') %>
That's it. The include function pulls in the content from another file and inserts it right there.
Passing Data to Includes
Includes can receive data from the parent template. Just pass an object as the second argument:
<%- include('partials/user-card', { user: user }) %>
Now the included file has access to the user variable. This makes includes incredibly flexible.
Relative Paths
Include paths are relative to the template file that contains them. So if your main template is in views/pages/ and you want to include from views/partials/:
<%- include('../../partials/header') %>
Use ../ to go up directories. Keep your path structure organized and this becomes second nature.
Dynamic Includes
Want to include different files based on conditions? Build the path dynamically:
<%- include('partials/themes/' + theme + '-theme') %>
If theme is 'dark', this includes partials/themes/dark-theme.ejs. Powerful for things like theme switching or component variations.