2.48.0 • Published 4 months ago

@memberjunction/templates-base-types v2.48.0

Weekly downloads
-
License
ISC
Repository
-
Last release
4 months ago

@memberjunction/templates-base-types

Base types and core functionality for the MemberJunction templating system. This package provides the foundational classes, types, and metadata management for templates that can be used in both client and server environments.

Overview

This package serves as the foundation for the MemberJunction templating system, providing:

  • Base template engine class for metadata management and caching
  • Extended template entity with additional functionality
  • Core type definitions for template rendering results
  • Template metadata access through a singleton pattern

Installation

npm install @memberjunction/templates-base-types

Core Components

TemplateEngineBase

The TemplateEngineBase class provides a singleton engine for managing template metadata, including templates, content variations, parameters, and categories.

import { TemplateEngineBase } from '@memberjunction/templates-base-types';

// Get the singleton instance
const templateEngine = TemplateEngineBase.Instance;

// Load template metadata (required before use)
await templateEngine.Config();

// Access templates
const templates = templateEngine.Templates;

// Find a specific template by name (case-insensitive)
const template = templateEngine.FindTemplate('Welcome Email');

TemplateEntityExtended

An enhanced version of the base TemplateEntity that includes associated content and parameters as properties, along with utility methods.

import { TemplateEntityExtended } from '@memberjunction/templates-base-types';

// Access template content and parameters
const template: TemplateEntityExtended = templateEngine.FindTemplate('Invoice Template');

// Get all content for the template
const allContent = template.Content;

// Get content by type
const htmlContent = template.GetContentByType('HTML');
const plainTextContent = template.GetContentByType('PlainText');

// Get the highest priority content
const primaryContent = template.GetHighestPriorityContent();
const primaryHtmlContent = template.GetHighestPriorityContent('HTML');

// Validate input data against template parameters
const validationResult = template.ValidateTemplateInput({
    customerName: 'John Doe',
    invoiceNumber: '12345'
});

if (!validationResult.Success) {
    console.error('Validation errors:', validationResult.Errors);
}

TemplateRenderResult

A simple class representing the result of a template rendering operation.

import { TemplateRenderResult } from '@memberjunction/templates-base-types';

// Typically returned by rendering operations
const result: TemplateRenderResult = {
    Success: true,
    Output: '<html>Rendered content...</html>',
    Message: undefined // Optional, typically used for error messages
};

API Reference

TemplateEngineBase

Properties

  • Templates: TemplateEntityExtended[] - All loaded templates
  • TemplateContentTypes: TemplateContentTypeEntity[] - Available content types
  • TemplateCategories: TemplateCategoryEntity[] - Template categories
  • TemplateContents: TemplateContentEntity[] - All template content records
  • TemplateParams: TemplateParamEntity[] - All template parameters

Methods

  • static get Instance(): TemplateEngineBase - Get the singleton instance
  • async Config(forceRefresh?: boolean, contextUser?: UserInfo, provider?: IMetadataProvider) - Load template metadata
  • FindTemplate(templateName: string): TemplateEntityExtended - Find a template by name (case-insensitive)

TemplateEntityExtended

Properties

  • Content: TemplateContentEntity[] - Associated content records
  • Params: TemplateParamEntity[] - Associated parameter definitions

Methods

  • GetContentByType(type: string): TemplateContentEntity[] - Get all content of a specific type
  • GetHighestPriorityContent(type?: string): TemplateContentEntity - Get the highest priority content
  • ValidateTemplateInput(data: any): ValidationResult - Validate input data against parameter requirements

TemplateRenderResult

Properties

  • Success: boolean - Whether the rendering was successful
  • Output: string - The rendered output
  • Message?: string - Optional message (typically for errors)

Usage Examples

Basic Template Metadata Access

import { TemplateEngineBase } from '@memberjunction/templates-base-types';

async function loadTemplates() {
    const engine = TemplateEngineBase.Instance;
    
    // Load metadata (only needed once)
    await engine.Config();
    
    // List all active templates
    const activeTemplates = engine.Templates.filter(t => t.IsActive);
    
    // Find templates by category
    const emailTemplates = engine.Templates.filter(t => 
        t.CategoryID === engine.TemplateCategories.find(c => c.Name === 'Email')?.ID
    );
    
    console.log(`Found ${activeTemplates.length} active templates`);
    console.log(`Found ${emailTemplates.length} email templates`);
}

Working with Template Content

import { TemplateEngineBase, TemplateEntityExtended } from '@memberjunction/templates-base-types';

async function getTemplateContent(templateName: string, contentType: string) {
    const engine = TemplateEngineBase.Instance;
    await engine.Config();
    
    const template = engine.FindTemplate(templateName);
    if (!template) {
        throw new Error(`Template "${templateName}" not found`);
    }
    
    // Get specific content type
    const content = template.GetHighestPriorityContent(contentType);
    if (!content) {
        throw new Error(`No ${contentType} content found for template "${templateName}"`);
    }
    
    return content.TemplateText;
}

Template Parameter Validation

import { TemplateEngineBase } from '@memberjunction/templates-base-types';

async function validateTemplateData(templateName: string, inputData: any) {
    const engine = TemplateEngineBase.Instance;
    await engine.Config();
    
    const template = engine.FindTemplate(templateName);
    if (!template) {
        throw new Error(`Template "${templateName}" not found`);
    }
    
    const validation = template.ValidateTemplateInput(inputData);
    
    if (!validation.Success) {
        // Handle validation errors
        validation.Errors.forEach(error => {
            console.error(`Parameter ${error.Source}: ${error.Message}`);
        });
        throw new Error('Template validation failed');
    }
    
    // Data is valid, proceed with rendering
    return true;
}

Dependencies

This package depends on the following MemberJunction packages:

  • @memberjunction/core - Core MemberJunction functionality and base classes
  • @memberjunction/core-entities - Entity definitions for templates, content, and parameters
  • @memberjunction/global - Global utilities and decorators

Integration with Other MJ Packages

This package serves as the foundation for:

  • @memberjunction/templates-engine - The full template rendering engine that builds on these base types
  • Custom template extensions - Third-party extensions can use these types for compatibility

The base types ensure consistent template handling across the entire MemberJunction ecosystem, whether templates are being managed, validated, or rendered.

Build and Development

# Build the package
npm run build

# Watch for changes during development
npm run start

The package is built using TypeScript and outputs to the dist directory. Type definitions are automatically generated during the build process.

Notes

  • This package intentionally has minimal dependencies to ensure it can be used in various environments
  • The TemplateEngineBase uses the singleton pattern to ensure consistent metadata access across an application
  • Template metadata is loaded from the MemberJunction database using the dataset system
  • The @RegisterClass decorator on TemplateEntityExtended ensures proper integration with the MJ entity system

License

ISC

2.27.1

8 months ago

2.23.2

8 months ago

2.46.0

5 months ago

2.23.1

8 months ago

2.27.0

8 months ago

2.34.0

6 months ago

2.30.0

7 months ago

2.19.4

9 months ago

2.19.5

9 months ago

2.19.2

9 months ago

2.19.3

9 months ago

2.19.0

9 months ago

2.19.1

9 months ago

2.15.2

9 months ago

2.34.2

6 months ago

2.15.0

9 months ago

2.34.1

6 months ago

2.15.1

9 months ago

2.38.0

5 months ago

2.45.0

5 months ago

2.22.1

9 months ago

2.22.0

9 months ago

2.41.0

5 months ago

2.22.2

9 months ago

2.26.1

8 months ago

2.26.0

8 months ago

2.33.0

6 months ago

2.18.3

9 months ago

2.18.1

9 months ago

2.18.2

9 months ago

2.18.0

9 months ago

2.37.1

6 months ago

2.37.0

6 months ago

2.14.0

10 months ago

2.21.0

9 months ago

2.44.0

5 months ago

2.40.0

5 months ago

2.29.0

8 months ago

2.29.2

8 months ago

2.29.1

8 months ago

2.25.0

8 months ago

2.48.0

4 months ago

2.32.0

7 months ago

2.32.2

7 months ago

2.32.1

7 months ago

2.17.0

9 months ago

2.13.4

10 months ago

2.36.0

6 months ago

2.13.2

11 months ago

2.13.3

10 months ago

2.13.0

11 months ago

2.36.1

6 months ago

2.13.1

11 months ago

2.43.0

5 months ago

2.20.2

9 months ago

2.20.3

9 months ago

2.20.0

9 months ago

2.20.1

9 months ago

2.28.0

8 months ago

2.47.0

5 months ago

2.24.1

8 months ago

2.24.0

8 months ago

2.31.0

7 months ago

2.12.0

12 months ago

2.39.0

5 months ago

2.16.1

9 months ago

2.35.1

6 months ago

2.35.0

6 months ago

2.16.0

9 months ago

2.42.1

5 months ago

2.42.0

5 months ago

2.23.0

9 months ago

2.11.0

12 months ago

2.10.0

12 months ago

2.9.0

1 year ago

2.8.0

1 year ago

2.7.0

1 year ago

2.6.1

1 year ago

2.5.2

1 year ago

2.6.0

1 year ago

2.7.1

1 year ago

2.5.1

1 year ago

2.5.0

1 year ago

2.4.1

1 year ago

2.4.0

1 year ago

2.3.3

1 year ago

2.3.2

1 year ago

2.3.1

1 year ago

2.3.0

1 year ago

2.2.1

1 year ago

2.2.0

1 year ago

2.1.5

1 year ago

2.1.4

1 year ago

2.1.3

1 year ago

2.1.2

1 year ago

2.1.1

1 year ago

2.1.0

1 year ago

2.0.0

1 year ago

1.8.1

1 year ago

1.8.0

1 year ago

1.7.1

1 year ago

1.7.0

1 year ago

1.6.1

1 year ago

1.6.0

1 year ago

1.5.3

1 year ago