Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Това ми е 'views/article/details'
- {{#if user}}
- {{#ifCond user._id '===' article.authordId }}
- <a class="btn btn-success btn-xs" href="/article/edit/{{article._id}}">Edit »</a>
- <a class="btn btn-danger btn-xs" href="/article/delete/{{article._id}}">Delete »</a>
- {{/ifCond}}
- {{#if user.isAdmin}}
- <a class="btn btn-success btn-xs" href="/article/edit/{{article._id}}">Edit »</a>
- <a class="btn btn-danger btn-xs" href="/article/delete/{{article._id}}">Delete »</a>
- {{/if}}
- {{/if}}
- // express.js
- const hbs = require('handlebars');
- const helpers = require('./../utilities/handlebars');
- app.set('views', path.normalize(path.join(__dirname, '/../views')));
- app.set('view engine', 'hbs');
- hbs.registerHelper("ifCond", helpers.ifCond);
- // utilities/handlebars.js
- module.exports = {
- ifCond: (v1, operator, v2, options) => {
- switch (operator) {
- case '==':
- return (v1 == v2) ? options.fn(this) : options.inverse(this);
- case '===':
- return (v1 === v2) ? options.fn(this) : options.inverse(this);
- case '!=':
- return (v1 != v2) ? options.fn(this) : options.inverse(this);
- case '!==':
- return (v1 !== v2) ? options.fn(this) : options.inverse(this);
- case '<':
- return (v1 < v2) ? options.fn(this) : options.inverse(this);
- case '<=':
- return (v1 <= v2) ? options.fn(this) : options.inverse(this);
- case '>':
- return (v1 > v2) ? options.fn(this) : options.inverse(this);
- case '>=':
- return (v1 >= v2) ? options.fn(this) : options.inverse(this);
- case '&&':
- return (v1 && v2) ? options.fn(this) : options.inverse(this);
- case '||':
- return (v1 || v2) ? options.fn(this) : options.inverse(this);
- default:
- return options.inverse(this);
- }
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement