On my express server I'm rendering a page with data as follows:
app.get('/people/:personID', function (req, res) {
res.render("people/profile", {person: req.person });
});
In my profile.ejs file I can access the data in an ejs tag like so: <p><%= person.name %></p>
I can't figure how to change an attribute of an html tag to the value stored in this object though.
This doesn't work: <img id="my_img" src=<%= person.picture %> alt="">
or this: $("#my_img").attr("src", <%= person.picture %>);
Also, if there's a better way to pass this document to the html page and access it I'm all ears(or eyes in this case). Thank you
EJS (Embedded JavaScript Templating) is one of the most popular template engines for JavaScript. As the name suggests, it lets us embed JavaScript code in a template language that is then used to generate HTML.
When to use HTML/ EJS? It varies according to your application. If you want to render a static page then go for an HTML file and if you want to render a dynamic page where your data coming from various sources then you must choose an EJS file. Good for the static web page.
You have to include string values within quotes.
In html:
<img id="my_img" src="<%= person.picture %>" alt="">
In jQuery:
$("#my_img").attr("src", "<%= person.picture %>");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With