Working with Modx without any extra

Hi all,

I already use Modx for 3 years and I always used extras, mainly pdoTools and formit.

To be honest, since the first time I tried to play with Modx, I started also with extras. This is also encouraged by the Modx documentation itself.

Out of curiosity I have been looking around for the native modx snippets, but with little success.

What are the native snippets that would allow me to generate a list of resources, for instance?
Do I need to write a snippet for that if I don’t want to use any extra?

The objective is a very simple implementation of a basic site that would only require some pdoTools snippets (pdoResource, pdoMenu) and nothing more. The idea is to understand what Modx offers natively helping to reduce external dependencies and possible security flaws like seen previously with formit and gallery.

Thanks all in advance

There are no native MODX snippets per se, but it’s quite easy to write your own, especially for basic operations such as retrieving objects, etc.

Here are a few methods you’ll find useful as you contemplate writing snippets:

To get you started, I recommend the official documentation to find out how to write a good snippet.

For example, the following snippet returns a list of published resources that are children of resource 23:

The snippet

<?php
/**
 * listResources
 *
 * Usage example:
 * [[listResources? &tpl=`myTpl`]]
 */
// grab the resources
$docs = $modx->getIterator('modResource',array(
    'parent' => 23,
    'published' => 1,
    'hidemenu' => 0
));

// loop through results
foreach ($docs as $doc) {
  // copy object fields into an array
  // to make their values available inside your chunk via [[+fieldname]] as seen below
  $fields   = $doc->toArray();
  // define the chunk used to display results. If none is specified, myTpl will be used
  $tpl      = $modx->getOption('tpl', $scriptProperties, 'myTpl');
  $output[] = $modx->getChunk($tpl, $fields);
}

return implode('', $output);

The templating chunk

<!-- myTpl-->
<li><a href="[[~[[+id]]">[[+pagetitle]]</a></li>

Inside your resource or template, the following should display all published resources that are children of document 23:

<ul class="list-unstyled">[[listResources]]</ul>

Hope you’ll find this useful.

Many thanks!
This is exactly the answer I was expecting.

Cheers

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.