Open randohinn opened 8 years ago
@randohinn You could consider something like this (assuming that postcount
is set to 4):
{% assign blog_1_name = "blog 1" %}
{% assign blog_2_name = "blog 2" %}
{% assign blog_3_name = "blog 3" %}
{% assign blog_4_name = "blog 4" %}
{% for i in (1..postcount) %}
{% assign varName = 'blog_%1_name' | replace:'%1',i %}
<div class="col-xs-12 col-md-6 col-md-offset-3 blogpost-link">
<h2>{{ [varName] }}</h2>
</div>
{% endfor %}
@freakdesign Nope, nothing prints out...
Not a very nice solution, but what you can do is put your blog names into an array (for example by split
ing a string like "blog 1|blog 2|blog 3") and then indexing into that array (like names[i]
). Not sure if there's a nicer way, at least not unless you control the environment in which Liquid is used (but I guess this is in a Shopify theme).
Hope that helps.
It's not a theme, so I have near-complete control over the environment. Building with gulp-liquify
if that's to be considered a limitation.
I have a list of
{% assign %}
tags that go something like this:And then I have another assign, called
blog_count
and set to4
. I would now like to loop over theblog_count
and display the specificblog_X_name
, where X is the current iteration. Tried something like this. Didn't workCan this be done?