Node JS Pass variables to pug template's JavaScript and html sections
I have the following route and it passes a serires of variables to the pug template.
items.js route
router.get('/edit/:itemObjectId', async function(req, res, next) {
var itemObjectId = req.params.itemObjectId;
var equipmentCategoryArr = ;
var lifeExpectancyArr = ;
var businessUnitResponsibleArr = ;
var item = undefined;
try {
item = await db_item.getItem(itemObjectId);
// Where item["result"] is either undefined or a JavaScript Object. E.g. {"createdAt":"2018-11-07T04:07:44.587Z","updatedAt":"2018-11-07T04:25:18.526Z","item_name":"GM Portable","manufacturer":"GM"}
res.render('editItem', {
title: 'Edit item details', // Give a title to our page
item: item["result"],
equipmentCategoryArr: req.app.locals.equipmentCategoryArr,
lifeExpectancyArr: req.app.locals.lifeExpectancyArr,
businessUnitResponsibleArr: req.app.locals.businessUnitResponsibleArr
});
} catch (err) {
return Promise.reject(JSON.stringify({ "Error": err }));
}
});
On my editItem.pug
page, I need to pass the variables of an item into the javascript section and a series of input boxes or labels.
editItem.pug
extends layout
block extraScript
script.
$(document).ready(function(){
// HOW TO PASS THE ITEM HERE IN THE JAVASCRIPT SECTION
// FOR EXAMPLE:
// if (item == undefined) {
// alert("item not found");
//}
});
block content
.container
if (item != undefined)
.card
h1.text-center #{title}
form(method='POST' action='/items/addItem' class='needs-validation' enctype='multipart/form-data' novalidate)
.card
.card-header
i.icon-calendar
h4.panel-title Item Name
.card-body
.form-row
input#validationItemName.form-control(name='itemName' type='text' placeholder='Item name' required='')
div.invalid-feedback Item name is required!
// HOW TO PASS item.item_name TO THIS INPUT BOX
javascript node.js express pug
add a comment |
I have the following route and it passes a serires of variables to the pug template.
items.js route
router.get('/edit/:itemObjectId', async function(req, res, next) {
var itemObjectId = req.params.itemObjectId;
var equipmentCategoryArr = ;
var lifeExpectancyArr = ;
var businessUnitResponsibleArr = ;
var item = undefined;
try {
item = await db_item.getItem(itemObjectId);
// Where item["result"] is either undefined or a JavaScript Object. E.g. {"createdAt":"2018-11-07T04:07:44.587Z","updatedAt":"2018-11-07T04:25:18.526Z","item_name":"GM Portable","manufacturer":"GM"}
res.render('editItem', {
title: 'Edit item details', // Give a title to our page
item: item["result"],
equipmentCategoryArr: req.app.locals.equipmentCategoryArr,
lifeExpectancyArr: req.app.locals.lifeExpectancyArr,
businessUnitResponsibleArr: req.app.locals.businessUnitResponsibleArr
});
} catch (err) {
return Promise.reject(JSON.stringify({ "Error": err }));
}
});
On my editItem.pug
page, I need to pass the variables of an item into the javascript section and a series of input boxes or labels.
editItem.pug
extends layout
block extraScript
script.
$(document).ready(function(){
// HOW TO PASS THE ITEM HERE IN THE JAVASCRIPT SECTION
// FOR EXAMPLE:
// if (item == undefined) {
// alert("item not found");
//}
});
block content
.container
if (item != undefined)
.card
h1.text-center #{title}
form(method='POST' action='/items/addItem' class='needs-validation' enctype='multipart/form-data' novalidate)
.card
.card-header
i.icon-calendar
h4.panel-title Item Name
.card-body
.form-row
input#validationItemName.form-control(name='itemName' type='text' placeholder='Item name' required='')
div.invalid-feedback Item name is required!
// HOW TO PASS item.item_name TO THIS INPUT BOX
javascript node.js express pug
add a comment |
I have the following route and it passes a serires of variables to the pug template.
items.js route
router.get('/edit/:itemObjectId', async function(req, res, next) {
var itemObjectId = req.params.itemObjectId;
var equipmentCategoryArr = ;
var lifeExpectancyArr = ;
var businessUnitResponsibleArr = ;
var item = undefined;
try {
item = await db_item.getItem(itemObjectId);
// Where item["result"] is either undefined or a JavaScript Object. E.g. {"createdAt":"2018-11-07T04:07:44.587Z","updatedAt":"2018-11-07T04:25:18.526Z","item_name":"GM Portable","manufacturer":"GM"}
res.render('editItem', {
title: 'Edit item details', // Give a title to our page
item: item["result"],
equipmentCategoryArr: req.app.locals.equipmentCategoryArr,
lifeExpectancyArr: req.app.locals.lifeExpectancyArr,
businessUnitResponsibleArr: req.app.locals.businessUnitResponsibleArr
});
} catch (err) {
return Promise.reject(JSON.stringify({ "Error": err }));
}
});
On my editItem.pug
page, I need to pass the variables of an item into the javascript section and a series of input boxes or labels.
editItem.pug
extends layout
block extraScript
script.
$(document).ready(function(){
// HOW TO PASS THE ITEM HERE IN THE JAVASCRIPT SECTION
// FOR EXAMPLE:
// if (item == undefined) {
// alert("item not found");
//}
});
block content
.container
if (item != undefined)
.card
h1.text-center #{title}
form(method='POST' action='/items/addItem' class='needs-validation' enctype='multipart/form-data' novalidate)
.card
.card-header
i.icon-calendar
h4.panel-title Item Name
.card-body
.form-row
input#validationItemName.form-control(name='itemName' type='text' placeholder='Item name' required='')
div.invalid-feedback Item name is required!
// HOW TO PASS item.item_name TO THIS INPUT BOX
javascript node.js express pug
I have the following route and it passes a serires of variables to the pug template.
items.js route
router.get('/edit/:itemObjectId', async function(req, res, next) {
var itemObjectId = req.params.itemObjectId;
var equipmentCategoryArr = ;
var lifeExpectancyArr = ;
var businessUnitResponsibleArr = ;
var item = undefined;
try {
item = await db_item.getItem(itemObjectId);
// Where item["result"] is either undefined or a JavaScript Object. E.g. {"createdAt":"2018-11-07T04:07:44.587Z","updatedAt":"2018-11-07T04:25:18.526Z","item_name":"GM Portable","manufacturer":"GM"}
res.render('editItem', {
title: 'Edit item details', // Give a title to our page
item: item["result"],
equipmentCategoryArr: req.app.locals.equipmentCategoryArr,
lifeExpectancyArr: req.app.locals.lifeExpectancyArr,
businessUnitResponsibleArr: req.app.locals.businessUnitResponsibleArr
});
} catch (err) {
return Promise.reject(JSON.stringify({ "Error": err }));
}
});
On my editItem.pug
page, I need to pass the variables of an item into the javascript section and a series of input boxes or labels.
editItem.pug
extends layout
block extraScript
script.
$(document).ready(function(){
// HOW TO PASS THE ITEM HERE IN THE JAVASCRIPT SECTION
// FOR EXAMPLE:
// if (item == undefined) {
// alert("item not found");
//}
});
block content
.container
if (item != undefined)
.card
h1.text-center #{title}
form(method='POST' action='/items/addItem' class='needs-validation' enctype='multipart/form-data' novalidate)
.card
.card-header
i.icon-calendar
h4.panel-title Item Name
.card-body
.form-row
input#validationItemName.form-control(name='itemName' type='text' placeholder='Item name' required='')
div.invalid-feedback Item name is required!
// HOW TO PASS item.item_name TO THIS INPUT BOX
javascript node.js express pug
javascript node.js express pug
asked Nov 12 '18 at 0:49
alextc
81142449
81142449
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
To pass the value of item.item_name
into the input element just leave out the quotes and this will render on the server:
input#validationItemName.form-control(value= item.item_name name='itemName'...
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53254740%2fnode-js-pass-variables-to-pug-templates-javascript-and-html-sections%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
To pass the value of item.item_name
into the input element just leave out the quotes and this will render on the server:
input#validationItemName.form-control(value= item.item_name name='itemName'...
add a comment |
To pass the value of item.item_name
into the input element just leave out the quotes and this will render on the server:
input#validationItemName.form-control(value= item.item_name name='itemName'...
add a comment |
To pass the value of item.item_name
into the input element just leave out the quotes and this will render on the server:
input#validationItemName.form-control(value= item.item_name name='itemName'...
To pass the value of item.item_name
into the input element just leave out the quotes and this will render on the server:
input#validationItemName.form-control(value= item.item_name name='itemName'...
answered Nov 12 '18 at 15:12
Graham
3,533123559
3,533123559
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53254740%2fnode-js-pass-variables-to-pug-templates-javascript-and-html-sections%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown