How would you create a custom for loop tag plugin?

I am trying to create a tag plugin that loops, like the Liquid {% for %} tag.

So one could do something like:

{% counter 3 %}
Going {{ counter }} time...
{% endcounter %}

To produce:

Going 1 time...
Going 2 time...
Going 3 time...

I’d like to do this with native for loop/context variable evaluation logic, rather than a hack.

One can’t just do a find/replace on {{ placeholders }} in the block body, as the block body is already sent to the tag with anything in double-curly-braces evaluated.

So you could hack it by inventing your own syntax, e.g. << placeholder >>, but this is just really dirty.

I’ve tried pasting the code of the actual Liquid For tag into my own plugin, but this won’t run. I get undefined method 'render' for nil:NilClass.

Does anyone understand how the for loop tag works well enough to help me out in creating my own looping tag? Thanks.

Couldn’t you do the following with a standard Liquid for? Or are you trying to do something more complex?

{% for i in (1..3) %}
  Going {{ i }} time...
{% endfor %}

Which should give you:

Going 1 time...
Going 2 time...
Going 3 time...

Reference: https://shopify.github.io/liquid/tags/iteration/

Yes, my example above was just an example! I am doing something much more complex that requires looping, but which can’t be achieved with any of {% for %}, {% cycle %}, etc.