Skip to main content

Posts

Showing posts with the label EJS

Intermediate Express.Js: Write Conditions and Loops In The EJS (study note)

We continue to use the app.js which we have build to be the study example. In the last post, we only add a simple EJS tag <%= thingVar %> inside h1 tag. This will match the "thing" to any "thing" request from the user. If Statement Today, the course shows us to write a simple if statement in the EJS file. I learn to apply it to my own app. In the if statement, if users send"/wantto/workout" request, the page will show a new content "Good Idea!". We wrote the if statement in the "diet.ejs" file, and we add "<% %>" tags around the code. We have to wrap every line of JavaScript anytime JavaScript starts and ends. Moreover, you may see there are two types of ejs tags here: <%= %> The one with equal sign. According to the course, when we add equal sign, the value that is returned inside of the tag will be rendered to the HTML page. it will be added to the HTML. For example, if we write ...

Intermediate Express.js: How To Send A Dynamic Response Page To Users?

In the last post, I shared an example to show you how to render a HTML page from a EJS file. And, I also mention that this way has limitation because users will receive the same page every time. To solve this kind problem, the course instructor  uses a template to send specific HTML responses to users depending on their specific requests. Step One: Create Another HTML Page We create another HTML page in EJS file. In my HTML page, I just put some diet text and named it as "diet.ejs". Step Two: Write The Route Parameter and render the EJS file In the app.js, I write a route parameter "/wantto/:thing" in the app.get. By following this pattern, users can request any word to replace the "thing" to match the route and get response. Inside the function, I write res.render("diet.ejs") to render the diet HTML page. Therefore, when I run the app, and request "/wantto/health", the app send the diet HTML page to me. ...