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.
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:
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:
After making the changes, it will look along the lines of this:
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:
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.