5

我正在将连接角色与灰尘模板 ejs 模板结合使用类似这样的语法

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

还有玉

if userCan('impersonate')
  button#impersonate Impersonate

如何在灰尘中做到这一点?

            {@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}

这段代码给我一个错误

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

编辑:我有这个{@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                                                                                                                                                                                                                                                     ",

这是我的控制器

    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这里没有工作

4

1 回答 1

4

Dust 不知道什么是函数。它只知道“引用”,这是您上下文中的关键。

如果上下文中的键是一个函数,Dust 将调用该函数并在其渲染中使用结果。此类函数称为上下文帮助器,您可以在文档中了解它们。或者,您可以直接向 Dust 注册可在任何地方访问的函数——这些被称为全局助手

如果userCan是 connect-roles 提供的某种全局变量,您可以在全局帮助器中使用它,如下所示:

dust.helpers.userCan = function(chunk, context, bodies, params) {
  var permission = context.resolve(params.permission);
  return 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}

编辑:它看起来像是userCan请求范围的,因此您可以通过将请求变量添加到您的上下文来使用它。就像是:

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}

需要明确的是,在 Dust 中没有直接在模板中调用函数的方法——它不支持任意 JS 执行。要调用带参数的函数,您必须提供一个调用该函数的助手,以便它可以将参数转换为您的函数期望的格式。

于 2016-01-11T19:34:45.443 回答