Calling MODX getResources in Code

In the previous article, we looked at a way to display a “teaser” for upcMODX logooming blog posts and set the &classKey property to select the resources you want. In this one, we’ll ditch most of our code and call getResources to get our list of resources. Note that you can call pdoResources, which is significantly faster, instead of getResources. The pdoResources snippet requires you to install the pdoTools package, but the properties are, for the most part, identical. Typically, the only change you need to make is that instead of &includeTVs=`1`, for pdoResources you need to provide a comma-separated list of TVs you want to include.

There are really only two situations when you’d want to use this technique. One is when you need the formatting and/or selection properties of getResources but want to show nothing when no results are returned. The other is when you need to modify the final results returned by
getResources using str_replace() or preg_replace().


Tpl Chunks

We’ll only use the outer Tpl chunk from the previous articles, though you’ll probably want to change it to meet your needs. You can still use the same inner Tpl but you’ll specify it in the &tpl property that will be passed on to getResources. Here are the chunks we used, to refresh your memory:

Here’s the outer Tpl chunk:

[code language=”html”]
<div class="upcoming_teaser_div">
<h3>Upcoming Articles</h3>

<div id="upcoming_teaser_outer">
<ul id="upcoming_teaser_ul">

Here’s the inner Tpl chunk:

[code language=”html”]
<li class="upcoming_teaser_li">
[[+pagetitle]] ([[!+pub_date:strtotime:date=`%a %b %d, %Y`]]) &mdash; [[+introtext]]


The Tag

The tag we use will be significantly different from the one we used in the previous articles. We’ll call our snippet MyGetResources. The tag will simply be the properties you would send to getResources with the addition of our &outerTpl property:

[code language=”html”]
… all other getResources properties here


The Code

We’re going to pass all the properties except the innerTpl property on to getResources when we call it with $modx->runSnippet().

[code language=”php”]
/* MyGetResources snippet */

/* Get outer Tpl chunk name from the properties */
$outerTpl = $modx->getOption(‘outerTpl’, $sp,

/* No need to pass this to getResources */

/* Pass all the remaining properties to getResources */
$result = $modx->runSnippet(‘getResources’, $scriptProperties);

if (empty($results)) {
/* return nothing */
$output = ”;
} else {
/* No use getting this unless we have something to show */
$outer = $modx->getChunk($outerTpl);

/* If you need to modify the $results, do it here */

/* Plug the results into the outer Tpl chunk */
$output = str_replace(‘[[+upcoming_inner]]’, $inner, $results);

return $output;


How It Works

The second argument to $modx->runSnippet() is simply an array containing the keys and values of the properties. Since these already exist as the $scriptProperties array, all we have to do is pass that as our second argument. As far as getResources is concerned, this is exactly the same as if we had called getResources in a snippet tag with those same properties. What getResources returns is a big string containing its formatted output.



Start by using getResources instead of MyGetResources in the snippet tag. Once you get the output you want, just change the snippet name in the tag to MyGetResources and add the &outerTpl property.


The Where Clause

The &where property allows you to specify complex selection criteria for getResources. The value of the property is a JSON string. Sometimes, it’s easier to specify your criteria in a PHP array, especially if you need to call PHP functions like time() for part of it. You can do this by creating the PHP array in the snippet and converting it to a JSON string with $modx->toJSON(). Using this method, there’s no need to include the &where property in the snippet tag. Here’s an example taken from our previous snippet:

[code language=”php”]
$where = array(
‘parent’ => $parent,
‘published’ => ‘0’,
‘pub_date:>=’ => time(),
‘class_key:IN’ => $classKeys,

$scriptProperties[‘where’] = $modx->toJSON($where);

Be sure this code is above the call to $modx->runSnippet().


For more information on how to use MODX to create a web site, see my web site Bob’s Guides, or
better yet, buy my book: MODX: The Official Guide.

Looking for quality MODX Web Hosting? Look no further than <a
href=””>Arvixe Web Hosting!

Tags: , , | Posted under MODX, MODX | RSS 2.0

Author Spotlight

Bob Ray

Bob Ray is the author of MODX: The Official Guide and over 30 MODX add-on components. He hosts Bob's Guides, a source of valuable information for MODX users, and has been very active in the MODX Forums with over 19,000 posts.

Leave a Reply

Your email address will not be published. Required fields are marked *