Suppose that you have a client who is not very tech-savvy. The client wants to select Resources manually to promote on the Home page with a teaser. You could create a Custom Manager Page (CMP) that lets the client select (and de-select) the pages, but writing a CMP is a fairly challenging process.
You could write code that adds options to the right-click menu in the Resource Tree to add and remove Resources from the list, but that’s even more challenging than creating a CMP.
You could create a form in the front-end for the selection, but if you have a lot of Resources, you’d have a monster drop-down list or a ton of checkboxes.
You could also create a “Yes/No” Template Variable that indicates whether a page should be shown, and either create a custom Snippet to check the TV, or use the
&tvFilters property of getResources, but then the client would have to edit each Resource to select or de-select it.
The getResources Snippet (or the pdoResources Snippet) will display teasers for you, and its
&resources property will let you specify a comma-separated list that determines which ones will be shown. We just need a way to make it easy for the client to create that list. The client could just edit the Snippet tag, but some clients don’t like to mess with anything that they consider “code”, which often includes Snippet tags. Worse yet, the Snippet tag will be most efficient if it’s in the Template—buried in a lot of other scary code. In addition, if they mess up the tag in any way, the page can look truly terrible or the teaser display will disappear altogether (and they’ll think they’ve broken the site).
Here’s a very low-tech (but fairly efficient) way to let the client select specific Resources to promote on the Home page (or any page) without seeing anything scary.
Create a Chunk called HomePageResources. In it, put nothing but a comma-separated list of IDs that get shown on the Home page (no spaces).
Put a getResources tag on the Home Page where you want the teasers to appear, with this property:
Create another Chunk with tags to serve as the Tpl Chunk for getResources (let's call it
TeaserTpl) and specify it in the
&tpl property of the getResources tag on the Home page.
[[getResources? &tpl=`TeaserTpl` &parents=`0` &resources=`[[$HomePageResources]]` ]]
&parents=`0` tells getResources to include all resources. If the resources you want to show are under specific parents, you can change this to a comma-separated list of the IDs of those parents.
TeaserTpl Chunk might look something like this:
<div class="resource_preview"> [[+pagetitle]] [[+introtext]] </div>
Now, all the client has to do to update the display is to look in the Resource tree and note the IDs of resources to be shown and type them into the Chunk (and remove the ones that should no longer be shown). If the client knows the pagetitle (or part of it) for a Resource they want to add or remove, they can use the Search box at the top of the Resource tree to find the ID.
To improve page-load speeds, you’ll want to call the getResources Snippet cached (as it appears above). That means you’ll have to remind the client to clear the site cache from the Manager’s Top Menu whenever the list changes.
The tag specifying the value of the
&resources property in the getResources Snippet tag will be replaced with the comma-separated list in the Chunk and Bob’s your uncle. The client never has to deal with (or even see) the Snippet tag.
It’s tempting to let the user list pagetitles rather than resource IDs, though getResources can’t use them. It can be done with another custom Snippet to translate the pagetitles into IDs, but it has some serious disadvantages:
For one thing, it will be slower, since the Snippet would have to loop through the pagetitles, retrieve the ID, and create the comma-separated list of them.
Another problem is that user may mistype the pagetitle, which will then never be shown.
Pagetitles may also contain characters that will interfere with a search for them.
- A bigger issue is that pagetitles can (and often do) change. Unless whoever changes the pagetitle remembers to edit the Tpl Chunk holding the pagetitles, the page will never appear again. The resource ID will never change, which makes it a much better option.
In my experience, clients rapidly learn that the ID is shown in parentheses after the pagetitle in the tree. As a bonus, the ID is easier to type than the full pagetitle.
Bob Ray is the author of the MODX: The Official Guide and dozens of MODX Extras including QuickEmail, NewsPublisher, SiteCheck, GoRevo, Personalize, EZfaq, MyComponent and many more. His website is Bob’s Guides. It not only includes a plethora of MODX tutorials but there are some really great bread recipes there, as well.