MODX Revolution features a number of very powerful features that are hidden under the surface, but when discovered are hard to live without. Two of these are Element Properties and Output Modifiers. This short article will give you a quick introduction on what they are, and how they could be used.

What we will want to achieve

Do you have some kind of breadcrumbs trail in your page's  title, which should display the parent of the resource you are in? Great - but we don't want that on regular pages. Our regular pages and blog pages (where we want the parents title in our page title) are in two different templates, but they share the same header chunk. We'll want to use  the same header chunk, but make it output different contents.

So how do we do this?

We will tell the chunk in the blog template to output the parent (using an element property), and in the chunk we will add some conditional logic (with output modifiers).

Let's assume the following to be your template.

[[$core.header]]
      <div id="content">
        <h1>[[*pagetitle]]</h1>
        [[*content]]
      </div>
      [[$core.footer]]
    </div>
  </div>
</body>
</html>

What we can do there is assign a property. Assigning a property can be seen as adding a variable or telling it to do something. When we put snippet calls in our template, we tell the snippet what to do using properties. For example, a Wayfinder call could have a startId and level property telling it how to process your menu.

Those properties we assign to snippets is something we can also assign to other elements (chunks and template variables are most applicable in this case). Knowing that, we can use the following to tell the core.header chunk to show the parent:

[[$core.header? &showParent=`true`]]
      <div id="content">
        <h1>[[*pagetitle]]</h1>
        [[*content]]
      </div>
      [[$core.footer]]
    </div> 
  </div> 
</body>
</html>

Just doing that wont do much... we will need to adjust our core.header chunk to make use of the property we pass to it (showParent), and based on that output the parent's title or not. When using properties with chunks, they will be added as placeholders so we can access that with the placeholder syntax. Let's use that, and throw in the conditional logic we need.

Let's say this is the relevant part of the core.header chunk:

...
<head>
  <title>[[*pagetitle]] :: [[++site_name]]</title>
...

After making the changes, it will look along the lines of this:

...
<head>
  <title>
     [[*pagetitle]] 
     [[+showParent:notempty=`:: [[*parent:getPagetitle]] `]]
     :: [[++site_name]]
  </title>
...

Okay. Let's break down what we did there.

  • First it simply fetches the pagetitle.
  • Then it fetches the placeholder we just set with the &showParent parameter on the chunk call, and do some magic with it:
    • It first checks if the placeholder is not empty (or: does it have a value?) using the output modifier "notempty". If it does, it outputs ":: [[*parent:getPagetitle]]". If it is empty it will return the placeholder's value (which is then nothing).
    • [[*parent:getPagetitle]] is where we use another output modifier, a custom one (snippet) this time, which fetches the parent's ID and parses it with a small snippet to get the pagetitle.
  • Then it simply sets the rest of the title.

By checking if the parameter showParent is empty, you're creating a way to conditionally control the value being outputted by the chunk. Throw in a custom output filter to parse the value further, and you've created a dynamic(-ish) title.

By the way, this is the custom getPagetitle snippet we use as output filter:

if ($input < 1) { return ''; }
$obj = $modx->getObject('modResource',array('id' => $input));
return $obj->get('pagetitle');

Due to recursive parsing of tags, it is needed to check the $input variable for a valid value, and stop doing things if it aint. After that, simply fetch the resource with the ID given, and return it's pagetitle.

You are always expanding my knowledge of MODX, Mark. Thanks!!!

Oh, and your new site looks amazing ;)

Glad you like it! :D

And lots more MODX where that came from..!

Hey, in your getPagetitle snippet, if you replaced:
get('pagetitle');
with
get('$field');
could you call the snippet with
&field=`whatever`
and return any field? Is that pretty much how getResourceField accomplishes the task?

Almost, use get($field) (no apostrophes when referencing a variable).

GetResourceField works the same way, tho that also supports TVs etc so is a bit more feature rich :-)

Comments are closed :(

While I would prefer to keep comments open indefinitely, the amount of spam that old articles attract is becoming a strain to keep up with and I can't always answer questions about ancient blog postings. If you have valuable feedback or important questions, please feel free to get in touch.