javascript - In dust.js can the inital value of $idx not be zero based? - Stack Overflow

If I am using the Index helper Is it possible to make the count start at 1 rather than 0. Both:{@idx}{.

If I am using the Index helper Is it possible to make the count start at 1 rather than 0. Both:

{@idx}{.}{/idx}

and

{$idx}

are zero based.

Does anyone know a way to do this?

It would be perfect if you could just do:

{$idx + 1}

but obviously that won't work.

If I am using the Index helper Is it possible to make the count start at 1 rather than 0. Both:

{@idx}{.}{/idx}

and

{$idx}

are zero based.

Does anyone know a way to do this?

It would be perfect if you could just do:

{$idx + 1}

but obviously that won't work.

Share Improve this question asked Feb 8, 2013 at 20:56 busyPixelsbusyPixels 3751 gold badge6 silver badges18 bronze badges 1
  • I am not answering you're question, but I assume this is for display. Have you thought about using CSS counters? css-tricks./numbering-in-style – fulmicoton Commented Feb 8, 2013 at 21:01
Add a ment  | 

3 Answers 3

Reset to default 10

I guess you can use math helper in bination with $idx:

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

To make a use of @math in dust templates you need to add dust helpers which is by default excluded from dust core package.

The specific syntax that you need in order to "load" the dust helpers in node is:

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

Incase you cannot add these helpers, which would be really strange decision, still you can create your own function in current code base like this which can be used instead of @gt or @math

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

Now you can use {position} instead of ${idx} which will count the loop from 1 to n.

Put the math helper inside {@idx}

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743717088a4495176.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信