zplCore.comzplCore.com

ExpressionEngine

public sealed class ExpressionEngine
Small expression language for label items, e.g. Upper([Name]) + " / " + Format(Now(), "dd.MM.yyyy") or If([Qty] > 10, "BULK", "SINGLE").
  • Fields: [Field Name] (data record), or item names ([TextItem1] = text of another item)
  • Literals: numbers, "strings", true/false
  • Operators: + - * / % (+ concatenates if either side is text), = <> < <= > >=, and/or/not, unary -
  • Variables: LABEL_INDEX, LABEL_NUMBER; in data bindings also LABEL_WIDTH, LABEL_HEIGHT (label units), RECORD_INDEX, PAGE_NUMBER and TOTAL_PAGES (when the record count is known); plus custom ones via ExpressionEngine.Variables
  • Conversions use ExpressionEngine.Culture (invariant by default); Format/ToString/ToNumber accept a culture name argument.
  • Functions: see ExpressionEngine.Functions; add your own.

Reference with every function, operator precedence, type conversion, empty values, cultures and number/date formats, job variables, errors and recipes: zplcore.com/docs/expressions; try expressions with your own data in the expression builder.
Thread safety: ExpressionEngine.Evaluate may be called concurrently once configured (the member cache is static and concurrent); configuration (ExpressionEngine.Variables, ExpressionEngine.Functions, AddTypeReference) is not thread-safe.

Constructors

SignatureDescription
# ExpressionEngine()

Properties

PropertyTypeDescription
Clock { get; set; }Func<DateTime>Clock for Now()/Today() (tests, reprints with the original date).
Culture { get; set; }CultureInfoCulture used for number/date text conversion, Upper/Lower and Format. Defaults to CultureInfo.InvariantCulture so labels don't change with the server's regional settings. A culture name argument of Format/ToString (3rd), ToNumber/IsNumeric (2nd) and ToDate (3rd), e.g. Format([price], "C", [culture]), overrides it for that call; a missing, empty or non-text argument means this culture. Not part of label JSON: templates loaded from JSON pass cultures as argument. Dates use the culture's standard calendar from a fixed table (ExpressionEngine.StandardCalendar), the same on every operating system; a culture whose ICU data defaults to another calendar is replaced by a read-only copy.
Functions { get; }Dictionary<string, Func<object[], object>>Functions callable as Name(args) (case-insensitive); add or replace entries, or use ExpressionEngine.SetCustomFunction. A function gets its arguments evaluated (missing ones are not passed; read them as null) and returns a number (Double; other numeric types are converted), text, Boolean, DateTime or null. Built in:
  • Text: Upper, Lower, Trim(text, chars?), TrimStart, TrimEnd, Len, Left(text, n), Right(text, n), Mid(text, start1, n?) (1-based), Substring(text, start0, n?) (0-based), Replace(text, find, with), PadLeft(text, width, char?), PadRight, Contains, StartsWith, EndsWith (ignoring case), IndexOf, Concat(…), Repeat(text, n)
  • Conditions: If(condition, then, else), IsEmpty, Coalesce(…) (first non-empty value)
  • Math: Round(value, decimals?) (half away from zero), Floor, Ceiling, Abs, Min(…), Max(…)
  • Date: Now(), Today() (from ExpressionEngine.Clock), AddDays, AddMonths, AddYears, ToDate(text, format?, culture?)
  • Conversion: Format(value, format?, culture?) (alias ToString), Str, Num, ToNumber(text, culture?), IsNumeric(value, culture?)
  • Check digits: Gs1CheckDigit(digits) (returns the digits with the check digit appended), Mod10, Luhn, Mod11, Mod43 (return the check character, see CheckDigits), VoicePickCode(gtin, lot, date?)
  • GS1: GS1(ai1, value1, ai2, value2, …) returns the element string "(01)…(10)…"; every AI and value is checked against the GS1 table (an error throws a FormatException naming the AI). With Symbology.Gs1_128 the FNC1 separators after variable-length values are added when the barcode is written.
Details: Left/Right/Mid/Substring clamp their counts (never an error); Contains/StartsWith/EndsWith/IndexOf ignore case, Replace does not; Repeat stops at 10,000 copies; If evaluates both branches; Round takes 0–15 decimals; ToDate returns 0001-01-01 for text that is no date and Num/ToNumber return 0 for text that is no number (thousands separators are not read, except by ToNumber/IsNumeric with a culture argument); Format formats text that is a number when a format is given (D/X take whole numbers) and throws for formats a value rejects and for culture names .NET does not know. Comparisons are numeric when one side is a number or both are number texts. Every function with examples: function reference.
static StandardCalendar { get; }IReadOnlyDictionary<string, Type>Standard calendars of the cultures that do not date in the Gregorian calendar (the defaults of Windows): ar-SA Umm al-Qura, th Thai Buddhist, Persian for fa, ps, ckb-IR, lrc, mzn and uz-Arab. Every other culture formats and reads dates in the Gregorian calendar – also where the ICU data of an operating system defaults to another one. The same table applies to every culture-dependent formatting of zplCore (bound fields with LabelItem.DataFieldFormat, table cells, clock fields).
TypeReferences { get; }IReadOnlyDictionary<string, Type>Registered types (name → type) whose public static and instance members may be used, e.g. Math.Max(1, [Qty]).
Variables { get; }Dictionary<string, object>Custom variables usable by name in expressions (case-insensitive), e.g. [Price] * VAT.

Fields & Constants

FieldTypeDescription
const MaxNesting = 256intDeepest nesting of parentheses, calls and unary operators the parser accepts (it recurses per level).

Methods

Method SignatureReturnsDescription
# AddTypeReference<TRef>(string? alias = default)ExpressionEngineAllows expressions to call public members of TRef (see ExpressionEngine.AddTypeReference).
# AddTypeReference(Type type, string? alias = default)ExpressionEngineAllows expressions to call public members of type: static members via the type name (Math.Max(1, [Qty]), DateTime.Now.AddDays(3)) and instance members on values of registered types. Only registered types are accessible.
static # Bool(object v)boolTruth value of the engine (conditions, and/or/not): null → false; text → true unless "", "0" or "false" (any case); numbers → true unless 0.
# Evaluate(string expression, Func<string, object>? fieldResolver = default)objectEvaluates an expression to its value. expression: The expression, e.g. If([qty] > 10, "BULK", "SINGLE"). · fieldResolver: Value of a field: gets the name inside […] (and bare names that are neither functions nor variables); null = every field is empty. Int32, Int64, Single and Decimal values of […] fields are read as Double. Returns A Double, text, Boolean, DateTime, null, or what a custom function or type member returned. · Throws FormatException: Syntax error, unknown function, malformed culture name, a format the value rejects, or an invalid GS1(…) value.
# EvaluateToString(string expression, Func<string, object>? fieldResolver = default)stringEvaluates to a display string: the value of ExpressionEngine.Evaluate converted with ExpressionEngine.ToText – what a label prints. expression: The expression, e.g. Format([price], "C", "de-DE"). · fieldResolver: Value of a field: gets the name inside […] (and bare names that are neither functions nor variables); null = every field is empty. Returns The result as text; empty for a null result. · Throws FormatException: Syntax error, unknown function, malformed culture name, a format the value rejects, or an invalid GS1(…) value.
static # Num(object v)doubleCulture-invariant number conversion (use ExpressionEngine.ToNum for the engine's ExpressionEngine.Culture).
static # Num(object v, IFormatProvider culture)doubleNumber conversion of the engine: null → 0, booleans → 1/0, text read with culture and then invariant (NumberStyles.Float: no thousands separators), else 0.
# SetCustomFunction(string name, Func<object[], object> function)ExpressionEngineAdds or replaces a function callable as Name(args).
# SetCustomVariable(string name, object value)ExpressionEngineAdds or replaces a variable usable by name in expressions.
static # Str(object v)stringCulture-invariant text conversion (use ExpressionEngine.ToText for the engine's ExpressionEngine.Culture).
static # Str(object v, IFormatProvider culture)stringText conversion of the engine: null → "", booleans → "True"/"False", formattable values with culture (its standard calendar, special spaces as plain spaces – the same on every operating system).
# ToNum(object v)doubleConverts a value to a number with ExpressionEngine.Culture (falls back to invariant parsing).
# ToText(object v)stringConverts a value to text with ExpressionEngine.Culture.