{% set entries = craft.entries.section( 'databases' ).orderBy( 'title' ) %}

{% for entry in entries.all() %}

  {% set ageGroup = _self.categories( entry, 'ageGroup' ) |spaceless %}
  {% set research = _self.categories( entry, 'research' ) |spaceless %}

  <h3>{{ entry.title }}</h3>
	<p>AgeGroups Categories: {{ ageGroup }}</p>
	<p>Research Categories: {{ research }}</p>
	<hr/>
{% endfor %}

{% macro categories( entry, category ) %}
  {% apply spaceless %}
	{# assumes category is either 'research' or 'ageGroup' #}
	{% set values = (category == 'research') ? entry.research.all() : entry.ageGroup.all() %}
  {% for value in values %}{{ value.slug }};{% endfor %}
  {% endapply %}
{% endmacro %}
