ejs
Embedded JavaScript templates for node
What is the best way to make vim highlight ejs (http://embeddedjs.com/) files?
Is it possible to set up html highlight for the file in general and javascript highlight to it's parts inside <% %>?
Appreciate your help!
Source: (StackOverflow)
i recently started on a node project and two of the modules i'm using is express and EJS. but default i usually use eclipse as my IDE, it works well for java and decent for html and javascript. but one problem i'm having is that for .ejs files i get no markup color coding, or any form of code completion. it's basically just a plain file.
does anyone know how i can get eclipse to interpret .ejs files as .html files? i figure it's pretty similar except for the occasional embed tags. or a recommendation for an IDE more well suited to node.js/html/ejs development.
thanks!
Source: (StackOverflow)
I'm using Node.js with Express web framework (and EJS template engine).
When I have to print a variable I do something like:
<% if (value) { %>
<%= value %>
<% } %>
Can I do the same thing without open others brackets ? Like:
<% if (value) { PRINT VALUE } %>
Is this possible? How to print the variable?
Source: (StackOverflow)
Is there a way to register helper functions to EJS templates, so that they can be called from any EJS template? So, it should work something like this.
app.js
ejs.helpers.sayHi = function(name) {
return 'Hello ' + name;
});
index.ejs
<%= sayHi('Bob') %>
Source: (StackOverflow)
I simply want to add an image in a view in my Sails-Project
My view-file has the location
views/album/albums.ejs
and the image is located in
assets/images/placeholder.png
if I add the image like this
<img src="../../assets/images/placeholder.png">
I get this error
GET http://localhost:1337/assets/images/placeholder.png 404 (Not Found)
Am I missing something?
Source: (StackOverflow)
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 IncomingMessage.anonymous (eval at <anonymous> (/usr/local/lib/node/.npm/ejs/0.3.1/package/lib/ejs.js:140:12))
at IncomingMessage.<anonymous> (/usr/local/lib/node/.npm/ejs/0.3.1/package/lib/ejs.js:142:15)
at Object.render (/usr/local/lib/node/.npm/ejs/0.3.1/package/lib/ejs.js:177:13)
at ServerResponse.render (/usr/local/lib/node/.npm/express/1.0.7/package/lib/express/view.js:334:22)
at Object.<anonymous> (/Users/me/Dropbox/Projects/myproject/server.js:188:9)
at param (/usr/local/lib/node/.npm/connect/0.5.10/package/lib/connect/middleware/router.js:146:21)
at pass (/usr/local/lib/node/.npm/connect/0.5.10/package/lib/connect/middleware/router.js:162:10)
at /usr/local/lib/node/.npm/connect/0.5.10/package/lib/connect/middleware/router.js:152:27
at Object.restrict (/Users/me/Dropbox/Projects/myproject/server.js:94:5)
at param (/usr/local/lib/node/.npm/connect/0.5.10/package/lib/connect/middleware/router.js:146:21)
I understand that I could make this error go away by simply adding a "user" local variable in my server code, but the whole point here is that I want to check for the existence of such variables at runtime using your every day if/else nullcheck type pattern. An exception for a variable that doesn't exist seems ridiculous to me.
Source: (StackOverflow)
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 which looks as such:
<div class="field required"><label for="id_username">Username</label><input type="text" name="username" id="id_username" /></div><div class="field required"><label for="id_password">Password</label><input type="password" name="password" id="id_password" /></div><div class="field required"><label for="id_confirm">Confirm</label><input type="password" name="confirm" id="id_confirm" /></div><div class="field"><label for="id_email">Email</label><input type="text" name="email" id="id_email" /></div>
Basically just a long string of HTML. I then try to render it using EJS and Express using the following code:
res.render('signup.ejs', {
session: loginStatus(req)
, form: signup_form_as_html
});
But on rendering the HTML is simply the string that I posted above, rather than actual HTML (and thus a form as I want). Is there any way to make that string render as actual HTML using EJS? Or will I have to use something like Jade?
Source: (StackOverflow)
I'm just starting with node.js + express + ejs. I can't find anywhere how to pull in the requested ejs file to the layout file.
I know full well that yield
is not the right thing here.
e.g.
layout.ejs
<html>
<head><title>EJS Layout</title></head>
<body>
<%= yield %>
</body>
</html>
index.ejs
<p>Hi</p>
Source: (StackOverflow)
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 <% } %>
Source: (StackOverflow)
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/shamoon/local/node/lib/node_modules/express/lib/view.js:315:17)
at /Users/shamoon/Sites/soldhere.in/app.js:26:7
at callbacks (/Users/shamoon/local/node/lib/node_modules/express/lib/router/index.js:272:11)
at param (/Users/shamoon/local/node/lib/node_modules/express/lib/router/index.js:246:11)
My source code is also very simple:
var express = require('express');
var app = module.exports = express.createServer();
// Configuration
app.configure(function(){
app.use(express.bodyParser());
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});
app.set('view engine', 'ejs');
app.set('view options', {
layout: false
});
app.get('/', function(req, res) {
res.render('index', {
message : 'De groeten'
});
});
app.listen(3000);
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);
In my folder, I have ejs installed in node_modules which I got using npm install ejs
.
so my question is.. what gives? What am I doing wrong so that node can't find EJS when I clearly have it installed?
Thanks
Source: (StackOverflow)
Okay I have a mostly static homepage but I wanted to have partial views that for navigation, footer ect. I'm using ejs and it looks like this:
my controller: home.js
// Dependencies
var express = require('express');
module.exports = {
get: function(req, res) {
app.set('view engine', 'ejs');
var model = {
layout:'home',
};
res.render('home');
}
};
My views directory has nav, home and footer all .ejs
Then the actual html file stripped of text would look as following.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" >
<title>Tom Jones</title>
<!-- CSS -->
<link rel="stylesheet" rel='nofollow' href="/css/home.css" type="text/css" media="screen" >
</head>
<body>
<%- partial('nav') %>
<!--content part -->
<div id="showcontainer">
<section>
</section>
</div>
<div id="maincontainer">
<section>
</section>
</div>
</body>
</html>
The Problem
When ever I test it out I run into the error partial is not defined. I tried requiring ejs but no success.
Source: (StackOverflow)
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?
Source: (StackOverflow)
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 solution I found is using the hasOwnProperty
method.
var tpl = '<% if (hasOwnProperty("foo")) { %>foo defined<% } else { %>foo undefined<% } %>';
console.log(ejs.render(tpl, { locals: { bar: "baz"} }));
This doesn't throw any errors.
Is there a better way to keep the template clean? Or why does it throw this error?
Source: (StackOverflow)