Advertisement
Gesh4o

Untitled

Nov 4th, 2016
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 2.00 KB | None | 0 0
  1. // Това ми е 'views/article/details'
  2. {{#if user}}
  3.   {{#ifCond user._id '===' article.authordId }}
  4.         <a class="btn btn-success btn-xs" href="/article/edit/{{article._id}}">Edit &raquo;</a>
  5.         <a class="btn btn-danger btn-xs" href="/article/delete/{{article._id}}">Delete &raquo;</a>
  6.     {{/ifCond}}
  7.     {{#if user.isAdmin}}
  8.         <a class="btn btn-success btn-xs" href="/article/edit/{{article._id}}">Edit &raquo;</a>
  9.         <a class="btn btn-danger btn-xs" href="/article/delete/{{article._id}}">Delete &raquo;</a>
  10.     {{/if}}
  11. {{/if}}
  12.  
  13. // express.js
  14. const hbs  = require('handlebars');
  15. const helpers = require('./../utilities/handlebars');
  16.  
  17.  app.set('views', path.normalize(path.join(__dirname, '/../views')));
  18.  app.set('view engine', 'hbs');
  19.  
  20.  hbs.registerHelper("ifCond", helpers.ifCond);
  21.  
  22. // utilities/handlebars.js
  23. module.exports = {
  24.     ifCond: (v1, operator, v2, options) => {
  25.  
  26.         switch (operator) {
  27.             case '==':
  28.                 return (v1 == v2) ? options.fn(this) : options.inverse(this);
  29.             case '===':
  30.                 return (v1 === v2) ? options.fn(this) : options.inverse(this);
  31.             case '!=':
  32.                 return (v1 != v2) ? options.fn(this) : options.inverse(this);
  33.             case '!==':
  34.                 return (v1 !== v2) ? options.fn(this) : options.inverse(this);
  35.             case '<':
  36.                return (v1 < v2) ? options.fn(this) : options.inverse(this);
  37.            case '<=':
  38.                return (v1 <= v2) ? options.fn(this) : options.inverse(this);
  39.            case '>':
  40.                 return (v1 > v2) ? options.fn(this) : options.inverse(this);
  41.             case '>=':
  42.                 return (v1 >= v2) ? options.fn(this) : options.inverse(this);
  43.             case '&&':
  44.                return (v1 && v2) ? options.fn(this) : options.inverse(this);
  45.             case '||':
  46.                 return (v1 || v2) ? options.fn(this) : options.inverse(this);
  47.             default:
  48.                 return options.inverse(this);
  49.         }
  50.     }
  51. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement