Chapter 7. Views and templates: Pug and EJS
This chapter covers
- Express’s view system
- The EJS templating language
- The Pug templating language
In the previous chapters, you learned what Express is, how Express works, and how to use its routing feature. Starting in this chapter, you’re going to stop learning about Express.
Well, okay, not exactly. You’ll still be using Express to power your applications, but as we’ve discussed so far, Express is unopinionated and requires a lot of third-party accessories to make a full-fledged application. In this chapter and beyond, you’ll start digging into some of these modules, learning how they work, and how they can make your applications lovely.
In this chapter we’ll talk about views, which give you a convenient way to dynamically generate content (usually HTML). You’ve seen a view engine before; EJS has helped you inject special variables into HTML. But although EJS provided a conceptual understanding of views, we never really explored everything that Express (and the other view engines) had to offer. You’ll learn the many ways to inject values into templates; see the features of EJS, Pug, and other Express-compatible view engines; and explore subtleties in the world of views. Let’s get started.
Jade Now Pug
Pug was originally called Jade, but was changed for legal reasons. The project has been renamed, but Jade is still used in a lot of code. During the transition period, you’ll have to remember both names.