In dust.js, can the initial value of $ idx not be zero?

If I use the index helper, is it possible to start the count from 1, not 0. Both:

{@idx}{.}{/idx}

and

{$idx}

based on zero.

Does anyone know how to do this?

It would be great if you could just do:

{$idx + 1}

but obviously this will not work.

+5
source share
3 answers

I think you can use the math assistant in conjunction with $ idx:

{@math key=$idx method="add" operand="1"/}
+9
source

To use @math in dust patterns, you need to add vacuum cleaners that are excluded from the dust pack by default.

The specific syntax you need to “load” the vacuum cleaners in node:

var dust = require('dustjs-linkedin');
dust.helper = require('dustjs-helpers');

, , , , @gt @math

var baseContext = dust.makeBase({
    position: function(chunk, context) {
        return context.stack.index + 1;
    },
  });

{position} ${idx}, 1 n.

+2

Put the math assistant inside {@idx}

<table>
{#names}
<tr><td>{@idx}{@math key="{$idx}" method="add" operand="1"/}{/idx}</td>
    <td>{name}</td>
</tr>
{/names}
</table>
+1
source

All Articles