ejs

What is the proper way to check for existence of variable in an EJS template (using ExpressJS)?

对着背影说爱祢 提交于 2019-11-28 15:56:02
On the EJS github page, there is one and only one simple example: https://github.com/visionmedia/ejs Example <% if (user) { %> <h2><%= user.name %></h2> <% } %> This seems to be checking for the existence of a variable named user, and if it exists, do some stuff. Duh, right? My question is, why in the world would Node throw a ReferenceError if the user variable doesn't exist? This renders the above example useless. What's the appropriate way to check for the existence of a variable? Am I expected to use a try/catch mechanism and grab that ReferenceError? ReferenceError: user is not defined at

Finding current url inside EJS view when using express

和自甴很熟 提交于 2019-11-28 13:45:01
I'm using Express and EJS to serve pages. I'm using Bootstrap for the UI, specifically the navbar. I'd like to add an 'active' class to the current page's <li> item, to show the current page. However, I cannot find how to get the URL from within the EJS code rendering the page. I found 2 workarounds: I used included passing the page name as a parameter in the route's res.render('myview', {pageName: 'myView'}); - which is not scalable and may cause issues. The other way, was to use jQuery on the client side to add the 'active' class to the item upon page ready - but that means including this

How to send data to HTML page and how to use AJAX for Single Page Application in NodeJS Server using express.js framework?

穿精又带淫゛_ 提交于 2019-11-28 12:04:35
问题 How can I display the form submitted data in another HTML Page From 1st page (page1.html)collecting the data from users and after appending this data in the database I want to show the submitted values in another page i.e.(page4.html) Below is my code I have tried using res.sendFile or res.send server.post('/addname', (req, res) => { const user = { timestamp: new Date, FName: req.body.FName, LName: req.body.LName, Phone: req.body.Phone, Email: req.body.email, Contact: req.body.business,

When I link javascript file in html it sends a request to server and causing error

[亡魂溺海] 提交于 2019-11-28 12:02:57
问题 I am using express and node.js in backend and ejs templating engine in front-end. My app.js look like this app.get('/book/:id', (req, res)=>{ var book_id = req.params.id; console.log('this book :', book_id); Book.findOne({ book_id }).then((book)=>{ res.render('book.ejs', {book, title: "Book"}); }).catch((e)=>{ // console.log(e); }); }); book.ejs file <!DOCTYPE html> <html> <head> <title><%= title %></title> <script type="text/javascript" src="jquery-3.3.1.min.js"></script> <script type="text

how to include a template with parameters in EJS?

爷,独闯天下 提交于 2019-11-28 10:41:50
As a real beginner in EJS, I have two charts in my html page, so I want to use my partial twice: <% include partials/spider-chart.ejs %> But I need to pass some parameters inside the ejs to differentiate between graphs. What is the best way? @Naeem Shaikh solution works. Though include also gives you more intuitive way of including a partial template and also passing context variables to that as found in documention section of ejs . <ul> <% users.forEach(function(user){ %> <%- include('user/show', {user: user}); %> <% }); %> </ul> Naeem Shaikh I think you want to render two different charts

Client side and Server side rendering of ejs template

£可爱£侵袭症+ 提交于 2019-11-28 10:39:48
问题 I always wanted to learn NodeJS to be able to run the same code on server and client side. I am using NodeJS with Express and EJS. So. I have a .ejs page with lot's of HTML, JS, CSS and a small bit with template. For the sake of justice let it be like this: the_list-->some.ejs <ul> <% for(i=0;i>the_list.length;i++) { %> <li>the_list[i]</li> <% } %> </ul> After some rendering on the server we have a perfect list. So. Now I want to rerender it on the client. I made some ajax request and now I

Can a js script get a variable written in a EJS context/page within the same file

那年仲夏 提交于 2019-11-28 08:33:29
As I wrote in the title, I'd like to get a value from a variable written into a ejs page/file, from a javascript file within the same page EJS: <% var test = 101; %> JS: <script> var getTest = test; </script> Or what if I'd like to use a function (with parameter) written into a EJS file and use this function in a JS context where the parameter is given to the function from JS context EJS: <% function fn(par){ ... } %> JS: <script> var test = 101; <%>fn(test)<%> </script> Edit : this Half considers you are using EJS on server side 1) You can pass an ejs variable value to a Javascript variable <

How to redirect to another page in node.js [duplicate]

﹥>﹥吖頭↗ 提交于 2019-11-28 05:59:19
This question already has an answer here: How to make a redirect (301) in Node.js / Express? 3 answers I have a login and a signup page. When random user wants to login, and login is successful, I want to redirect him to another .ejs page (for example UserHomePage.ejs), however, nothing I've tried have worked so far. if (loggedIn) { console.log("Success!"); res.redirect('/UserHomePage'); } else { console.log("Error!"); } I would also like to know, how to redirect a user on button click. Let's say I'm on display user page, where I display all of my users, then there is "add another used button"

adding .css file to ejs

北战南征 提交于 2019-11-28 05:58:32
im working on node.js(express) with ejs and im not able to include a .css file to it.i tried the same thing seperately as a html-css duo and it worked fine...how can i include the same in my .ejs file. My app.js goes thus: var express = require('express'); var app = express(); app.set('views', __dirname + '/views'); app.get('/', function(req, res){ res.render('index.ejs', { title: 'My Site', nav: ['Home','About','Contact'] }); }); app.get('/home', function(req, res){ res.render('index.ejs', { title: 'My Site', nav: ['Home','About','Contact'] }); }); app.get('/about', function(req, res){ res

HTML select option with EJS

馋奶兔 提交于 2019-11-28 02:51:01
问题 I'm making a configuration for my web application, try to rendering it to web page. Below is part of my code. I want to the option selected to the config[0].volume has. So, if config[0].volume is '50', The selected option will be '50'. The codes are working well. But I wondered. 'How can I shorten this code?' So verbose with my code. <select id="volume"> <option value="1" <%= config[0].volume == '1' ? 'selected' : ''%>>1</option> <option value="5" <%= config[0].volume == '5' ? 'selected' : ''