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

后端 未结 1 1335
名媛妹妹
名媛妹妹 2020-12-30 18:19

I\'m using dust.js to render a list of variable names that are sometimes long and contain underscores like:

SUM_COUNT_LABOR_COUNTRIES_SMS_PAST

The browser do

相关标签:
1条回答
  • 2020-12-30 18:41

    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.

    0 讨论(0)
提交回复
热议问题