Given the Express app uses EJS with a layout file layout.ejs that includes <%- body %>, and the route renders index.ejs with content <h1>Welcome</h1>, what is the final HTML output?
/* layout.ejs */ <!DOCTYPE html> <html lang="en"> <head><title>Site</title></head> <body> <header>Header Content</header> <main><%- body %></main> <footer>Footer Content</footer> </body> </html> /* index.ejs */ <h1>Welcome</h1> /* Express route */ app.get('/', (req, res) => { res.render('index'); });
Remember that <%- body %> in the layout is replaced by the rendered content of the view.
The layout wraps the view content inside the HTML structure. The <%- body %> placeholder is replaced by the index.ejs content, so the final output includes header, main with <h1>Welcome</h1>, and footer.
In an EJS template, you want to include a partial file named nav.ejs. Which syntax is correct?
Including a partial in EJS uses the include function with <%- %> tags to render raw HTML.
The correct syntax is <%- include('nav') %>. The <%- %> tag outputs unescaped HTML, which is needed for partials. Other options either have wrong syntax or escape the output.
The Express app uses EJS with a layout and partials. The layout includes <%- include('header') %> and <%- body %>. The route renders index.ejs. However, the page shows the raw <%- include('header') %> text instead of the header content. What is the likely cause?
/* layout.ejs */ <!DOCTYPE html> <html> <head><title>Test</title></head> <body> <%- include('header') %> <main><%- body %></main> </body> </html> /* header.ejs */ <header>Site Header</header> /* Express setup */ app.set('view engine', 'ejs'); app.get('/', (req, res) => { res.render('index'); });
Think about how layouts are processed and whether EJS code inside layouts is executed.
If the layout is used as a string template and not rendered through EJS, the EJS tags inside it won't be processed. This causes the include tag to appear as raw text. The layout must be rendered by EJS to process includes.
Given layout.ejs includes header.ejs, and header.ejs includes nav.ejs, what is the final rendered output if nav.ejs contains <nav>Menu</nav> and header.ejs contains <header><%- include('nav') %></header>?
/* nav.ejs */ <nav>Menu</nav> /* header.ejs */ <header><%- include('nav') %></header> /* layout.ejs */ <!DOCTYPE html> <html> <head><title>Site</title></head> <body> <%- include('header') %> <main><h1>Home</h1></main> </body> </html>
Remember that includes inside partials are processed recursively by EJS.
The header.ejs includes nav.ejs, so the <nav>Menu</nav> is inserted inside the header. The layout includes the header partial, so the final output nests nav inside header inside body.
What is the main advantage of using layouts combined with partials in an Express app's templating system?
Think about how layouts and partials help organize repeated parts of web pages.
Layouts provide a common page structure, and partials are reusable components like headers or footers. Using them together avoids repeating the same HTML in multiple files, making the app easier to maintain and update.