{reverse:ids:limit}

Description

Reverses the order of a dash-separated list of ids and returns it joined again by dashes. It is the deterministic counterpart of shuffle, which uses the same code but randomises instead of reversing. Empty segments and segments that trim to nothing are dropped before reversing, and the string 0 is treated as empty and dropped too, so reverse of 0-1-2 is 2-1. Whitespace around each segment is trimmed. The optional limit keeps only the first N ids of the already-reversed list (it slices after the reverse, not before). Typical use is flipping the result of an ids or treeids list so the newest or last item comes first. Like shuffle it is never cached, but unlike shuffle its output is fully predictable.

Parameters

ids required

A dash-separated list of ids (the kind ids, treeids, relation getters and similar commands produce). Each segment is trimmed; empty segments and the bare string 0 are dropped. A single value with no dashes is returned unchanged.

limit optional default (none - return all)

Optional. After the list is reversed, keep only the first N ids and drop the rest. Counting starts from the new front of the reversed list. Omit it (or pass 0 or empty) to return the whole reversed list. A limit larger than the list length simply returns the whole list.

Examples

test{reverse:12-34-56}
Expected56-34-12
Actual56-34-12
The everyday case: a dash-separated list comes back in the opposite order, still dash-joined.
test{reverse:a-b-c-d}
Expectedd-c-b-a
Actuald-c-b-a
reverse does not check that the segments are real ids - it reverses any dash-separated tokens.
test{reverse:12-34-56-78-90:2}
Expected90-78
Actual90-78
The optional second parameter slices AFTER reversing, so you get the first N ids of the flipped list - here the last two originals, newest first.
test{reverse:one - two - three}
Expectedthree-two-one
Actualthree-two-one
Each segment is trimmed before reversing, so spaces around the dashes do not survive in the output.
test{reverse:12--34---56}
Expected56-34-12
Actual56-34-12
Runs of dashes produce empty segments, which are filtered out before the reverse - the result has no blank entries.
test{reverse:0-1-2}
Expected2-1
Actual2-1
Watch out: the engine filters out empty-looking segments, and PHP counts the string 0 as empty, so a literal 0 id is silently removed. reverse of 0-1-2 is 2-1, not 2-1-0.
test{reverse:solo}
Expectedsolo
Actualsolo
With nothing to reorder, a one-element list (or any value with no dashes) comes back as-is.
test{reverse:1-2-3-4-5:10}
Expected5-4-3-2-1
Actual5-4-3-2-1
If the limit exceeds the number of ids, the slice simply returns the whole reversed list - no padding, no error.
test{limit:{reverse:{ids:9e1d2b9f88e3d6c3bf0eb967378610d6}}:0:1}
Expecteda65d19971986e750761f0efcd9a16b1b
Actuala65d19971986e750761f0efcd9a16b1b
The common real use: wrap ids (or treeids) in reverse so the list runs the other way, then feed it to view or item. Here reverse flips the fixture slice list and limit takes the first id of the flipped list - which is the last id the unreversed list would have ended on.