Sections
|
<( Rendering Arrays )>
Arrays are extremely useful when you want to render a whole bunch of objects of the same time. If you want to render all of the elements in an array, one after another, all you need is one simple reference using an empty pair of brackets:
Here are all of the Date elements in arrayOfDates: <(arrayOfDates[])>
|
This is great in that you don't have to reference 50 different objects manually, but what if you wanted to put HTML line breaks (<br>) in between them? You could make a render definition that adds the line breaks at the beginning or the end, but then you'd have extra line breaks somewhere.
Luckily, DML2 has a solution for this: array spacers. To add some arbitrary code in between the elements of an array, just follow the reference to the array immediately with a tag that reads <(&spacer)>, follow this with whatever text you want inserted in between elements and then add a closing <(/&spacer)>.
Here are all of the Date elements in arrayOfDates with HTML line breaks in between: <(arrayOfDates[])><(&spacer)><br><br><(/&spacer)>
|
Now for a new problem: You've got your web site's news in an array of DML2 objects, but (in order to save bandwidth), you only want the latest 5 news updates to be shown on the main page. To do this, you can use ranges of arrays. Inside of the brackets where you'd normally put the number of the element you want to render, just write the number of the first element to render, followed by a comma, and then the number of the last element to render. To render elements 1-5, for example, you'd just need <(arrayOfDates[0,4])>. You could optionally follow this by a spacer (just like in the entire array rendering).
But what if you just want to render the last 5 elements of an array? You don't always know how many elements are in an array, so you can't hard-code the numbers. Instead, just insert a pound sign (#) in the range. The pound sign means the number of elements in the array.
! | Note:
Remember that the first element of an array is 0. Therefore, the pound sign (which returns the number of objects in the array) is actually referencing an object outside of the array's bounds. Put another way, if you have 5 elements, the last element is referenced as 4, but the pound sign evaluates to 5.
|
Of course, I still haven't solved the problem of rendering only the last 5 elements of an array. This is done by typing in simple math equations (addition and subtraction) into the range. The end result looks rather cryptic, but if you break it down into small chucks, it makes perfect sense. An example is in order here.
Here are the last 5 elements of arrayOfDates: <(arrayOfDates[#-5,#-1])>
Here are the first 5: <(arrayOfDates[0,5])>
|
! | Note:
If you run out of the boundaries of the array, DML2 doesn't care and won't do anything. This is useful if you're doing something like a ChangeLog that only has 1 entry (so far), but you set up the code to render the last 5 entries. You can just tell it to render the last 5 and DML2 will just render the whole array if it has less than 6 elements.
|
|
|