Document ObjectI am currently developing a MODx site and thought I’d take a break and write a little post about using the MODx Document Object which uses document specific variables to affect output evaluated against the value of a specific Document Object(s) (complete list). I thought this might be particularly helpful since I believe this to be a powerful yet poorly documented component of MODx. Here I am going to use them in a snippet.

In my example I have a site that has a splash page posessing two links allowing you to enter 1 of 2 sub-sites from that point. My scheme is to have either a splash.css, an option1.css or an option2.css called into the header. I am wanting to limit my call in to only one of these as it will affect the graphics and color schemes of my documents.

Document IDs

I am using the document IDs to drive my choices here. The splash page has ID#1, option one has the ID#2 and option two has the ID#3. Except for the splash page everything is housed under ID#2 or ID#3 as containers.

So my task is to identify which ID I am currently on (landing pages with ID#1, #2 or #3) or which parent ID I am currently below (a child of #2 or #3).

The Snippet

<?php
$CSS1 = $modx->documentIdentifier;
$CSS2 = $modx->documentObject['parent'];
if ($CSS1=="1")
  $output="{{splashCSS}}";
else
if ($CSS1=="2")
  $output="{{option1CSS}}";
else
if ($CSS1=="3")
 $output="{{option2CSS}}";
else
if ($CSS2=="2")
  $output="{{option1CSS}}";
else
if ($CSS2=="3")
 $output="{{option2CSS}}";
return "$output";
?>
  • Thanks a lot. I've been working with MODx for a few months now and I'm eager to go beyond the basics and start writing my own snippets.
    I assume the [[snippetCall]] goes in the <head>.
  • Yes, in this example I am using it in the head to call in varying CSS files.
blog comments powered by Disqus