|
In this chapter’s application, we’ll utilize EJS templates, though as previously mentioned almost any template engine in the Node community can be used. If you’re not famil- iar with EJS, don’t worry. It’s similar to tem- plating languages found in other languages (PHP, JSP, ERB). We’ll cover some basics of EJS in this chapter, but we’ll discuss EJS and several other template engines in greater detail in chapter 11.
Whether it’s rendering an entire HTML
page, an HTML fragment, or an RSS feed, rendering views is crucial for nearly every application. The concept is simple: you pass data to a view, and that data is trans- formed, typically to HTML for web applications. You’re likely familiar with the idea of views, because most frameworks provide similar functionality; figure 8.11 illustrates how a view forms a new representation for the data.
Express provides two ways to render views: at the application level with app .render(), and at the request or response level with res.render(), which uses the for- mer internally. In this chapter, you’ll only use res.render(). If you look in ./routes/ index.js, a single function is exported: the index function. This function invokes res.render() in order to render the ./views/index.ejs template, as shown in the fol- lowing code:
exports.index = function(req, res){
res.render('index', { title: 'Express' });
};
In this section, you’ll see how to do the following:
Configure the Express view system Look up view files
Expose data when rendering views
Before looking at res.render() more closely, let’s configure the view system. |
|