metaphor metaphor - 10 days ago 6
Jade Question

Handlebars Get URL Parameter With If Statement

I'm building a simple CRUD operations with expressjs. My app is running well with default template engine Jade, but i'd like to try Handlebars template engine

express-handlebars
. In handlebars, how to use if statement within URL ?
I have this code in my Jade template:

[...]

form(action="/users/edit/#{ (_id == undefined) ? user._id : _id }", method="POST").form-horizontal

[...]


Then i change those code into handlebars:

[...]

<form class="form-horizontal" action="/users/edit/{{ _id undefined ? user._id : _id }}" method="post">

[...]


I got this error:

Error: Missing helper: "_id"
at Object.<anonymous> (/home/bill/Websites/public_html/nodejs-apps/codepolitan-demohbs/node_modules/handlebars/dist/cjs/handlebars/helpers/helper-missing.js:19:13)
at Object.eval [as main] (eval at createFunctionContext (/home/bill/Websites/public_html/nodejs-apps/codepolitan-demohbs/node_modules/handlebars/dist/cjs/handlebars/compiler/javascript-compiler.js:254:23), <anonymous>:9:79)
at main (/home/bill/Websites/public_html/nodejs-apps/codepolitan-demohbs/node_modules/handlebars/dist/cjs/handlebars/runtime.js:173:32)
at ret (/home/bill/Websites/public_html/nodejs-apps/codepolitan-demohbs/node_modules/handlebars/dist/cjs/handlebars/runtime.js:176:12)
at ret (/home/bill/Websites/public_html/nodejs-apps/codepolitan-demohbs/node_modules/handlebars/dist/cjs/handlebars/compiler/compiler.js:525:21)
at ExpressHandlebars._renderTemplate (/home/bill/Websites/public_html/nodejs-apps/codepolitan-demohbs/node_modules/express-handlebars/lib/express-handlebars.js:247:12)
at ExpressHandlebars.<anonymous> (/home/bill/Websites/public_html/nodejs-apps/codepolitan-demohbs/node_modules/express-handlebars/lib/express-handlebars.js:173:21)


I tried again with this code:

<form class="form-horizontal" action="/users/edit/{{#if _id undefined}} user._id {{else}} _id {{/if}}" method="post">


Still got an error:

TypeError: Cannot read property 'hash' of undefined
at Object.<anonymous> (/home/bill/Websites/public_html/nodejs-apps/codepolitan-demohbs/node_modules/handlebars/dist/cjs/handlebars/helpers/if.js:16:17)
at Object.eval [as main] (eval at createFunctionContext (/home/bill/Websites/public_html/nodejs-apps/codepolitan-demohbs/node_modules/handlebars/dist/cjs/handlebars/compiler/javascript-compiler.js:254:23), <anonymous>:9:32)
at main (/home/bill/Websites/public_html/nodejs-apps/codepolitan-demohbs/node_modules/handlebars/dist/cjs/handlebars/runtime.js:173:32)
at ret (/home/bill/Websites/public_html/nodejs-apps/codepolitan-demohbs/node_modules/handlebars/dist/cjs/handlebars/runtime.js:176:12)
at ret (/home/bill/Websites/public_html/nodejs-apps/codepolitan-demohbs/node_modules/handlebars/dist/cjs/handlebars/compiler/compiler.js:525:21)
at ExpressHandlebars._renderTemplate (/home/bill/Websites/public_html/nodejs-apps/codepolitan-demohbs/node_modules/express-handlebars/lib/express-handlebars.js:247:12)
at ExpressHandlebars.<anonymous> (/home/bill/Websites/public_html/nodejs-apps/codepolitan-demohbs/node_modules/express-handlebars/lib/express-handlebars.js:173:21)


Here is my users route (edit operation):

router.put('/edit/(:id)', Auth.login, Auth.is_admin, function(req, res, next) {
session_store = req.session;

req.assert('username', 'Name is required').isAlpha().withMessage('Required letter or number').notEmpty();
req.assert('email', 'Invalid Email').notEmpty().withMessage('Empty Email').isEmail();
req.assert('firstname', 'Required letter or number').isAlpha();
req.assert('lastname', 'Required letter or number').isAlpha();

var errors = req.validationErrors();
console.log(errors);

if (!errors) {
v_username = req.sanitize('username').escape().trim();
v_email = req.sanitize('email').escape().trim();
v_firstname = req.sanitize('firstname').escape().trim();
v_lastname = req.sanitize('lastname').escape().trim();
v_admin = req.sanitize('admin').escape().trim();

User.findById(req.params.id, function(err, user) {
user.username = req.param('username');
user.email = req.param('email');
user.firstname = req.param('firstname');
user.lastname = req.param('lastname');
user.admin = req.param('admin');

user.save(function(err, user) {
if (err) {
req.flash('msg_error', 'Error!!!');
} else {
req.flash('msg_info', 'Success!!!');
}

res.redirect('/users/edit/'+req.params.id);
});
});
} else {
// displaying an error
errors_detail = "<p>Please check your data.</p></ul>";

for(i in errors) {
error = errors[i];
errors_detail += '<li>'+error.msg+'</li>'
}

errors_detail += '</ul>';

req.flash('msg_error', errors_detail);
res.render('users/edit', {
_id : req.params.id,
session_store : session_store,
username : req.param('username'),
email : req.param('email'),
firstname : req.param('firstname'),
lastname : req.param('lastname')
});
}
});


Btw, if i change the action URL like this
action="/users/edit/#{{ user._id }}", method="POST")
then it works.

But i just want to know is it possible to do above thing ?

Any help would be appreciated. Thank you.

Answer

From the documentation (http://handlebarsjs.com/builtin_helpers.html) :

You can use the if helper to conditionally render a block. If its argument returns false, undefined, null, "", 0, or [], Handlebars will not render the block.

so the syntax is :

{{#if _id}}user._id{{else}}_id{{/if}}

I just wonder why testing _id ? you should test user._id instead no ?

{{#if user._id}}user._id{{else}}_id{{/if}}
Comments