Search ancestors

Search upward with findParent

Looking down the Resource Tree is pretty natural in MODX with addons like getResources, but what if you need to look up?

findParent will search up a resource's ancestor tree looking for the first match with a given property or template variable. Order starts with the resource's parent then moves to grand parent and so on.

• From Chunks •

Let's do an example. Below we are saying "starting at the current resource, look upward (from parent to grandparent and so-on) for a resource with a pagetitle of Women's Jackets and if found return it's id".

[[!findParent? &id=`[[*id]]` &haystack=`pagetitle` &needle=`Women's Jackets` &return=`id`]]

Maybe you want to look upward for a specific template type. This will look upward for an ancestor with a template of 6 and return it's alias.

[[!findParent? &id=`[[*id]]` &haystack=`template` &needle=`6` &return=`alias`]]

• From Snippets •

Of course findParent can also be called from a Snippet like so:

$s = $modx->runSnippet('findParent',array(
   'id' => '25',
   'haystack' => 'template',
   'needle' => '6',
   'return' => 'id'
return $s;

Comments (0)

Add a Comment

Allowed tags: <b><i><br>Comment:

** Look up, you might find something **