Slice
Return a slice of a list (array).
This is useful if we wish to extract only a certain number of elements from an array, for example splitting a large list into two lists.
Arguments
The filter has two arguments start and end.
These tell the filter which elements to return from the list.
Both arguments can be either a number (positive integer) or a keyword.
Note
Mixing numbers and keywords is not allowed
i.e. either both arguments are numbers or both arguments are keywords
The keywords are:
“start” - the first element
“middle” - the element that is in the middle of the list
“end” - the last element in the list
Example arguments
start |
end |
Comment |
“start” |
“end” |
returns the whole list |
“start” |
“middle” |
returns the first half of the list |
“middle” |
“end” |
returns the second half of the list |
0 |
1 |
returns the first element |
1 |
3 |
returns the second element |
0 |
4 |
returns everything from the first UP TO the fourth element |
Example usage
Template
{# damage | slide:"start":"middle" } {component}, {/}
Output
NSF Bumper, Front Bumper, OFF Bumper, Grill
Using slice to save vertical space
Sometime we show a list of items as two columns.
Each column displaying a different value from the item e.g. label and agreed.
When we have a very long list, this pattern will waste a lot of space.
To work around this we want the whole list split into two columns.
For example we want to display a table like this
Label |
Agreed |
Label 00 |
£100.00 |
Label 01 |
£100.00 |
Label 02 |
£100.00 |
Label 03 |
£100.00 |
Label 04 |
£100.00 |
Label 05 |
£100.00 |
Label 06 |
£100.00 |
Label 07 |
£100.00 |
Label 08 |
£100.00 |
Label 09 |
£100.00 |
Label 10 |
£100.00 |
As two columns:
Label |
Agreed |
Label |
Agreed |
Label 00 |
£100.00 |
Label 05 |
£100.00 |
Label 01 |
£100.00 |
Label 06 |
£100.00 |
Label 02 |
£100.00 |
Label 07 |
£100.00 |
Label 03 |
£100.00 |
Label 08 |
£100.00 |
Label 04 |
£100.00 |
Label 09 |
£100.00 |
Label 05 |
£100.00 |
Label 10 |
£100.00 |
We can use the slice filter to achieve this. See the following example:
Notice that in the left column of the inner table we call slice with start and middle, and in the right column we call slice with middle and end.
See slice.docx