javascript - Simple for loop with JsRender - Stack Overflow

I'm using JsRender to render a template client-side. However, I'm unable to use the for loop

I'm using JsRender to render a template client-side. However, I'm unable to use the for loop tag to repeat an html-portion of the block because it seems to accept only arrays or objects. Instead, my JSON return a variable which is a number (the number of times I should repeat the block). How can I repeat html N times using JsRender?

I'm using JsRender to render a template client-side. However, I'm unable to use the for loop tag to repeat an html-portion of the block because it seems to accept only arrays or objects. Instead, my JSON return a variable which is a number (the number of times I should repeat the block). How can I repeat html N times using JsRender?

Share Improve this question asked Oct 24, 2012 at 16:53 StefanoStefano 3,25810 gold badges62 silver badges101 bronze badges 6
  • Instead of putting -1 and going away, please ment so that if my question is not clear I can edit it – Stefano Commented Oct 24, 2012 at 16:56
  • 1 Have you tried the whole html block in an array? – Franz Noel Commented Oct 24, 2012 at 16:57
  • what do you mean by "whole block in an array"? The block is a simple html portion that I should repeat N times. With PHP it's a simple range-based for loop, however I don't find a way to do it with JsRender – Stefano Commented Oct 24, 2012 at 16:59
  • if you have a block with the code <div>Something that I want to write</div>. Then, you can put this whole long "string" in an array. One block of html code is equivalent to 1 array. Then, repeat the array. – Franz Noel Commented Oct 24, 2012 at 17:12
  • this won't work because I don't get from the response an HTML block but only a number saying how many times it must be repeated – Stefano Commented Oct 24, 2012 at 17:27
 |  Show 1 more ment

2 Answers 2

Reset to default 4

I extended @webdeveloper answer, as there is no need for additional loop because javascript arrays are working in a way that you only have to define last element

$.views.helpers({
    repeatLoop: function( count ) {
        if (!count) return [];
        var repeat = [];
        repeat[count-1] = {};
        return repeat;
    }
});

And then use as

@{{for ~repeatLoop(10)}}
    {{:#index+1}}
@{{/for}}

I am not sure, that JsRender provide this functionality from the box. You can write your own tag, like here: Example Scenario: Creating custom helpers to iterate through fields

$.views.helpers({
    getFields: function( count ) {
        var fieldsArray = [];
        for (var i=0; i < count; i++) {
            fieldsArray.push({});
        }

        return fieldsArray;
    }
});

Demo: http://jsfiddle/6UeZC/

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

相关推荐

  • javascript - Simple for loop with JsRender - Stack Overflow

    I'm using JsRender to render a template client-side. However, I'm unable to use the for loop

    5小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信