如何在本地环境中使用带有 Dust.js 的助手?

How do I use helpers with Dust.js on a local environment?

我正在使用来自 linkedIn fork (2.5.1) 的 dust-full.js 进行编程。我正在尝试使用助手来处理条件,但我似乎无法让它工作。现在,我正在使用我的本地机器(我目前无意包含服务器后端,但它可能是这个项目的目标)。我目前在我的 Dropbox public 文件夹中测试我的代码。所有非辅助代码都有效。

有问题的代码:

    {?quote temp="4"}
        Once you have chosen the best answer{@if cond="{temp} > 1"}s{/if}, please select the "Submit" button.
    {:else}
        Choose the best answer{@if cond="{temp} > 1"}s{/if}, then select the "Submit" button.
    {/quote}

我之前添加了 dust-full.js 发行版和助手,如下所示:

<script src="script/dust-full.js" type="text/javascript"></script>
<script src="script/dust-helpers.min.js" type="text/javascript"></script>

我在其他线程和 dust.js tutorial 上看到有人需要 "require" 助手。使用这样的代码:

var dust = require('dustjs-linkedin');
dust.helper = require('dustjs-helpers');

然而,当我包含这个时,我得到了控制台错误:"ReferenceError: require is not defined." 我认为这是因为 "require" 在 node.js 中通常是 used/included,但我老实说不知道。我不想包含 node.js,因为我不知道它而且我对学习其他库不感兴趣。但是,我不知道如何评价帮手。

我有四个问题:

  1. 我提供的代码中是否存在任何明显的错误?
  2. dust.js 助手是否仅在使用服务器端脚本时可用?
  3. (假设2的答案是"No") helpers是否只能与dust-full.js和dust-helpers.js一起使用?
  4. (假设 3 的答案是 "No")如何仅使用客户端脚本来使用助手?

使用 require 加载助手适用于像 Node 这样的服务器环境。您应该能够完全按照加载助手的方式执行操作——只需将它们包含在主要的灰尘脚本之后,它们就会自动添加到 dust 对象中。

您所做的看起来是正确的,那么您是否使用了错误的除尘助手 Javascript 文件路径?

这是显示 {@gt} 助手工作的片段。 ({@if} 助手已被弃用,因此您在使用它时会在控制台中收到警告——但 {@gt} 完全按照您的意愿行事。)

<div id="output"></div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/dustjs-linkedin/2.5.1/dust-full.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/dustjs-helpers/1.5.0/dust-helpers.min.js"></script>
<script>
  var tmpl = dust.compile('Hello World! Choose the best answer{@gt key=temp value=1}s{/gt}', "test"),
      context = { "temp": 4 };
  dust.loadSource(tmpl);
  dust.render("test", context, function(err, out) {
    document.getElementById("output").textContent = out;
  });
</script>