Use object in for loop

I’m using the same layout for pages in different categories and wondering if there is a way to call object page.categories from the front matter in for loop.

{% for page in site.collection limit:3 %}
{% if page.categories contains ‘{{ page.categories }}’ %}

{{ page.title }}


{% endif %}
{% endfor %}

Please indent your code with 4 spaces before it or use backticks ``` before and after so it is monospaced.

```
{% for ... %}
{% endfor %}
```

e.g.

{% for ... %}
{% endfor %}

Your first issue is that your variables are colliding as names.

Page is already a variable.

So use p or collection_page or item.

{{ page.categories | inspect }} Categories of current page 

{% for p in site.collections %}
  {{ p.categories | inspect }} categories of current item 
  {% if p == page %}
...

Aim

What are you trying to achieve? In terms of the frontend result of displaying collections and categories? You might be able to do something similar using site.categories.

Code

Your comparison of categories seems weird with the quotes as that will not evaluate.

Plus it’s bad a idea to check if an array contains another array or hash. Try displaying an object to understand it before you use it.

```json
{{ p.categories | jsonify }}
```

```json
{{ page.categories | jsonify }}
```

Categories not in collections

Plus categories are not supported in collections, which I cover more on below.

Note that collections cannot have categories in the usual sense. Only posts can. Well, posts is also a collection, but other collections won’t have categories.

Yes you can set categories on any page like a collection. But it will not add to the grouping of all categories in site.categories.

Further, a post will convert this string implicitly in Jekyll to an array

categories: abc def
# to
categories:
  - abc
  - def

While any non-posts that have categories won’t get that benefit. You’ll have to be explicit with the array form.