Calling functions and checking results in DustJs

I combine roles with a dust pattern. The ejs pattern has something like this syntax.

<% if (userCan('impersonate')) { %> <button id="impersonate">Impersonate</button> <% } %> 

and in jade

 if userCan('impersonate') button#impersonate Impersonate 

How to do it in the dust?

  {@eq key=userCan('edit data') value="true" } <td><a href='/assets/edit/{.ID_ASSET}'>Edit</a></td> <td><a href='/assets/delete/{.ID_ASSET}'>Delete</a></td> {:else} {/eq} 

This code is causing me an error

 Wed, 06 Jan 2016 16:57:47 GMT uncaughtException Expected end tag for assets but it was not found. At line : 42, column : 13 

Edit: I have this in {@contextDump key="full"/}

  "tail": {}, "isObject": true, "head": { "enrouten": { "routes": {}, "path": "function path(name, data) {var route;route = this.routes[name];if (typeof route === 'string') {return path2regexp.compile(route)(data);}return undefined;}" }, "userIs": "function (action) {var act = ert(req, action);return roles.test(req, act)}", "userCan": "function (action) {var act = ert(req, action);return roles.test(req, act)}", "isAuthenticated": "function () { [native code] }", "_csrf": "FSaqN0PWxOF4slTUfnGHXJ0NkPOTJFl0u57eM=", "title": " ", "assets": [ { "ID_ASSET": 1, "SYMBOL_KODE": "12.TR.18", "DOK_NAME": "9042", "DESCRIPTION": "  9042", "DATE_RELISE": "2001-10-04T21:00:00.000Z", "POS_KODE": "pos kode 1 ", 

And this is my controller

  router.get('/', function (req, res) { var context = { req: req, // from Express callback userCan: function(chunk, context, bodies, params) { var permission = context.resolve(params.permission); return context.get('req').userCan(permission); } } models.SPR_ASSET.findAll({ include: [ models.SPR_TYPE_UM, models.SPR_TYPE_ASSETS, models.SPR_ASSETS_DS ] }).then(function(assets) { res.render('assets', { title: ' ', assets: assets context: context }); }); 

var context does not work here

+5
source share
1 answer

Dust does not know what functions are. He only knows about the "links", which are the keys in your context.

If the key in your context is a function, Dust will call the function and use the result in its rendering. Such functions are called contextual helpers , and you can read about them in the documentation . In addition, you can register functions directly with Dust, which are available anywhere - they are called global helpers .

If userCan is some kind of global, accessible for connection roles, you can use it in a global helper, for example:

 dust.helpers.userCan = function(chunk, context, bodies, params) { var permission = context.resolve(params.permission); return userCan(permission); } 

And in your template:

 {@userCan permission="edit data"} <td><a href='/assets/edit/{.ID_ASSET}'>Edit</a></td> <td><a href='/assets/delete/{.ID_ASSET}'>Delete</a></td> {:else} Please log in. {/userCan} 

Edit: it looks like userCan has a request scope, so you can use it by adding a query variable to your context. Sort of:

 var context = { req: req, // from Express callback userCan: function(chunk, context, bodies, params) { var permission = context.resolve(params.permission); return context.get('req').userCan(permission); } } {#userCan permission="edit data"} <td><a href='/assets/edit/{.ID_ASSET}'>Edit</a></td> <td><a href='/assets/delete/{.ID_ASSET}'>Delete</a></td> {:else} Please log in. {/userCan} 

To be clear, there is a no method for calling a function directly in your template in Dust - it does not support arbitrary JS execution. To call a function with parameters, you must provide an assistant that calls the function so that it can convert the parameters to the format that your function expects.

+4
source

Source: https://habr.com/ru/post/1240014/


All Articles