templon - v2.1.0
    Preparing search index...

    Interface CompileTemplateOptions

    Configuration options for template compilation. These options control how templates are processed and how variables are resolved.

    interface CompileTemplateOptions {
        autoStringifyObjects?: boolean;
        maxVariableDepth?: number;
        parseBinInts?: boolean;
        parseStrings?: boolean;
        preserveUndefined?: boolean;
        resolver?: (path: string) => any;
        strict?: boolean;
        stringTransform?: (value: string) => string;
        variablePattern?: RegExp;
    }
    Index

    Properties

    autoStringifyObjects?: boolean

    When enabled, objects and arrays are automatically converted to JSON strings. This is useful when you want to display complex data structures in the template.

    // With autoStringifyObjects: true
    compileTemplate("Data: {{data}}", { data: { foo: "bar" } })
    // Output: "Data: {"foo":"bar"}"
    true
    
    maxVariableDepth?: number

    Maximum depth for nested variable resolution. This prevents circular references and infinite loops in complex data structures.

    // With maxVariableDepth: 1
    compileTemplate("{{a.b.c}}", { a: { b: { c: "value" } } }, { maxVariableDepth: 1 })
    // Output: "" (c is at depth 2)
    10
    
    parseBinInts?: boolean

    When enabled, attempts to parse string values as BigInt. This is useful when working with large numbers that exceed JavaScript's Number.MAX_SAFE_INTEGER.

    // With parseBinInts: true
    compileTemplate("{{bigNum}}", { bigNum: "9007199254740993" })
    // Output: 9007199254740993n
    false
    
    parseStrings?: boolean

    When enabled, attempts to parse string values as JSON. This is useful when you want to convert string representations of objects back to objects.

    // With parseStrings: true
    compileTemplate("{{json}}", { json: '{"foo":"bar"}' })
    // Output: { foo: "bar" }
    true
    
    preserveUndefined?: boolean

    Controls how undefined variables are handled in the output. When false, undefined variables are replaced with an empty string. When true, the original variable placeholder is preserved.

    // With preserveUndefined: false
    compileTemplate("Hello {{name}}", {})
    // Output: "Hello "

    // With preserveUndefined: true
    compileTemplate("Hello {{name}}", {}, { preserveUndefined: true })
    // Output: "Hello {{name}}"
    false
    
    resolver?: (path: string) => any

    Custom function to resolve variable values. This allows for custom logic when resolving template variables.

    Type declaration

      • (path: string): any
      • Parameters

        Returns any

        The resolved value or undefined if the variable should be handled by the default resolver

    compileTemplate("Hello {{user.name}}", {}, {
    resolver: (path) => {
    if (path === "user.name") return "Custom User";
    return undefined;
    }
    });
    strict?: boolean

    When enabled, throws an error if a variable referenced in the template is not found. This is useful for catching missing variables early in development.

    // Will throw error if 'name' is not provided
    compileTemplate("Hello {{name}}", {}, { strict: true });
    false
    
    stringTransform?: (value: string) => string

    Transform function applied to the final string output. This allows for custom string processing after all variables are resolved.

    Type declaration

      • (value: string): string
      • Parameters

        • value: string

          The processed string value

        Returns string

        The transformed string

    compileTemplate("Hello {{name}}", { name: "World" }, {
    stringTransform: (s) => s.toUpperCase()
    });
    // Output: "HELLO WORLD"
    variablePattern?: RegExp

    Custom regular expression pattern for matching variables in the template. The pattern should have a capture group for the variable name.

    // Using custom pattern <variable>
    compileTemplate("Hello <name>", { name: "World" }, {
    variablePattern: /<([^{}]+)>/g
    });
    /{{([^{}]+)}}/g