Episode 3 — NodeJS MongoDB Backend Architecture / 3.5 — Template Engine EJS
3.5 — Exercise Questions: Template Engine EJS
Practice for Section 3.5 — EJS template engine setup, syntax, control flow, partials, and static files.
How to use this material (instructions)
- Read
README.md->3.5.a->3.5.b->3.5.c->3.5.d->3.5.e. - Answer closed-book — then compare to lessons.
- Interview prep —
3.5-Interview-Questions.md. - Cheat sheet —
3.5-Quick-Revision.md.
Template Engine Concepts (Q1-Q6)
Q1. What does a template engine do in one sentence?
Q2. What is server-side rendering (SSR)? How is it different from client-side rendering?
Q3. Name three popular Node.js template engines besides EJS.
Q4. In the MVC pattern, which layer does a template engine represent?
Q5. Name two scenarios where a template engine is a better choice than React or Vue.
Q6. Name two scenarios where React or Vue is a better choice than a template engine.
Setting Up EJS (Q7-Q12)
Q7. What two app.set() calls are needed to configure EJS in Express?
Q8. Do you need to require('ejs') in your Express app? Why or why not?
Q9. What is the default folder Express looks in for EJS templates?
Q10. When calling res.render('profile', { user }), what three things happen internally before the browser gets HTML?
Q11. What error do you see if you forget to install the ejs package?
Q12. What error do you see if the template file does not exist in the views/ folder?
EJS Syntax (Q13-Q22)
Q13. What is the difference between <%= expression %> and <%- expression %>?
Q14. If a user submits <script>alert('XSS')</script> as their name, what happens when you render it with <%= name %>? What about <%- name %>?
Q15. What does <% code %> do? Does it produce any visible output?
Q16. What is the difference between an EJS comment <%# text %> and an HTML comment <!-- text -->?
Q17. Write the EJS tag to include a partial located at views/partials/sidebar.ejs.
Q18. Why must you use <%- include() %> (dash) and not <%= include() %>?
Q19. What does locals refer to inside an EJS template?
Q20. Why does <% if (message) { %> crash when message was not passed, but <% if (locals.message) { %> works?
Q21. What does the -%> closing tag do differently from %>?
Q22. How do you output a literal <%= %> on the page (for a tutorial) without EJS executing it?
Control Flow (Q23-Q30)
Q23. Write the EJS code to display "Welcome, Admin" if user.role === 'admin' and "Welcome, User" otherwise.
Q24. Write the EJS code to loop through an array called items and render each as a <li> element.
Q25. What is the difference between using forEach and a for loop in EJS? When would you prefer for?
Q26. Given this data:
{ users: [{ name: 'A', active: true }, { name: 'B', active: false }] }
Write EJS to show only active users in a <ul>.
Q27. Write a for loop in EJS that displays the first 5 items from an array called products.
Q28. How would you use a switch statement in EJS to render different badges based on ticket.priority being 'low', 'medium', or 'high'?
Q29. Write EJS code that shows "No items found" when the items array is empty, and a list of items otherwise.
Q30. You have nested data:
{
categories: [
{ name: 'Electronics', products: ['Laptop', 'Phone'] },
{ name: 'Books', products: ['Node.js Guide', 'Express Handbook'] }
]
}
Write EJS to display each category as an <h2> with its products as a nested <ul>.
Partials and Layouts (Q31-Q36)
Q31. What is a partial in EJS?
Q32. You have a card partial at views/partials/card.ejs. How do you pass a product variable to it when including?
Q33. What package provides layout support (<%- body %>) for EJS? How do you install and configure it?
Q34. In a layout file, what does <%- body %> get replaced with?
Q35. Can a partial include another partial? What is a practical example?
Q36. Write the three partial include lines for a page that uses header.ejs, page-specific content, and footer.ejs.
Static Files (Q37-Q42)
Q37. What Express middleware serves static files? Write the one-line setup.
Q38. If a CSS file is at public/css/style.css, what URL do you use in the <link> tag?
Q39. Why should you use absolute paths (/css/style.css) instead of relative paths (css/style.css) in EJS templates?
Q40. What happens if you place app.use(express.static('public')) after your route definitions?
Q41. A user reports that images load on the homepage but not on /admin/dashboard. What is the most likely cause?
Q42. Write the complete Express setup that configures EJS, serves static files from public/, and renders a home page.
Answer Hints
| Q | Hint |
|---|---|
| Q1 | Merges HTML template with data on the server |
| Q7 | view engine and views |
| Q11 | Cannot find module 'ejs' |
| Q13 | Escaped vs unescaped output |
| Q18 | Partial HTML would be escaped as text |
| Q20 | Accessing undefined property vs undefined variable |
| Q25 | for allows break and continue |
| Q38 | /css/style.css (no public/ prefix) |
| Q39 | Relative paths break on nested URLs like /admin/users |
| Q41 | Relative paths in image src attributes |
<- Back to 3.5 -- README