Skip to content
This repository has been archived by the owner on Feb 3, 2023. It is now read-only.

for equivalent operator ".." #37

Open
scippio opened this issue Dec 29, 2016 · 2 comments
Open

for equivalent operator ".." #37

scippio opened this issue Dec 29, 2016 · 2 comments

Comments

@scippio
Copy link

scippio commented Dec 29, 2016

like: {% for i in 0..10 %}
http://twig.sensiolabs.org/doc/tags/for.html

@guohuihot
Copy link

var locals = {
    arr: function(pattern) {
        // 返回
        if (pattern.indexOf('..') == -1) {
            return [];
        }
        var aPattern = pattern.split('..');
        var arr = [];

        if (!/\d+/.test(aPattern[0])) {
            for (var i = aPattern[0].charCodeAt(0); i <= aPattern[1].charCodeAt(0); i++) {
                arr.push(String.fromCharCode(i));
            }
        } else {
            for (var i = aPattern[0]; i <= aPattern[1]; i++) {
                arr.push(i);
            };
        }
        return arr;
    }
};

{% for i in arr('1..10') %}
 {{ i }} 
{% endfor %}

@nylen
Copy link

nylen commented Aug 11, 2018

Like @guohuihot, I would say this should be done outside of the swig library, but I would write it more explicitly:

{% for i in range(1, 10) %}
  ...
{% endfor %}

The range function is easy to write or to pull in from a library:

I suggest closing this issue.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants