# Using Fragments
# Querying fragments
To query a list of fragments in a zone to display in your templates:
{% set fragments = craft.fragments.zone('myZoneHandle').all() %}
Once you have queried the list of fragments, you can access the fields the same way you do for Entries.
{% for fragment in fragments %}
<h1>{{ fragment.textField }}</h1>
<img src="{{ fragment.imageField.one().url() }}" />
{% endfor %}
To target only fragments of a certain fragment type, modify the previous query like this:
{% set fragments = craft.fragments.zone('myZoneHandle').type('myFragmentTypeHandle').all() %}
# Fragment visibility
By default, fragments created in a zone will always be included when the zone is queried.
To override this, each fragment can be configured to be included or excluded from the zone based on the current page URL using the visibility rules.
Currently, only URL rules (full paths and wildcards) are allowed.
# Fragments field
The Fragments field allows your content editors to select fragments from all or some of the zones available.
# GraphQL support
GraphQL is supported from version 1.1.0. An example query is shown below:
{
fragments(zone: "myZone", type: "myType", currentUrl: "/my-page") {
__typename
uid
title
... on myFragmentType_Fragment {
textField
}
... on anotherFragmentType_Fragment {
imageField
}
}
}
The arguments zone
and type
work the same way as the Twig example above.
Use currentUrl
to pass in a URL that can be used to match against any fragment visibility rules that you have set up.
← Configuration Tips →