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) %>');
Related
I have this error
An exception has been thrown during the rendering of a template
("Named route does not exist for name: auth.signup").
This is the TWIG code for the NAV template
<li>Sign up</li>
This is the ROUTE definition
$app->group('/auth', function () {
$this->get('/signup', 'App\Controllers\Auth\AuthController:getSignup')
->setName('auth.signup');
$this->post('/signup', 'App\Controllers\Auth\AuthController:postSignup');
$this->get('/signin', 'App\Controllers\Auth\AuthController:getSignin')
->setName('auth.signin');
$this->post('/signin', 'App\Controllers\Auth\AuthController:postSignin');
});
I'm stumped because the SIGNIN template code works just fine
<form action="{{ path_for('auth.signup') }}" method="post" autocomplete="off">
Any ideas?
I found the issue. I'm new to this and I thought was being cleaver... to cleaver for my own good. I had my route collections in separate files and would only load the route asked for. Seems that TWIG needs the containers that hold PATH_FOR values as well. I put all the routes in a single file and it works fine
I cannot find a way to include external .js file to Node ejs template. I want to put logic and data into object in external .js file, include that file to index.ejs template and pull data from it.
I tried by inserting standard way
<script src="sample.js"></script>, and it doesn't work
Then I tried ejs specific keyword <% include partials/sample.js %> and this works only for adding partials (ejs code snippets).
I inserted .js file into static directory which is defined in executable server.js, no results again.
But interestingly, including css file into ejs template classic way works fine, for example
<link href="/assets/styles.css" rel="stylesheet" type="text/css" />
Workaround would be to include external ejs file where I would put logic and data inside <% %> tags, but this is obviously a patch and not a viable solution, because ejs is not a js file. Besides, it doesn't work.
I cannot find any solution on Internet. Any hint?
Thanks
You can't.
Note: You can only pass data from .ejs file to .js file but not the other way. It won't work because .ejs is rendered on the server side while .js runs on the client side.
I am assuming you are using EJS on server side
1) You can pass an ejs variable value to a Javascript variable
<% var test = 101; %> // variable created by ejs
<script>
var getTest = <%= test %>; //var test is now assigned to getTest which will only work on browsers
console.log(getTest); // successfully prints 101 on browser
</script>
2) You can't pass a js variable value to a ejs variable
Yes, you can't: if it is on server.
Why:
The EJS template will be rendered on the server before the js is started execution(it will start on browser), so there is no way going back to server and ask for some previous changes on the page which is already sent to the browser.
A workaround with Express:
myScripts.js
module.export = {
foo() {},
bar() {}
}
Then in your app.js
var myScripts = require('/path/to/myScripts');
res.render('template', {
utils: myScripts
}); // you forgot a ')' here
then in your template.ejs
// utils will act in global scope for this file
<%
utils.foo();
utils.bar();
%>
I believe you are using it contrary to the intent.
In a controller you can define external scripts and styles you want to include like so
res.render('page-one', {
title: 'Page One',
data: pageData,
libs: ['page-one', 'utils'],
styles: ['page-one']
});
In your static assets for your app you have a js folder and a css folder
|- static/
|- css/
|- fonts/
|- img/
|- js/
favicon.ico
|- templates/
In your js folder you place the file page-one.js and utils.js
In your css folder you place the file page-one.css
In the head section of your html in the ejs template
<!-- styles included on all pages -->
<link rel="stylesheet" href="/css/bootstrap.css">
<!-- styles specific to individual pages -->
<% if (typeof styles !== 'undefined') { %>
<% if (styles.length > 0) { %>
<% for (let style of styles) { %>
<link rel="stylesheet" href="/css/<%= style %>.css">
<% } %>
<% } %>
<% } %>
Typically it is best practice to include scripts at closing body tag so they don't block page render so before the closing body tag in your ejs file
<!-- scripts included on all pages -->
<script src='/js/libs/jquery.min.js' type='text/javascript'></script>
<!-- page specific scripts -->
<% if (typeof libs !== 'undefined') { %>
<% for (let lib of libs) { %>
<script src='/js/<%= lib %>.js' type='text/javascript'></script>
<% } %>
<% } %>
</body>
When your page renders it will render the script and CSS includes automatically
The if block is in case you don't define any includes in the controller.
In your Express application you define your static and external script includes like so
Remember up above we created js and css folders inside a directory named static
// Define static assets
app.use(express.static('static'));
// included on all pages
app.use('/js/libs', express.static(path.join(process.cwd(), 'node_modules/jquery/dist'), { maxAge: 31557600000 }));
Finally, if you absolutely must include JavaScript in your template like rendering JSON data, etc. using special ejs tag <%- %>
<% if (jsonData) { %>
<script id="jsonData">var jsonData=<%- JSON.stringify(jsonData) %>;</script>
<% } %>
I was able to do that by:
serving the Js folder/file in the node app entry file like so:
app.use(express.static(path.join(__dirname, 'views/js')));
Added DOM functions in a index.js file which is in views/js folder.
Added script tag that links to the index.js file before the end body tag of index.ejs file like so:
<script scr="index.js" type="text/javascript"></script>
Note that the src in the script tag does not have "./js/index.js". I actually don't know why it works that way (same with external css stylesheet).
You can achieve it by adding the code below:
app.use(express.static(path.join(__dirname,public)));
You have to add a directory named public (or whatever you are naming) in your project folder. In that folder you can add your external JS file.
NB: the expression path.join is used to make the directory public available/accessible when you call/initiate app from outside the project folder.
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');
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.