ejs

How can I set different <title> values for each template in ejs?

寵の児 提交于 2019-12-04 13:40:45
I am using Sails.js and it comes with ejs-templates. There is a default view called layout.ejs which is always including the body parts from other templates. The title element is defined in the layout.ejs and is therefore always the same. Due to SEO I would like to change the title depending on the view that is included. Is there some way of knowing which view is actually called from Sails, in the .ejs file? Update the <title> tag of your layout.ejs to <title><%= title %></title> and send a title value from your controller along with the other data, like res.view('viewname', { title:

use one large external file for many javascript templates with backbone.js?

蹲街弑〆低调 提交于 2019-12-04 13:06:41
I have two different HTML pages that serve up backbone apps. Up until now, I have put all the js templates inside of each respective HTML file. Now, I'm refactoring a bit and would like to share some backbone views between files. When loading a view that can't find a js template, the whole app will error out. I know the proper way to merge these two would be to have external js templates, using EJS for example, and have 1 template per file, however, I'd like to just have one huge HTML file with embedded <script type='text/template'> and share the template HTML file between my 2 pages. Is this

EJS access express variable in JS onload function

时光怂恿深爱的人放手 提交于 2019-12-04 11:50:11
I know you can get the value of variables in an ejs file like so, <h1><%= title %></h1> if i were to use the same title variable in a onload javascript function in the same ejs page, how would i use it . for instance <script> window.onload(){ var s = <%= title %> alert(s); } </script> this function produces a console error saying Uncaught syntax error: Unexpected identifier although I'm able to see the actual value of the variable To output it within a script, the result will need to be understood as code. Since you didn't note the value of title , I'll assume for the examples: res.render(

How can I pass variable to ejs.compile

此生再无相见时 提交于 2019-12-04 11:13:06
问题 My bottom_index.ejs looks like that: <div>The bottom section</div> In my code I declare ejs: ejs = require('ejs'); Then compile the function: var botom_index_ejs = ejs.compile(fs.readFileSync(__dirname + "/../views/bottom_index.ejs", 'utf8')); and then call it to get rendered html: botom_index_ejs() It works fine! Now I would like to change my template to: <div><%= bottom_text %></div> and be able to pass the parameter (bottom_text) to the bottom_index.ejs How should I pass the parameters?

Node.js, Express, EJS - Active class on current page in navigation

元气小坏坏 提交于 2019-12-04 10:30:04
问题 I'd like to render a 'current' class on each of my navigation links depending on which page I'm on in my layout.ejs template. Currently, my express controller index looks like this: // About exports.about = function(req, res) { res.render('content/about.ejs', { title: 'About' }); }; And in my layout.ejs I have the following, which I'd like be rendered dynamically. <ul class="nav" id="nav"> <li><a href="/">Home</a></li> <li class="current"><a href="/about">About</a></li> <li><a href="/"

EJS templates: How to generate an HTML tree structure in the most elegant and handy way

北慕城南 提交于 2019-12-04 10:10:48
I have this nested Array of Objects: var tree = [ { name: "item 1", link: "#link-1", children: [ { name: "item 1.1", link: "#link-11", children: [ { name: "item 1.1.1", link: "#link-111", children: [] } ] }, { name: "item 1.2", link: "#link-12", children: [] } ] }, { name: "item 2", children: [] } ]; I would like to generate an HTML ul li tree structure from this model. The only solution I found is just below (via a recursive function). But I would like to avoid string concatenation, because in my real situation I have more markup to add on each rows of li (and this is really not handy). <%

Nodejs - How to show qr-image result in view

ぃ、小莉子 提交于 2019-12-04 08:29:37
I use qr-image plugin for Nodejs to generate a QR code and it works pretty good. The problem is showing result image in ejs. var express = require('express'); var router = express.Router(); var qr = require('qr-image'); router.get('/', function(req, res) { var code = qr.image("text to show in qr", { type: 'png', ec_level: 'H', size: 10, margin: 0 }); res.type('png'); code.pipe(res); // res.render('index', { title: 'QR Page', qr: code }); }); When i uncomment last line, nodejs crashs. How to send code to view as a variable? Update: This code returns [object Object] in result page. var code = qr

Nodejs EJS partials with scripts in the head

元气小坏坏 提交于 2019-12-04 07:25:57
I'm working with EJS to render and server HTML pages from a Nodejs server. Some of the partials I include have scripts and stylesheets referenced in the head, but this causes the client to make multiple requests for the same file (for example if the parent view also includes that file) For example: <!-- file: parent.ejs --> <html> <head> <link rel="stylesheet" href="public/mystylesheet.css"> <script src="public/myscript.js"> </head> <body> <%- partial("partial.ejs") %> </body> </html> And in the partial: <!-- file: partial.ejs --> <html> <head> <link rel="stylesheet" href="public/mystylesheet

node.js ejs include error

ⅰ亾dé卋堺 提交于 2019-12-04 07:15:28
I am creating this template: <% include head %> <Placemark> <name><%=name%></name> <description><%=description%></description> <Point> <coordinates><%=coordinates%></coordinates> </Point> </Placemark> <% include foot %> But I always get this error: if (!filename) throw new Error('filename option is required for includ ^ Directories: justas@justas-Studio-1555:~/node-socket.io/socket.io/examples/kml$ ls -1 app.js foot.ejs head.ejs placemark.ejs Can someone help, I according to toolbox everything should work app.js: var http = require('http'); var ejs = require('ejs'); var fs = require('fs') http

How do I use req.flash() with EJS?

时光毁灭记忆、已成空白 提交于 2019-12-04 05:31:01
I want to be able to flash a message to the client with Express and EJS. I've looked all over and I still can't find an example or tutorial. Could someone tell me the easiest way to flash a message? Thanks! I know this is an old question, but I recently ran across it when trying to understand flash messages and templates myself, so I hope this helps others in my situation. Considering the case of Express 4, the express-flash module, and an ejs template, here are 2 routes and a template that should get you started. First generate the flash message you want to display. Here the app.all() method