Chaining Pipes and Precedence
The shop page in Greenhouse shows a total price and a label. Both expressions look right, and one of them is quietly wrong: the label shows "Discount" in normal case and "FULL PRICE" in capitals. The bug is not in the pipe. It is in which part of the expression the pipe grabbed.
The rule
Pipes run left to right, grab less than maths, more than
?:.
Chaining. You can pass one pipe's output into another. They run left to right:
{{ plant().name | slice:0:12 | titlecase }}First the name is cut to 12 characters, then its words are capitalised. Order matters. | date | uppercase works: date turns the Date into text, and uppercase accepts text. | uppercase | date fails, because uppercase only accepts strings and a Date is not one. Each pipe must accept what the previous one produced.
Arguments follow colons, one per colon: slice:0:12, date:'d MMM':'UTC'.
Precedence. The pipe operator binds more loosely than arithmetic, comparison, &&, || and ??. So the whole calculation on its left is done first:
{{ price() * count() | currency:'INR' }} <!-- (price × count), then formatted -->But it binds more tightly than the ternary ? :. So in a ternary, the pipe takes only the last branch:
{{ sale() ? 'Discount' : 'Full price' | uppercase }}
<!-- same as: sale() ? 'Discount' : ('Full price' | uppercase) -->The fix is parentheses:
{{ (sale() ? 'Discount' : 'Full price') | uppercase }}The shortcut
When in doubt, bracket it out. If anything more complex than a single value sits to the left of |, wrap it in parentheses. You will never have to recall the precedence rules, and the next reader will not have to either.
Find the pipe
| uppercasesits after a ternary.
Drills
Answer fast — trust the rule.
In {{ a() + b() | number }}, what is formatted?
In which order do chained pipes run in {{ x | slice:0:5 | uppercase }}?
When sale() is true, what does {{ sale() ? 'yes' : 'no' | uppercase }} show?
How do you make {{ sale() ? 'yes' : 'no' | uppercase }} upper-case the result whichever branch wins?
Remember
- Chained pipes run left to right; each must accept the previous output.
- Pipes grab less than maths and logic, more than
? :. - Shortcut: when in doubt, bracket it out.
