1

Is there a way to use regex expression comparison using dust template.

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

I want any notes with 's' as the beginning, to be printed as "sample: {notes} " else it will directly print the {notes}.

Is it possible to do so with using any external helper?

Abha
  • 21
  • 1

1 Answers1

0

You can use a helper function

Helper

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);
  }
}

Usage

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

DustJS helpers

bmatovu
  • 3,756
  • 1
  • 35
  • 37