Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Pipes  ›  Lesson

Chaining Pipes and Precedence

Pipes Precedence 12 minPipes can be chained left to right, and because the pipe operator binds more loosely than arithmetic and logic but more tightly than the ternary operator, parentheses make the intended grouping explicit.

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.

Fixing the price labelStep 1 of 4
  1. Find the pipe

    | uppercase sits after a ternary.

Drills

Answer fast — trust the rule.

quick drill

In {{ a() + b() | number }}, what is formatted?

quick drill

In which order do chained pipes run in {{ x | slice:0:5 | uppercase }}?

quick drill

When sale() is true, what does {{ sale() ? 'yes' : 'no' | uppercase }} show?

quick drill

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.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainWhat gets piped?

Explain what each of these Greenhouse expressions displays and why: {{ price() * count() | currency:'INR' }} and {{ sale() ? 'Discount' : 'Full price' | uppercase }}. Then rewrite the second so the whole result is upper case.