-
Notifications
You must be signed in to change notification settings - Fork 844
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
global scope of variables #749
Comments
Hello, you probably have already found a solution for this problem, but since there isn't much documentation on ejs, I will just write this to help people new to ejs.
I'm not really sure if this is a question or a statement, but in your code you don't pass anything to
That indeed, would not be good and as I just mentioned, this is not the case. As long you don't pass any data to the templates, they don't know anything. In your example, you render the view with main.js /* You can declare where ejs will find the views folder,
so you don't need to always use the absolute path to render pages.
(I think ejs also automatically looks if it can find a views folder in the root directory,
which results in the same outcome, but I'm not sure)*/
app.set("views", path.join(__dirname, "views"));
app.get("/", function (req, res) {
res.render(path.join("pages/view.js"), {
genres: ["Adventure", genre: "Fantasy"]
});
}); view.js <body>
<%- include('../partials/header.ejs') %>
<% for(let genre of genres) { %>
<%= genre %>
<% } %>
<div class="background"></div>
<div...
</body>
<script> ... </script> In If you want a partial to also be able to access any variable you pass to a page, you need to pass it on in the header.ejs <div class="container">
<% if(name_of_variable_in_partial) {
for(let genre of name_of_variable_in_partial) {%>
<%= genre %>
<% }} %>
</div> |
Hello, im newbie in ejs, so i need a help with find out some thing.
What if i have data that i send to my ejs layout.
below my header has access for all my data that i sent before. But what if i need my header will have access only for genres data and not for listOfMovies data. The reason why i am asking is because I'm used to the fact that you can't use global variables. I would say that isnt global variables but anyway for me it looks like not so good when my header has aceess to all data that i pass.
The text was updated successfully, but these errors were encountered: