Skip to main content

Template Interpolation

Pylee’s template interpolation system provides powerful, flexible string templating capabilities for dynamic configuration generation. This system supports variable substitution, secret handling, and context-aware processing across different environments and use cases.

Overview

Template interpolation in Pylee allows you to:
  • Dynamic value substitution: Replace placeholders with actual values at runtime
  • Variable and secret integration: Seamlessly incorporate variables and secrets
  • Context-aware processing: Different interpolation behavior based on usage context
  • Multi-level precedence: Resolve values from organization, registry, and server levels
  • Secure secret handling: Maintain security while enabling flexible configuration

Basic Syntax

Template Format

Template strings use curly braces {} to denote placeholders:

Simple Variable Substitution

Multiple Variables

Variable and Secret Prefixes

Variables

  • Prefixed: {var.VARIABLE_NAME} - Explicitly references a variable
  • Unprefixed: {VARIABLE_NAME} - References a variable or secret (with secrets taking precedence)

Secrets

  • Prefixed: {secret.SECRET_NAME} - Explicitly references a secret
  • Unprefixed: {SECRET_NAME} - References a variable or secret (with secrets taking precedence)

Prefix Examples

Precedence Resolution

Precedence Order

Variables and secrets are resolved in the following order of precedence:
  1. Server level: Most specific configuration
  2. Registry level: Shared configuration across servers
  3. Organization level: Global organizational defaults

Precedence Example

Template {API_URL} resolves to "https://registry.api.com" (registry overrides organization).

Variable vs Secret Precedence

When both a variable and secret have the same name at the same level, secrets take precedence for unprefixed references:

Advanced Features

Default Values

Provide fallback values when variables might be undefined:

Nested Interpolation

Variables can reference other variables:

Complex Template Patterns

Context-Aware Processing

Display Context

Templates processed for UI display with secrets masked:

Copy Context

Templates processed for copying with full values:

Execution Context

Templates processed for runtime execution:

Usage Examples

Basic Configuration

Multi-Environment Configuration

Database Configuration

Service Configuration

Integration with Pylee Components

ConnectButton Integration

The ConnectButton component uses template interpolation for dynamic configuration:

Environment Variable Generation

Command Generation

Error Handling

Missing Variables

If a template variable is not found, the placeholder is left unchanged:

Malformed Templates

Malformed templates are left unchanged:

Non-String Values

Non-string values are converted to strings:

Performance Considerations

Template Compilation

  • Templates are compiled once and cached
  • Use template caching for frequently used patterns
  • Avoid complex nested interpolation chains

Memory Management

  • Clean up unused template contexts
  • Use weak references for large template sets
  • Monitor memory usage with many templates

Optimization Strategies

Security Considerations

Secret Exposure Prevention

  • Never log interpolated templates containing secrets
  • Use context-appropriate secret handling
  • Implement proper access controls

Template Injection Prevention

  • Validate template syntax before processing
  • Sanitize user-provided template strings
  • Limit template complexity to prevent DoS

Secure Practices

Testing Templates

Unit Testing

Integration Testing

Migration Guide

From Legacy Template System

When migrating from older template systems:

Batch Migration Tool

Troubleshooting

Common Issues

Template Not Resolving

Solutions:
  • Check variable exists in context
  • Verify correct prefix usage
  • Ensure proper precedence setup

Incorrect Precedence

Solutions:
  • Review precedence configuration
  • Check variable/secret levels
  • Verify context processing

Secret Exposure

Solutions:
  • Check context configuration
  • Verify secret handling rules
  • Review component implementation

Debugging Tools

Best Practices

Template Design

  • Use descriptive variable names
  • Keep templates readable and maintainable
  • Avoid overly complex nested interpolation
  • Document template usage and dependencies

Variable Management

  • Use consistent naming conventions
  • Group related variables logically
  • Implement proper validation
  • Document variable purposes

Security

  • Never expose secrets in display contexts
  • Use appropriate prefixes for clarity
  • Implement proper access controls
  • Regular security audits

Performance

  • Cache compiled templates
  • Minimize template complexity
  • Use efficient variable lookups
  • Monitor performance metrics

Next Steps