Every now and then I get a request to make certain lists sortable. Maybe a law firm wants their employee list to be sortable by name or practice. Maybe it’s a production company and they want their films to be sortable by genre, year, and alphabetically. Databases are the usual way to go when sorting information is involved.
However, if you are a MODx user you can pull this off by simply using getResources. This is beneficial for a few reasons:
- You don’t know how to use databases
- The client plans on maintaining their site once you are done
- You simply don’t feel like dealing with databases for this project
- It’s fun trying something different every now and then
Back in the day, before MySQL was an integral part of everyday web building, you would simply create multiple pages with the same info but arranged differently. Then in your “sort” menu you would simply link to the proper page. The downside to this way is that if any information changes (like a phone number, position, a new employee, removing a former employee) you have to make that change on all pages you created.
The common method would be to simply put all that info in a database and then simply call on that info as you see fit. This is obviously the best and most versatile way of doing this. There really is no downside to using this method unless you simply have no idea how to use databases and don’t have time to learn.
MODx and getResources sits in the middle of these 2 methods. You are basically following the old school method, in that you are creating multiple pages for the various sorting states. However, if any edits are needed in the future you only have to make that change once.
Let’s run through this process using the employee example. I am going to assume that you are familiar with MODx and somewhat familiar with using Snippets like getResources or wayFinder.
Create Your TVs and Build Out Your Employee Pages
So – if you have a root level page or directory called “Employees”. This will be the parent of all employees. All of the employee’s info will be marked up in the Resource Content area of these pages. This is what people will see when they click on the employee’s linked name. This area has nothing to do with what we will be doing. We will be using Template Variables (TVs) to set up each employee’s sorting ability.
You can use as many TVs as you like. You can sort a list 10 different ways if you prefer. In the spirit of keeping things simple I will talk about just a few. In this example I will tell you how to sort by position and also alphabetically. We are going to make one TV called “emp-position”. This will be the one that getResources will be looking for. You will also need to create a few more TVs to accommodate the template file for your results. (Assuming you want to display the name, picture, position, phone number, etc… in your results.)
On every employee resource you create you will need to fill these TVs out with their information. Do yourself a favor and alphabetize the order of the employee resources. Remember you can drag and drop resources in Revolution. It’s really easy to reorder your resources. Doing this will make things a bit easier in the end.
Now you will need to create and save a template chunk. We’ll call it “TPLemployees”. It might look something like this:
<a href=”/[[+alias]].html”><img src=”/images/[[+emp-img-sm]]” alt=”[[+emp-name]]” /></a>
<p><a href=”/[[+alias]].html”>[[+emp-name]]</a><br /><em>[[+emp-position]]</em><br />[[+emp-phone]] </a>
So given this, you will have needed to create the following TVs (and filled them out on each resource):
Most of your work is done at this point.
The Sorting Page(s)
Next you will need to create a hidden-from-menu resource. This will be your sorted results page. In the Resource Content of this page you are going to call up a list of employees that meet your sort criteria using a getResources snippet call. This call will look something like this:
Now put this call on your root employee page:
What’s Going On With All of This?
Let me explain what the getResources snippet is calling on:
TPLemployeesis the template file you created. It provides the mark up of the basic info that you entered in all those TVs. This template applied to each resource under the “Employee” directory you created.
7tells getResources where to look for all of you employee resources (or pages). The 7 is the ID of the employee directory. This will most likely be a different ID for you.
1are necessary in order for getResources to process TVs. It is not necessary to change any of these values for the sake of this tutorial. However, you should read up on them to fully understand them.
50simply limits your results to 50. This can be any number.
ASCsorts the employees in ascending order depending on the Menu Index of each resource. Good thing you alphabetized your employee resources!
ManagementThis is the magic TV. It is saying to filter out all employees with a TV value of “Management”. This value can be anything, as long as there is a TV with the same value in any of your employee resources.
View the Magic!
Now when you view your “employees.html” page it should display all employees in ascending alphabetical order. Build a mini sorting nav so that those viewing your site can click a link and have the proper results served to them. So when they click on your “Management” link, only employees labeled as “management” will be displayed. Add a “All Employees” link to you sorting nav on the sorted pages and point it back to the root employees page.
You’ve Scratched the Surface
This is a very basic run-through of this technique. With the right amount of TVs and properly labeled snippet calls, you can organize those resources any way you see fit. getResources is a very powerful and useful tool inside of MODx. Read more about it to see what else you can do. I hope this walkthrough was helpful. Understanding how to use add-ons like getResources or wayFinder will have you feeling and looking like a MODx pro!