1

有没有办法使用灰尘模板使用正则表达式比较。

例如: @select key="{notes}"} {@eq value="s+"} sample: {notes} {/eq} {@default} {notes} {/default} {/select}

我希望任何以's'开头的笔记都打印为“sample:{notes}”,否则它将直接打印{notes}。

是否可以使用任何外部助手来做到这一点?

4

1 回答 1

0

您可以使用辅助功能

帮手

dust.helpers.regexp = function(chunk, context, bodies, params) {
  var regexp = new RegExp(params.pattern, params.flag);
  if (regexp.test(params.term)) {
    return chunk.render(bodies.block, context);
  } else {
    return chunk.render(bodies['else'], context);
  }
}

用法

{@regexp term=notes pattern="^[sS](\w+)" flag="g"}
  sample: {notes}
{:else}
  {notes}  
{/regexp}

DustJS 助手

于 2018-08-03T22:11:44.303 回答