Dynamic Key Name in Dust.js

守給你的承諾、 提交于 2019-12-24 02:13:53

问题


I'm looking for a way to call a dynamic key in my dust template file, something like

<table> 
{#array1}
<tr>
    {#array2}
    <td>{#array1}{object.#dynAttrName#}{/array1}</td>
    {/array2}

{/array1}
</table>

I would like to access to something like "object.attribute1" where 1 is the id of the current object in {array1}. (array1[n].id)

Thank you for your Help !


回答1:


It can be done by adding a helper function to the context object:

Context object:

{
   get: function (chunk, context, bodies, params) {
       var obj = dust.helpers.tap(params.ofObj, chunk, context);
       var prop = dust.helpers.tap(params.prop, chunk, context);
       return chunk.write(obj[prop]);
   },

   a: {
       b: "bbb"
   }
}

Template

{#get prop="b" ofObj=a/}

You can try this in linkedin dust tester

I believe it can also be done defining a global dust helper.



来源:https://stackoverflow.com/questions/14505834/dynamic-key-name-in-dust-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!