Repeat loop of Jekyll Data [duplicate] - jekyll

This question already has answers here:
How do a loop n.times in Liquid templating using an existing for loop
(3 answers)
Closed 7 years ago.
Is it possible to repeat loops of data? Something like so:
{% for competition in site.data.competitions loop:3 %}
Which would loop and export the the data 3 times?
It would just save me making lots of fake data =)
Thanks!

I don't think that there's a Liquid filter to do it, but you can always put a for in a for:
{% for i in (1..3) %}
{% for competition in site.data.competitions %}
...
{% endfor %}
{% endfor %}

Related

Searching for page in site.pages by path without iteration

I have a path to some page from root in Jekyll as a variable path. I want to get some variables from FrontMatter of that page. How could I find this page in site.pages without iterating over all pages?
I mean something like
{% assign aim = site.pages[path] %}
instead of
{% for p in site.pages %}
{% if p.path == path %}
{% assign aim = p %}
{% endif %}
{% endfor %}
Will this solution be faster for a site with a thousand of pages?
You can use the where liquid filter for this:
{% assign aim = site.pages | where:"path",path %}
Thanks #β.εηοιτ.βε for the hint!
That was what I expected, but when I used this template, I've faced the problem: the output of {{aim.title}} was empty.
The where filter produces list even it consists of just one element! But each filepath in a file system points exactly at one file, so I expected that aim will be the page, not a list. To fix this, I've added first filter:
{% assign aim = site.pages | where:"path",path | first %}
And now aim is the page variable I am searching for.
About speed. This solution builds a site 2X faster on my hardware.

How do i compare a String out of a list in Jinja2 to another string?

I'm using a for loop and inside that forloop i generate tables out of DataBase data.
The thing is that i need to show multiple tables, but the if statement in Jinja2 doesn't seem to work for some reason. I need to check if the string on index 2 of my_list[2] is equal to the 'None'.
{% if '{{my_list[2]}}' == 'None' %}
There is no connection.
{% else %}
There is a GOOD connection.
{% endif %}
Why isn't this working?
{% if not customer_list[2] %}
This does exactly wat i needed.

Counting items in Django templates

Is it possible to tally items being listed as part of the django template within the html?
For example, I have a django template with the following code snippet in it:
<div>
{% for thing in thing_list %}
{% if thing.status == "n" %}
<a>{{ thing.status.count }}</a>
{% endif %}
{% endfor %}
</div>
This django template displays all of the things in a list, and I can call each attribute of the thing and display it, so I know I have access to all of the fields.
I want to count then number of "things" and display that number as text. My current attempt above isn't working. Does anyone have any suggestions?
As Willem says, you should do this in the view. Rather than passing a list of all Things and then checking their status in the template with if, you should filter your Things when querying them from the database in the first place:
thing_list = Thing.objects.filter(status='n')
Then you can do just {{ thing_list.count }} in the template.

Uglify File but don't minify certain strings

Is it possible using UglifyJS2 with gulp to effectivly ignore certain strings such as:
{% if VARIABLE == VARIABLE %}
{% else %}
{% elseif VARIABLE > VARIABLE %}
{% endif %}
Essentially, we are using liquid in our js as we are on the Shopify eCommerce platform. But we need the liquid to remain exactly how it is so that Shopify can process the liquid.
Many thanks for any help you can provide!

How to replace a string with linebreaks in Jinja2 [duplicate]

This question already has answers here:
Passing HTML to template using Flask/Jinja2
(7 answers)
Closed 6 years ago.
I have some data in jinja2 like this
'item1|item2|item3'
And I want to turn it into rendered linebreaks. However, when I replace it with br/ tags, I get the br tags rendered on the page. So
{{ 'item1|item2|item3' | replace("|", "<br/>") }}
renders as
item1<br/>item2<br/>item3<br/>
When I want
item1
item2
item3
on my page. I feel like I'm missing some obvious trick here...
This has to do with autoescaping. Solution that worked for me was:
{% autoescape false %}
{{ 'item1|item2|item3' | replace("|", "<br/>") }}
{% endautoescape %}