Build a MODX snippet that outputs a paginated list of resources using the built‑in paginate controller
Create a reusable MODX snippet that fetches and displays a paginated resource list, with optional caching to improve performance.
14 May 2026, 00:23 UTC

Goal
Create a reusable MODX snippet that returns a paginated list of resources, letting editors place the tag on any page and control the number of items shown.
Prerequisites
- MODX Revolution 2.x or 3.x installed and accessible via the manager.
- Write permission for the snippet class file (e.g.,
core/components/yourpackage/htdocs/snippets/). - A template that contains the snippet tag (e.g.,
[[!MyPaginatedResources? &limit=5]]). - Optional: a TV or field you want to display (e.g.,
myTV) that exists in the resource template.
Procedure
- Create the snippet class file. In the manager, open
core/components/yourpackage/htdocs/snippets/and create a PHP file namedMyPaginatedResources.php. The class must extendmodSnippetand implementexecute(). - Read input parameters. Inside
execute()use$this->get('limit'),$this->get('start'),$this->get('sort'), and optionally$this->get('classKey'), providing defaults (limit = 10, start = 0, sort = 'createdon'). - Build the resource filter. Use
$modx->newFilter()to limit to the desired class; add aclass_keycondition ifclassKeyis set, otherwise default tomodResource. - Fetch the collection. Call
$modx->getCollection('modResource', $filter, $limit, $start, $sort, 'DESC'). Verify the argument order matches your MODX version (2.x vs 3.x). - Apply pagination. Pass the collection to
$modx->paginate($collection, $limit, $start)to obtain amodXPagedResultobject. - Loop through the page. Iterate over
$pagedResult->getItems(), retrieve fields such as$resource->pagetitleor$resource->get('myTV'), and output them using MODX tags or plain PHP echo. - Cache the output (optional). After building the HTML string, call
$modx->cacheManager->save('my_paginated_output', $html, $ttl)where$ttlis seconds. To reuse the cached markup, expose a second tag[[!MyPaginatedResources? &useCache=1]]that returns the stored HTML when present. - Return the markup. End the method with
return $html;so MODX inserts the string at the tag location.
Verification checks
- Run the tag with
[[!MyPaginatedResources? &limit=5&start=0]]on a test page and confirm exactly five resource titles are displayed. - Click the pagination links generated by the snippet; ensure the displayed items change and that
$modx->getTotalRows()matches the total count across all pages. - Check server logs for Execution time exceeded warnings; if present, reduce the limit or add an index on the sort field (e.g.,
createdon) to speed the query.
Limitations & troubleshooting
The paginate controller API differs between MODX 2.x and 3.x; using the wrong argument order causes a fatal error. Keep queries simple and consider indexing the sort field to avoid full‑table scans. Missing TVs referenced in the loop will generate PHP notices; verify every field exists in the resource template or TV definition.
Rollback
If the snippet throws a syntax error or breaks page rendering, delete the MyPaginatedResources.php file via the manager or SSH. The site reverts to its previous state without a database rollback, because the snippet is code only.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.