ejs

adding .css file to ejs

不羁的心 提交于 2019-11-26 19:56:11
问题 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', {

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

拜拜、爱过 提交于 2019-11-26 18:22:19
问题 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> 回答1: Edit : this Half

Loop through JSON in EJS

守給你的承諾、 提交于 2019-11-26 15:57:58
问题 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)

Express and ejs <%= to render a JSON

点点圈 提交于 2019-11-26 15:34: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 ? 回答1: Oh that was easy, don't use <%= , use <%- instead. For

Pass variables to JavaScript in ExpressJS

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-26 15:19:44
I am completely lost on this; I am using NodeJS to fetch a JSON and I need to pass the variable to my page and have JavaScript use the data. app.get('/test', function(req, res) { res.render('testPage', { myVar: 'My Data' }); That is my Express code (very simple for testing purposes); now using EJS I want to gather this data which I know to render on the page is simply <%= myVar %> But I need to be able to gather this data in JavaScript (if possible within a .js file) but for now just to display the variable in an Alert box I have tried In Jade it is like alert('!{myVar}') or !{JSON.stringify

How to escape HTML in node.js EJS view?

时光怂恿深爱的人放手 提交于 2019-11-26 14:05:00
问题 I want to escape the html in bloglist[i].Text field. How to do that with EJS? <!DOCTYPE html> <html> <head> <title><%= title %></title> <link rel='stylesheet' href='/stylesheets/style.css' /> </head> <body> <h1><%= title %></h1> <p>Welcome to <%= title %></p> <% for(var i=0; i < bloglist.length; i++) { %> <h3> <%= bloglist[i].Title %></h3> <div> <%= bloglist[i].Text %> </div> <% } %> </body> </html> 回答1: You are escaping the value correctly by using: <%= bloglist[i].Text %> If you want to

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

大憨熊 提交于 2019-11-26 11:57:18
问题 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 <% } %> 回答1: 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 });

Render a variable as HTML in EJS

…衆ロ難τιáo~ 提交于 2019-11-26 11:49:43
问题 I am using the Forms library for Node.js (Forms), which will render a form for me on the backend as so: var signup_form = forms.create({ username: fields.string({required: true}) , password: fields.password({required: true}) , confirm: fields.password({ required: true , validators: [validators.matchField(\'password\')] }) , email: fields.email() }); var signup_form_as_html = signup_form.toHTML(); The final line var signup_var signup_form_as_html = signup_form.toHTML(); creates a block of HTML

Passing an object to client in node/express + ejs?

烂漫一生 提交于 2019-11-26 09:00:46
问题 I have a pretty large object that I need to pass to a function in a client script. I have tried using JSON.stringify, but have run into a few issues with this approach - mostly performance related. Is it possible to do something like this in ejs? app.get(\'/load\', function(req, res) { var data = { layout:\'interview/load\', locals: { interview: \'\', data: someLargeObj } }; res.render(\'load\', data); }); And in my client script, I would pass this object to a function like so <script type=\

How to create global variables accessible in all views using Express / Node.JS?

孤人 提交于 2019-11-26 08:44:35
问题 Ok, so I have built a blog using Jekyll and you can define variables in a file _config.yml which are accessible in all of the templates/layouts. I am currently using Node.JS / Express with EJS templates and ejs-locals (for partials/layouts. I am looking to do something similar to the global variables like site.title that are found in _config.yml if anyone is familiar with Jekyll. I have variables like the site\'s title, (rather than page title), author/company name, which stay the same on all