How do I implement custom rendering logic in dust.js?

血红的双手。 提交于 2019-11-30 07:45:58

there are many ways in dust to approach this. what i think you're looking for is to probably define a dust filter. you can extend dust.filters to add your own filter. dust.filters looks like this in the source:

dust.filters = {
  h: function(value) { return dust.escapeHtml(value); },
  j: function(value) { return dust.escapeJs(value); },
  u: encodeURI,
  uc: encodeURIComponent,
  js: function(value) { if (!JSON) { return value; } return JSON.stringify(value); },
  jp: function(value) { if (!JSON) { return value; } return JSON.parse(value); }
};

so what you want to do is add another key-value to it that filters your variable. e.g. if you use underscore:

_.extend(dust.filters, {zws: function(value){ your code here}})

then you can call it in your dust template like so:

the variable is: {variable|zws}

hope this helps.

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