2012-08-07 60 views
4

這個問題是關於使用dust.js模板系統和子上下文中的數據路徑。我的意圖是支持i18n使用鍵到字符串的映射。dust.js:在作用域部分中使用路徑

由於數據是這樣的:

{i18n : { firstname : "First Name" }, 
people : [ 
    {"name" : "Moe"}, 
    {"name" : "Curly"} 
]} 

在灰塵,您可以使用部分列出的每個人:

{#people} 
    {name} 
{/people} 

你也可以使用一個路徑去的名字國際化字符串訪問:

{i18n.firstname} 

但以下不起作用:

{#people} 
    {i18n.firstname}: {name} 
{/people} 

事實上,文件明確表示:

爲了避免脆性和混亂的引用,路徑從來沒有走回頭路了 上下文堆棧。如果您需要鑽取 父級上下文中可用的密鑰,請將該密鑰作爲參數傳遞。

所以我嘗試通過鍵作爲參數:

{#people i18n=i18n} 
    {i18n.firstname}: {name} 
{/people} 

但是,這是行不通的。當我與這對dust homepage實驗,我看到了編譯模板有這樣的代碼:

"i18n": ctx.get("i18n") 

這讓我覺得上面的例子應該能正常運行。

那麼是什麼給?我該如何做這項工作?

注:以下確實工作:

{#people firstname=i18n.firstname} 
    {firstname}: {name} 
{/people} 

但是如果你需要在人內訪問了很多國際化的鍵背景下,這個不能很好地工作。

+0

我還開了票在LinkedIn:欲瞭解更多信息,免受灰塵的LinkedIn叉看看這個wiki頁面GitHub有詳細的討論:https://github.com/linkedin/dustjs/issues/100#issuecomment-7615536​​ – JBCP 2012-08-09 16:25:13

回答

5

路徑可以在鍵和區段中使用,指定您希望Dust查找鍵的位置。要了解如何以及何時使用路徑,您應該首先了解「灰塵」如何在沒有路徑時查找按鍵。你可能想坐下來。

當搜索鍵時,Dust首先在當前上下文中查找,然後在每個父上下文中查找,直到沒有其他父項搜索爲止。如果找不到鍵,則Dust將不執行任何操作(除非您使用Exists或不存在部分)。如果Dust發現密鑰,它將停止搜索並使用給定的值。這不是太陌生,對吧?這就是JavaScript在執行環境中尋找變量的方式。

然而,當使用路徑時,不是查找樹根,而是使用路徑時,Dust僅查看子節點。以一個例子來最好地說明這一點。

JSON: 

{ 
    "i18n": { 
    "firstname": "First Name" 
    }, 
    "name": "My Dust Template", 
    "firstname": "Surprise!", 
    "people": [ 
    { 
     "name": { 
     "firstname": "Moe", 
     "lastname": "Howard" 
     } 
    }, 
    { 
     "name": { 
     "firstname": "Curly", 
     "lastname": "Howard" 
     } 
    } 
    ] 
} 

,粉塵:

Dust Template: 

{! Show the name of the template !} 
{name}{~n} 
{#people} 

    {! 
    As you noted, the following will not work, because 
    when using a path, Dust only searches 
    deeper into the JSON. The output will be: 
    ": Surprise!" 
    !} 
    {i18n.firstname}: {firstname}{~n} 

    {! 
    To get your i18n string, you need to look up the 
    stack instead of down. This can be done by using a 
    section without a path, as follows. The output will be: 
    "First Name: Moe" 
    "First Name: Curly" 
    !} 
    {#i18n}{firstname}{/i18n}: {name.firstname}{~n} 

    {! 
    Note that we successfully used the path to 
    get the firstname of each of the people. 
    !} 
{/people} 

[編輯]:https://github.com/linkedin/dustjs/wiki/Where-else-does-Dust-search-if-the-value-is-not-defined%3F-A.K.A.-Dust-Scoping

+0

我接受這個答案,因爲它解釋了發生了什麼事以及如何使它工作。我的沮喪和接受的延遲來自於這種解決方案的詳細情況,如果你有很多對i18n對象內的路徑的引用。我相信LinkedIn的粉絲對這個問題有了一些新的解決方案,但是我們後來從灰塵中走了出來。 – JBCP 2014-04-01 13:37:48

3

如果這是你的模板:

{#people i18n=i18n} 
    {i18n.firstname}: {name} 
{/people} 

這裏是你的上下文堆棧的樣子,因爲它通過您的「姓名」數組迭代

{ your original context} 
i18n -> firstname: "First Name" 
name -> "Moe" 

發生的事情是,當你定義一個參數,灰塵會將您定義的所有參數壓入上下文。那麼當它在你的上下文中找到一個數組時,灰塵會一次一個地推入堆棧中的所有項目。

所以現在,當您在該部分中定義路徑上下文時,即使您已經將i18n作爲參數傳遞,i18n上下文仍處於上下文堆棧中,並且當您嘗試使用路徑訪問i18n時,像{i18n.firstname},灰塵不會找到它,因爲它必須回溯找到它,而getPath不會回溯。get方法,而另一方面呢原路返回,所以這就是爲什麼,當你這樣做:

{#people firstname=i18n.firstname} 
    {firstname}: {name} 
{/people} 

它的作品,因爲它是訪問與get方法的部分中的名字。希望你明白我想說的話。

什麼,我會做的就是定義一個輔助方法,它在部分像這樣:

{#people} 
    {#i18n}{firstname}{/i18n}: {name}{~n} 
{/people} 

,並確定在上下文的方法(或將其推送到你的全球背景下,以[與makeBase定義]讓它成爲一個全球幫手)像這樣:

i18n: function(chunk, context, bodies, params){ 
    var trans = context.get(translation); //or whatever name you give to your i18n list 
    chunk.render(bodies.block, context.push(trans)); 
    return chunk; 
} 

在灰塵網站上測試了它,它的工作原理。這種方法的優點是你現在可以在該部分的範圍內格式化你的i18n輸出。另外,在你的全局上下文中定義helper和i18n列表是很好的,所以你可以使用makeBase。祝一切順利。