ejs

Error: Cannot find module 'ejs'

耗尽温柔 提交于 2019-11-27 20:17:24
问题 Here is my complete error: Error: Cannot find module 'ejs' at Function._resolveFilename (module.js:317:11) at Function._load (module.js:262:25) at require (module.js:346:19) at View.templateEngine (/Users/shamoon/local/node/lib/node_modules/express/lib/view/view.js:133:38) at Function.compile (/Users/shamoon/local/node/lib/node_modules/express/lib/view.js:65:17) at ServerResponse._render (/Users/shamoon/local/node/lib/node_modules/express/lib/view.js:414:18) at ServerResponse.render (/Users

EJS include functions defined in a separate ejs file

烈酒焚心 提交于 2019-11-27 19:27:00
问题 I am trying to include an ejs file that contains functions for setting up my views. These functions were defined to be used a helpers. I have tried using: <% include helpers.ejs %> But when I try to use the function from this file: <% module_start('$body', [{name:'display',value:'block'}], 'body'); %> I get the error: Reference Error: module_start is not defined When I copy over the code from 'helpers.ejs' to my original view file, 'test.ejs', it works as expected. I have gone through several

Serving a static HTML page containing an image using Node JS / Express

余生长醉 提交于 2019-11-27 18:53:03
问题 I am able to serve static HTML pages using express. I also installed "ejs" to render the page with data from local variables in my .js file. I just need to display a small logo in the corner of the page along with the rest of the data. Opening just the html file in the browser loads the image just fine, but with the server I get a broken image. I think it may be a problem with my file path or directory structure. Here is my original simple code without the futile attempts: server.js var

How to generate content on ejs with jquery after ajax call to express server

ε祈祈猫儿з 提交于 2019-11-27 16:51:55
问题 I want to implement a search feature on my website so what i do is make a jquery ajax call with the text to the express server which searches mongodb and gives an object array of the users that match. Now i receive this object succesfully but since there are no partials on ejs how can i refresh just the results list generating the html for each user? 回答1: The node EJS packages comes with a client-side javascript library located in ./node_modules/ejs/ejs.js or ./node_modules/ejs/ejs.min.js .

How would you check for undefined property in ejs for node.js?

大憨熊 提交于 2019-11-27 13:30:39
问题 What is the best way to check for an undefined property in an ejs template? (I'm using the node.js package by TJ Holowaychuk) Example: var tpl = '<% if (foo) { %>foo defined<% } else { %>foo undefined<% } %>'; console.log(ejs.render(tpl, { locals: { bar: "baz" } })); I'd expect this to render "foo undefined". It does throw an foo undefined error instead. I know that this is not supposed to be an issue, since this is expected behavior in the tests. Is there an easy way to avoid this? The only

Loop through JSON in EJS

亡梦爱人 提交于 2019-11-27 12:19:47
I have codes in EJS below, <script> var row =<%-JSON.stringify(data)%> console.log(row); </script> <% for(var i=0; i<JSON.stringify(data).length; i++) {%> <tr> <td> <%= JSON.stringify(data)[i].id%> </td> </tr> <% } %> output of row is correct, an array of 3 objects, each with properties id, name etc.. I can manipulate the row to popuate the table in JS. However, I am wonderring whether there is a way to allow it be done in the above manner? When I run the code above, JSON.stringify(data).length is not 3, but rather the length of the whole string. Another questions is when I try to add <% alert

Express and ejs <%= to render a JSON

邮差的信 提交于 2019-11-27 11:16:57
In my index.ejs I have this code: var current_user = <%= user %> In my node I have app.get("/", function(req, res){ res.locals.user = req.user res.render("index") }) However, on the page I obtain var current_user = [object Object] and if I write var current_user = <%= JSON.stringify(user) %> I obtain: var current_user = {"__v":0,"_id":"50bc01938f164ee80b000001","agents":... Is there a way to pass a JSON that will be JS readable ? piggyback Oh that was easy, don't use <%= , use <%- instead. For example: <%- JSON.stringify(user) %> The first one will render in HTML, the second one will render

Rails with backbone-rails: asset helpers (image_path) in EJS files

你说的曾经没有我的故事 提交于 2019-11-27 10:59:37
问题 I have a Rails 3.1 app that uses the codebrew/backbone-rails. In a .jst.ejs template, I would like to include an image, like so: <img src="<%= image_path("foo.png") %>"/> But of course the asset helpers are not available in JavaScript. Chaining ERB (.jst.ejs.erb) does not work, because the EJS syntax conflicts with ERB. Here is what I know: The asset helpers are not available in the browser, so I need to run them on the server side. I can work around the problem by making the server dump

Can I use conditional statements with EJS templates (in JMVC)?

纵然是瞬间 提交于 2019-11-27 10:38:40
and if yes, what is the syntax? My goal is to prepend an 's' to the word 'comment' when there is more than one. in an jQuery.ejs template in a JMVC app. The following breaks. I can't find any docs for conditionals... <%=commentsNumber%> comment<% if (commentsNumber > 1) { %> s <% } %> For others that stumble on this, you can also use ejs params/props in conditional statements: recipes.js File: app.get("/recipes", function(req, res) { res.render("recipes.ejs", { recipes: recipes }); }); recipes.ejs File: <%if (recipes.length > 0) { %> // Do something with more than 1 recipe <% } %> Conditionals

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

♀尐吖头ヾ 提交于 2019-11-27 09:27:00
问题 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