templon - v2.1.0
    Preparing search index...

    Function compileTemplate

    • Compiles templates with advanced features for variable resolution and processing.

      This function processes templates by replacing variables with their corresponding values from the provided variables object. It supports deep variable resolution, object processing, and various configuration options for customization.

      Key Features:

      • Deep variable resolution using dot notation (e.g., {{user.profile.name}})
      • Automatic object/array processing and JSON stringification
      • Strict mode for validation of required variables
      • Custom resolvers for complex variable resolution logic
      • Circular reference protection with configurable depth limiting
      • Customizable variable pattern matching

      Type Parameters

      • T

        The type of the template input which is also the output type

      Parameters

      • template: T

        The template to compile. Can be a string, object, or array.

      • variables: Record<string, any> = {}

        Object containing variable values for template resolution

      • options: CompileTemplateOptions = {}

        Configuration options for template compilation

      Returns T extends string ? string : T

      The compiled template with variables resolved

      // Basic string interpolation
      compileTemplate("Hello {{name}}", { name: "World" });
      // Output: "Hello World"
      // Nested object access
      compileTemplate("Hello {{user.profile.name}}", {
      user: {
      profile: {
      name: "John"
      }
      }
      });
      // Output: "Hello John"
      // With strict mode
      try {
      compileTemplate("Hello {{name}}", {}, { strict: true });
      } catch (error) {
      // Error: Missing variable: name
      }
      // Custom variable pattern
      compileTemplate("Hello <name>",
      { name: "World" },
      { variablePattern: /<([^{}]+)>/g }
      );
      // Output: "Hello World"