Direct Access to modResource and its respective Template Variables for Ajax Implementation


A few suggestions involved the use of the venerable getResources() and pdoResources Packages. I have to completely agree with Paul Murphy as to the weight of these tools as they are excessive for such a simple task.

I discussed many of these topics in my book in Part II, where I demonstrate some AJAX interactions with rowboat, getResources, and homegrown snippets which were much faster and lighter on the server. In this implementation, I am bypassing all the security features of MODX and directly interacting with modResource objects.

base getResource Snippet

$modResourceObj = $modx->getObject ( 'modResource', 816 );
$templateVarInfo ='';
$out = '';

if ($modResourceObj instanceof modResource) {
	$resourceArray = $modResourceObj->toArray ();
	$resourceArray ['content'] = $modResourceObj->getContent ();
	$templateVarCollection = $modResourceObj->getTemplateVars ();
	if ($templateVarCollection) {
		foreach ( $templateVarCollection as $modTemplateVarObj ) {

			if ($modTemplateVarObj instanceof modTemplateVar) {

				/* dumps a join of template vars for the resource and their respective values */
				// print_r ( $modTemplateVarObj->toArray () );

				/* Shows just the output in the manner they were intended to output - but may not work well for straight screen display */
				// echo $modTemplateVarObj->process();

				/* Shows only values without formatting or what they belong to */
				// echo $modTemplateVarObj->getContent();

				/* Creates a simple display of name and value */
				$templateVarInfo .= $modTemplateVarObj->get ( 'name' ) . '=>' . $modTemplateVarObj->get ( 'value' ) . '<br>';
	$resourceArray['templateVars'] = $templateVarInfo;

	/* at this point everything is in the array */
        // print_r ($resourceArray);

Further Development

As alluded to by the responders to the question, the content will include the Template assigned to the page if one is designated to be used when the page is viewed. This could be a problem unless the source page is using an empty template, or if a bit of forward-thinking were used in its creation. By placing the entire header and footer in chunks they can be subsequently removed using the :replace=`[[$headerChunkName]]==`:replace=`[[$footerChunkName]]==` filter on the content area. By using the replace filter in our implementation the page will also be viewable to regular site visits.

To begin with I would template the TemplateVars in a chunk:



Template the Template Vars

// $templateVarInfo .= $modTemplateVarObj->get ('name') . '=>' . $modTemplateVarObj->get ('value') . '<br>';

$templateVarInfo .= $modx->getChunk('getResourceTemplateVar', $modTemplateVarObj->toArray());

Any attribute contained in the $modTemplateVarObj will be available to be templated, including [[+caption]]. Once this loop completes, you should have a formatted string ready for use in final component. If you don't have any output, make sure the chunk name is spelled correctly and that it actually exists in the Element Tree


<div class="row">
  <div class="col-md-8">
  <div class="col-md-4">
     <dl class="dl-horizontal">

Applying some Bootstrappery (version 3) I am quickly provided a decent display of the Resource attributes. I added filters to the content area to remove the header and footer, as well as limit the number of characters which are being return. You can do this if you would like or simply utilize [[+content]] if your implementation will work with the full page contents being shown.

Template the output

$out = $modx->getChunk('getResourceTemplate', $resourceArray);
/* Return the output if it is populated, otherwise dump the array */
return ($out) ? $out : print_r( $resourceArray );

Simply insert the above line at the end of the getResource Snippet code. The contents of the Template Variables will be inserted into the final output providing a formatted display for your implementation.

Making the Snippet Dynamic


This can be used as a static snippet call: [[!getResource?doResourceAction=`vIeW`&resourceId=`99`]] or as a $_GET or $_POST query string: somePage.html?doResourceAction=`vIeW`&resourceId=`99`. This allows the ajax to be tested before it is actually used ensuring it is actually functioning as intended before having to deal with unexpected responses from the AJAX response.


Duis autem cavete! The snippet is extremely fast and light on the server, but it also completely bypasses all forms of MODX Revolution ACLs and security. Use this at your own risk. Make sure you are pointing Wayfinder or what ever you are building the links from to a directory/container on the site it is safe to run on. This implementation relies on the underlying Snippet (getResources, Wayfinder, etc.) to handle ACLs correctly.

Wayfinder Row Template

<li [[+wf.classes]]><a href="[[]]?doResourceAction=`view`&resourceId=`[[+id]]`" [[+wf.attributes]]>[[+wf.linktext]]

This is just an example. If you are using Wayfinder to create the desired links, simply duplicate the row being used to create the links and modify it accordingly. Lastly, change the snippet call to use the new row Template chunk you just created.