PenScript Inline Operations
Penscript supports inline operations within curly brackets {}
, enabling users to manipulate and transform data dynamically within templates. This guide explains the supported inline operators and their usage through realistic examples.
Pipe Operator (|)
The pipe operator | is used to chain multiple transformations on a value, making the syntax more readable and concise.
Syntax
{ value | method }
This is equivalent to:
{ ":method": value }
Examples
{ 'hello' | uppercase } // Output: 'HELLO'
{ 'HELLO' | lowercase } // Output: 'hello'
{ 'example' | capitalize } // Output: 'Example'
{ '40' | number } // Output: 40 (converted to a number)
Supported Methods
- lowercase – Converts a string to lowercase.
- uppercase – Converts a string to uppercase.
- capitalize – Capitalizes each word in a string.
- number – Converts a string to a number.
The pipe operator cannot be used with methods that require additional parameters.
Ternary Operator (? :)
The ternary operator enables inline conditional logic.
Syntax
{ condition ? true_value : false_value }
If condition evaluates to true
, true_value
is returned; otherwise, false_value
is returned. If : false_value
is omitted, it defaults to an empty string ''
.
Examples
{ user.family_name ? user.family_name : user.given_name } // Output: user's last name if available, otherwise first name
{ user.given_name ? user.given_name } // Output: first name if available, otherwise ''
{ true ? 'Yes' : 'No' } // Output: 'Yes'
{ false ? 'Yes' : 'No' } // Output: 'No'
String and Number Concatenation (+)
The +
operator is used to concatenate strings or add numbers.
Behavior
- If both values are numbers, addition is performed.
- If one or both values are strings, concatenation occurs.
Examples
{ 'Hello' + ' World' } // Output: 'Hello World'
{ 10 + 20 } // Output: 30
{ age + 5 } // Output: 25 (if age is 20)
{ 'Age: ' + 30 } // Output: 'Age: 30'
{ 2 + ' apples' } // Output: '2 apples'
{ '12' + 34 } // Output: '1234'
{ 100 | number + '99' | number } // Output: 199 (both values converted to numbers before addition)
Subtraction (-)
The -
operator is used to subtract numbers.
Behavior
- Subtraction only works with numbers.
- If one or both operands are strings, the result is undefined.
Examples
{ 50 - 20 } // Output: 30
{ age - 5 } // Output: 15 (if age is 20)
{ '50' - 20 } // Output: undefined (since '50' is a string)
Multiplication (*) and Division (/)
Examples
{ 4 * 3 } // Output: 12
{ 10 / 2 } // Output: 5
{ 15 / 4 } // Output: 3.75
Comparisons (>, <, ≥, ≤, ==, !=)
Comparison operators allow evaluating conditions dynamically.
Examples
{ 3 < 2 } // Output: false
{ 3 > 2 } // Output: true
{ '3' > 2 } // Output: true ('3' is converted to 3)
{ 'hello' > 2 } // Output: false (string cannot be compared to number)
{ 20 <= 20 } // Output: true
{ 3 == 3 } // Output: true
{ 3 != 4 } // Output: true
{ 'apple' == 'apple' } // Output: true
{ '42' == 42 } // Output: true ('42' is converted to a number)
{ true == true } // Output: true
Operator Precedence
Operators follow this priority order:
✅ ? (ternary) > | (pipe) > + (concatenation) > - (subtraction)
Examples
{ 'john' + 'doe' | uppercase } // Output: 'johnDOE'
{ user.family_name ? name + 'test' | uppercase : 'nope' } // Output: 'familyTEST'
{ user.given_name ? name + 'test' | uppercase : 'nope' } // Output: 'nope'
{ 100 | number + '99' | number } // Output: 199
Parentheses for Order of Operations
Use parentheses to enforce specific evaluation order.
Examples
{ 10 * 4 / 2 + 5 - 2 } // Output: 23
{ 10 * ((4 / 2) + 5 - 2) } // Output: 50
Practical Test Cases
Here are test cases to illustrate real-world scenarios:
{
season: 'summer',
test_function: 'testing | uppercase',
is_true: true,
is_false: false,
given_name: 'John',
user: {
family_name: 'Doe',
},
number: 27,
number_str: '172',
}
Expression | Output |
---|---|
{season} | summer |
{ season | uppercase } | SUMMER |
{ test_function|uppercase } | TESTING UPPERCASE |
{ number_str|number} | 172 |
{ number + 28 } | 55 |
{ 'Hello' + ' World' } | Hello World |
{ user.family_name ? 'yes' + 'sure?'| uppercase : 'nope' } | yesSURE |
{ user.birth_date ? 'oops' + 'sure' | uppercase : 'nope' } | nope |
{ 100number + '99' | number } | 199 |
Summary
Penscript’s inline operators offer a powerful way to manipulate data dynamically. Key takeaways:
✅ Use |
for chaining transformations.
✅ Use ? :
for conditional expressions.
✅ Use +
for concatenation or addition.
✅ Use -, *, /
for numeric operations.
✅ Use >, <, ==, !=
for comparisons.
✅ Parentheses ()
help control precedence.
Updated 7 days ago