A block of helper methods
Put one {% csharp %} … {% endcsharp %} block at the very top of the template and call its methods with CallCSharp. The action needs "enableScripts": true, and the app needs LiquidCompose__AllowScripts=true.
{% csharp %}
public string NormalizeCode(string value)
{
return (value ?? "").Trim().ToUpperInvariant();
}
public string Money(double amount)
{
return amount.ToString("0.00", CultureInfo.InvariantCulture);
}
{% endcsharp %}
{
"orderNumber": {{ content.order.number | CallCSharp: 'NormalizeCode' | ToJson }},
"total": {{ content.order.total | CallCSharp: 'Money' | ToJson }},
"reference": {{ content.order.reference | Default: '' | CallCSharp: 'NormalizeCode' | ToJson }}
}What helpers may be
- Public instance methods with one
string,doubleorboolparameter, returning one of those types. Private methods, fields andconstare allowed. - Namespaces in scope:
System,System.Collections.Generic,System.Globalization,System.Linq,System.TextandSystem.Text.RegularExpressions. C# 12. - No I/O: helpers compile against a fixed reference set, without
System.IO,HttpClient,ProcessorConsole. UseCallFunctionfor lookups. - One block, in the root template. Blocks in partials, a second block, overloads, generics or two parameters fail with
TemplateSyntaxErrororUnsupportedScriptFeature.
Values in and out
- JSON numbers become
double; strings are never parsed into numbers or Booleans. - A null or missing value fails with
ScriptExecutionFailed. PutDefault: ''beforeCallCSharpfor optional fields. Defaultturns values into strings, so don't use it before a helper that takes adoubleor abool.- A helper that throws fails the action with the helper's name and the exception type, never its message, which may contain data.
Compiled once, stopped at the deadline
The first run of a template compiles its block with Roslyn, which can take a few seconds on a fresh worker; later runs use the cached assembly. timeoutSeconds includes that compile, so leave room for it. A helper that never returns is stopped at the deadline with TransformationTimeout; its worker is replaced and the next run works normally.