One of the many great things about
MODX is how you can take many routes to accomplish a given task. This is great because as you get more comfortable working with snippets and plugins to tweak your website as you intended you can quite easily go back to your code and realize you could have
optimized certain things much more. This is something that happens to me every single day when maintaining my websites, so I decided to make it a working philosophy to
spend more time building and less time maintaining.
As I am progressing experimentation with Revolution, I have come across multiple situations where smarter coding will save me tons of time updating websites in the future and making it much more
"user friendly" for my customers. The purpose of this thread is to share precisely these simple little tricks that will save you time in the future.
Today's tip is using the snippet getResources, which I've learned to love almost as much as BBQ.
If you are coding your HTML correctly, you are most likely using classes on certain blocks to indicate when a list of repetitive blocks is starting or ending. For example, when displaying several columns on your HTML code, where you separate each by a margin on the right, you will most likely have a class defined on the last column, probably named "last". This is useful because you can then code in your CSS that the column with the class "last" should not have a right margin. This will earn you some more pixels to play with.
But when you take this code into MODX, there are so many ways to format this HTML code using snippets and chunks that you might fail to realize the most efficient way to code it so it takes you less time to maintain or make further modifications to your templates. The ways people could work around this are numerous:
- If you are in a hurry you will probably leave your structured html code in the template to make sure it doesn't break and repeat snippet calls within each column to pull the data you want. This is not awesome because you are repeating commands which can be grouped into a single request, making the processing of your page a lot faster. This also isn't ideal as you are fixing display as governed by your HTML structure whereas you could be using dynamic values to adjust and allow your client more customization when updating content.
- Other people would make a single call (as recommended) and use two template files, one for the general results, and one specific for the last result where the "last" class can be then inserted. getResources provides a variable for this: &tplLast, and until recently this is what I was using. But it bother me having two similar chunks with just 1 difference. So i was looking for a way to make it work with a single chunk and a single request.
So I realized something really awesome by reading the documentation at:
http://rtfm.modx.com/display/ADDON/getResources
Yes, reading the manual from time to time is awesome! What I realized was a very simple placeholder I could setup in my request. This is called "idx". The way it works is pretty simple, you set it up in your request and assign it a numeric value. This value is then used on each result that comes from your request. So if I have in my request &idx=`0` or use the default of 1, then each result will have its own numeric value on increments which you can use in many clever situations.
So I had two different types of columns I needed to adjust. First I had a 3 column setup where I required the third column to have a class of "last", since that would always be my last column. This was as simple as:
<div class="col2[[+idx:is=`3`:then=` last`]]">
Nothing fancy, just a simple output modifier to check if the idx value on the entry was 3, if it was, then it would add the "last" class. If it wasn't it would just not display anything.
By doing this simple modification I saved myself 1 chunk, which makes future updates on the template a lot simpler and faster since its all centralized on a single file.
But I had another columns issue that was a bit more complex. I had a row of two columns that I wanted to be flexible with. I wanted to allow the customer to be able to dynamically select how many entries would be output, furthermore, based on the amount of entries I would need to use different HTML elements so that my columns would adjust accordingly, so if it was more than 2 columns, then I would need a new row of two columns and so on. So simply relying on the number of results wasn't going to cut it. I needed something smarter. So after reading further into the output modifiers I found something awesome!!
There is an output modifier that allows you to determine "even" and "odd" numbers, so with this, I could mark every 2nd column (even number) to use the "last" class with a very simple check:
<div class="col4[[+idx:mod:is=`1`:then=` last`]]">
The result? every two columns it adds the class and by doing this I am able to allow an unlimited amount of entries the client can choose within his admin, I'm using contexts to store site-wide settings and template variables to store page-wide or category-wide settings (I'll write more on that some other time). So the client has full control and my HTML wont break.
Isn't it awesome? and again, I don't have to rely on a helper chunk for the last item, so I have strictly one chunk and 1 request, making my webpage a lot faster.
There are a lot of similar situations where clever use of what's available on the core can save you a lot of time in the future, so I would like to encourage everyone who likes to find alternatives ways or more optimized ways to do the same thing to explore such ideas, and while you are at it, share them here! MODX is awesome for this.
Cheers,
Jose R. Lopez