I'm using ejs 2.4.1 and ejs-mate 2.3.0 which uses ejs 1.0,
So in ejs2.4.1, <%- include('user/show', {user: user}) %> is supported, but is not supported by ejs-mate.
Is there any way to use ejs's include while using ejs-mate?
Something like <%- ejs.include('user/show', {user: user}) %> ??
Another question will be any other ejs-mate alternative supports ejs2?
maybe try to set your view-engine to ejs-mate.
in your app.js:
app.set('view-engine','eje');
Change to:
app.set('view-engine','ejs-mate');
Related
I'm using NodeJS, expressJS and ejs to load files. while rendering index.ejs app.get('/', function(req, res){ res.render('index');, it loaded perfectly but when I'm embedding navbar.ejs in index.ejs <% include(includes/navbar.ejs) %> it is loading fine (no error in console) but nothing is appearing on the page.
Folder managed:
project>app.js ,
project>pages>index.ejs ,
project>pages>includes>navbar.ejs
I believe you have a typo there in your syntax. You're missing the - in the include. It should rather be
<%- include('includes/navbar.ejs'); %>
Also note that the path should be in quotes.
I need to pass an object inside an ejs include statement. I found a couple of questions on the same issue but I get a weird error when i do the following,
<div>
<%- include (folder/index , {"user": user}) %>
</div>
I get,
ENOENT: no such file or directory, open '/views/(folder/index.ejs'
Any idea why i get a enoent when i pass the object inline ?
if i simply do,
<%- include folder/index %>
this works
but i need to pass user object to index. is there a way to do this?
When you insert space after include, EJS will use include preprocessor directives (<% include folder/index %>); Although they are still supported, the new syntax is like the following:
<div>
<%- include('folder/index', {user: user}) %>
</div>
(See includes Documentation)
found a solution...
Yes. I missed the obvious.
var json_tmp = JSON.parse('<%- JSON.stringify(pass_object) %>');
I've been using the following code with success across my whole website, its just a variable that is passed to the EJS to load a header:
Node
res.render('main', {
header: 'header1.ejs'
});
EJS
<%- include(header) %>
Today I switched from using "bcrypt" to using "bcrypt-nodejs" and now EJS gives me the following error every time I try and open a page.
Error: ENOENT, no such file or directory 'C:\Users\user\Desktop\node\views( header).ejs'
It seems to have forgotten how to parse variables, but only when coupled with Include as other variables passed to EJS still work. Removing "bcrypt-nodejs" doesn't seem to have fixed the problem. Does anyone have any idea what I've done wrong?
Figured it out, has nothing to do with bcrypt. EJS has updateded, the proper syntax is now:
<% include header %>
Trying to make a website with Harp.js here. I use ejs templates, and want to store some useful javascript functions in a central file. How do I do that? I tried to use
<% include _render_util.js %>
But it does not work (seems like js file is not parsed).
Any ideas?
Thanks!
Although there are ways of making this work (sometimes), it's not something that was deliveratly built into Harp.js. Forcing this behaviour often takes time to debug and causes unexpected issues.
Here is a quick experiment I made that works (I didn't throughly test it):
helpers.ejs
I created a say_hello function that takes a name and outputs the string Hello, {name}.
<%
say_hello = function (name) {
return 'Hello, ' + name;
}
%>
index.ejs
I include helpers.ejs (the file mentioned above) in the first line and then use the function in the second line. That outputs <h1>Hello, beautiful</h1>.
<% include helpers.ejs %>
<h1><%= say_hello("beautiful") %></h1>
Example gist: https://gist.github.com/jorgepedret/816c2b3985ad12cef022
There's an open issue on GitHub discussing this issue https://github.com/sintaxi/harp/issues/272
This example is more of a hack than a recommended solution. I've seen cases where it breaks in unexpected ways.
I use node.js/ejs on the server side and backbone.js on the client side.
Both server side and client side use the same templating style.
So the problem is, if I put template code meant for the client inside a template it still get's parsed on the server side.
If found out that something like this works:
<%- "<%= done ? 'done' : '' %\>" %>
However, IMHO this uglifies the code in a way which makes the whole point of using templates useless.
How would you approach this?
Is there a way to define blocks of code inside EJS-templates which do not get parsed like a {literal}-tag used in other templating languages?
Update: For now I use backbone's _.templateSettings to use different delimiters on the client side.
Update: Here's a similar solution in a JSP context: Underscore.js Templates Within JSP
The way I have dealt with this is to override the opening and closing tags on node so that the 2 instances of ejs are lookgin for different tags.
On node you can pass in options
{open:'<%',close:'%>'}
In my case I use <% and <# for my two versions. Then in node ejs template I have something like this (where name is from backbone and everyauth obviously from node):
<% if(everyauth.loggedIn) %><h1><#= name #></h1><% } %>
I assume the question could be read as following because it was this thread Google provide me at first link:
“How I can escape EJS template code delimiter tag only for limited items?”
tl;dr:
Use <%# %> to break the original parsing code (e.g. <<%# %>%= done ? 'done' : '' %<%# %>> will done the following unparsed code <%= done ? 'done' : '' %>)
Long explanation
Imagine a case I decide to change % by ? using { delimiter: '?' } option (that could be the case here, because we want not to use the same has Backbone.js).
Great, that solves your problem. Imagine now later, for some reason, you use your templating system to generate an XML. This XML will start with <?xml version="1.0" encoding="UTF-8"?>.
You will facing the same issue again. What do? You will change the delimiter again? And after that, you will change again? etc. No, for punctual escaping, what we should is just to be capable to say “Not parse this part of the document as EJS”.
So a trick is to avoid EJS understand it's an EJS delimiter parser. So avoid it (in our current case) parse <? (or <% in an original case).
So by simply adding <?# ?> to break the parsing, you will add nothing (the # item is for EJS comment) and you will avoid parser to understand <<?# ?>?xml version="1.0" encoding="UTF-8"?<?# ?>>. The output will be <?xml version="1.0" encoding="UTF-8"?>
Conclusion
In a punctual necessity to avoid EJS parsing, you can just trick the parser to produce the output you need by using <%# %> as a delimiter tag breaker.
For sure, probably in your case, you can just use the marked answer because you will use the EJS tag in a lot of cases.
Well, the way that I currently approach this is to use require.js with the text plugin; this allows me to include the templates using AJAX during development time and have them all compiled into an optimized/minified single file bundle during deploy time.
Of course, if you don't use require.js for dependency management of the rest of your JS code this doesn't work nearly as well, but I can't stand to do javascript dev without require.js anymore now that I'm used to it anyway.
Alternately, there may be other similar technologies that you could use to solve the same problem.
I use backbone.layout.manager on both the client and server side, because I want my templates to be exactly the same.
The way I solved the template delimiter issue was to render the page on the server side, then inject the raw backbone templates.
With new ejs you can add a custom delimiter at client side :
https://github.com/mde/ejs#custom-delimiters
eg :
Custom delimiters can be applied on a per-template basis, or globally:
var ejs = require('ejs'),
users = ['geddy', 'neil', 'alex'];
// Just one template
ejs.render('<?= users.join(" | "); ?>', {users: users}, {delimiter: '?'});
// => 'geddy | neil | alex'
// Or globally
ejs.delimiter = '$';
ejs.render('<$= users.join(" | "); $>', {users: users});
// => 'geddy | neil | alex'