Reputation: 1
Hello
how can i pass a variable from an EJS page to another EJS page.
For example, I have this page
// insertion.ejs //
<!DOCTYPE html>
<html lang=”en”>
<head></head>
<header>
<% include ../views/header %>
</header>
<body>
<script>
var a="hello";
</script>
</body>
</html>
And this page
// get.ejs //
<!DOCTYPE html>
<html lang=”en”>
<head></head>
<header>
<% include ../views/header %>
</header>
<body>
<script>
var b
</script>
</body>
</html>
So how can I pass the content of the variable a to the variable b
Upvotes: -1
Views: 3523
Reputation: 1
Pass variables to an EJS partial in the include:
<%- include('../partials/header', {variant:'compact'}); %>
Reference: https://community.wappler.io/t/how-can-i-use-parameter-inside-partials/21877/5
Upvotes: -1
Reputation: 1
<p> <%= startingContent %> </p>
(Here startingContent
is a key created in page where you want your data).
app.get("/", function(req, res) {
res.render("home", {startingContent: homeStartingContent});
startingContent:
is the Key you identified in page where you want your data.
homeStartingContent:
is the variable of which you wanted the value to be displayed.
Happy Coding!
Upvotes: 0
Reputation: 3863
Well you can model binding in one page, but not two separate page. For this purpose you have cook up your own recipe, you have two novel ways of doing this, first use socket.io , with this method let each page publish and subscribe to events, whenever one page emits a event, the other automatically receives it and vice versa. The alternative approach is to utilize node local storage and cache system modules. For local storage you can memcache, node-cache just do a npm search. For a fast and direct caching use redis,redis in-memory data structure store, used as a database, cache and message broker. It supports data structures such as strings, hashes, lists, sets, sorted sets with range queries.
Happy Coding :):
Upvotes: 0
Reputation: 111424
If you mean to pass the value when a user clicks a link and goes from one page to the other, then you can use a query parameter and have a link like:
<a href="page2?name=value">link</a>
where name
is the name of the query parameter and value
is its value, that could be populated by the EJS template with an appropriate data.
Now you could get that parameter in your route handler and pass it to the template with other variables or you could access it by the client-side JavaScript on the page.
If you want to pass the variable in a sense that you want to have that variable available to a user then you need to use some session storage, a cookie or local storage.
If you want to pass the variable from one page to the other all users just to reduce duplication then you should use include
to include that in all pages that need it or to pass it by the handlers.
This is not very clear from your question what is your intention but this is more or less what you can do in all scenarios.
Upvotes: 2
Reputation: 106
You can use LocalStorage concept. Here is the explanation how to use local storage. https://www.w3schools.com/html/html5_webstorage.asp
Upvotes: -1