簡體   English   中英

如何在本地環境中將幫助程序與Dust.js一起使用?

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

我正在使用來自linkedIn分支(2.5.1)的dust-full.js進行編程。 我正在嘗試使用輔助程序作為條件,但似乎無法使其正常工作。 現在,我正在使用本地計算機(目前我無意包括服務器后端,但可能是該項目進行的地方)。 我目前在我的Dropbox公用文件夾上測試我的代碼。 所有非輔助代碼均有效。

有問題的代碼:

    {?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 教程中看到,需要一個“需要”助手的人。 使用如下代碼:

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

但是,當包含此內容時,會出現控制台錯誤:“ ReferenceError:未定義。” 我認為這是因為node.js中通常使用/包括了“ require”,但是老實說我不知道​​。 我不希望不包含node.js,因為我不知道它,並且我對學習其他庫不感興趣。 但是,我不知道如何評估助手。

我有四個問題:

  1. 我提供的代碼中是否有任何明顯的錯誤?
  2. 是否只有在使用服務器端腳本時才能使用dust.js幫助器?
  3. (假設2的答案為“否”)只能將helpers與dust-full.js和dust-helpers.js一起使用嗎?
  4. (假設3的答案為“否”)一個人如何僅使用客戶端腳本使用助手?

使用require加載幫助程序是針對像Node這樣的服務器環境。 您應該能夠完全完成加載助手的操作,只需將它們包括在主要的灰塵腳本之后,它們就會自動添加到dust對象中。

您所做的工作看起來正確,因此是否可能使用了錯誤的幫助對象Javascript文件路徑?

這是一個顯示{@gt}幫助程序工作的代碼段。 {@if}幫助器已被棄用,因此在使用時會在控制台中收到警告-但{@gt}確實{@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> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM