How-to: Dynamic Content Blocks with MODX getResources

Aug 25, 2013

Do Everything with getResources

getResources is the all-in-one utility snippet for MODX. Some examples of its infinite usages can be found here, here and here...and below, is yet another really useful application, brought to you by Lars (see author bio).

Content Blocks with getResources

If you have ever used getResources, you probably know the templating properties "tpl", "tplOdd", "tplFirst", etc. which allows you to use different chunks for your output, depending on the position of the Resource in the result set.

But there's more: The property "tplCondition". With this you can define a chunk for your output depending on the value of any available Resource Field.

Take a look at this simple call:


This will use the chunk "chunkA" for your output if the resource uses template ID 2; "chunkB", if the resource uses template ID 3; and so on. As mentioned, you can use any resource field available.

I came across this when a customer wanted to create pages that would include recurring "content blocks" like "Text only", "Image Gallery", "Video Gallery", "Table", etc. - but the position and number of those content blocks weren't fixed and setting up hundreds of templates wasn't an option :).

Here's a diagram:

MODX Content Blocks

Something similar can of course be achieved with MIGX(db) for example. But that's what I like about MODX - many ways to do things. And if I read the roadmap correctly, something like "content blocks" may be built in in MODX 3.0 and I'm really excited to see what will happen!

Some benefits:

  • Change the position of content blocks by drag and drop in the resource tree. Give your customers some creative freedom and the feeling of control :).
  • Easily create anchor navigation menus with "Wayfinder" or similar. Great for One-Pagers!
  • Publish or unpublish content blocks with the built in resource fields "Publish Date" and "Unpublish Date".
  • Show or hide content blocks depending on users or user groups with the built in resource ACLs.

Some tools:

Alias plugin - A resource's alias is mandatory when using FURLs and has to be unique. Here's a little plugin that creates a unique alias for content blocks to prevent problems when using the same page title for multiple content blocks. *Editor's note: code is not tested nor reviewed, although I'm sure it's better than my own ;)

// Define the parent's template Id. Child resources will get a unique alias that's not built from the resource's page title.
$templateId = 1;  
// Activate the system event "OnBeforeDocFormSave" in your plugin settings.  
if ($modx->event->name == 'OnBeforeDocFormSave') {
    $parentId = $_REQUEST['parent'];
    $parent = $modx->getObject('modResource', $parentId);
    if ($parent !== NULL) {
        $parentTemplateId = $parent->get('template');
        if ($parentTemplateId == $templateId && $_REQUEST['alias'] == '') {
            $alias = uniqid();
            $resource->set('alias', $alias);

Redirection snippet - If (by mistake or on purpose) a content block is viewed in the frontend directly, the visitor will see a blank page.(*Editor's note: depending on the Template assigned.) Here's a litte snippet that, when called from the content block Resource's template, will simply redirect the visitor to the Resource's parent. *Editor's note: you can also use the redirectoid Snippet and pass 2 to the &id property, or set it in the Snippet's properties tab :P

$parentId = $modx->resource->get('parent');  
$url = $modx->makeUrl($parentId);  

That's it! Thank you Lars for that awesome feature review - yet another web development task made easy with MODX and getResources. Yay!