How to output a list of category groups in a Twig template.
Craft gives us a lot of ways to output categories in a Twig template. However, it’s not clearly documented how to output a category group. But, with the help of craft.app
and the getallGroups()
method available in Craft, we can easily get what we need.
Here’s an example implementation using a simple unordered list and outputting only the group name:
{% set categoryGroups = craft.app.categories.getAllGroups() %}
<ul>
{% for group in categoryGroups %}
<li>{{ group.name }}</li>
{% endfor %}
</ul>
To find out what other properties are available for a group, you can use {{ dd(group) }}
to dump and die a group and get a glimpse of the internals.
You’ll get something like this:
CategoryGroup {#1752 ▼
+id: 1
+structureId: 1
+fieldLayoutId: 6
+name: "Blog"
+handle: "blog"
+maxLevels: null
+uid: "d1ff62a0-5c47-45e9-b544-fd7e429f706d"
-_siteSettings: null
-_errors: null
-_validators: null
-_scenario: "default"
-_events: []
-_eventWildcards: []
-_behaviors: array:1 [▶]
}