moclojer
GitHub
  • README
  • First Steps
    • Overview
    • Installation
    • Your First Mock Server
    • Dynamic Responses
    • Multiple Endpoints
    • Real-World Example
  • Templates
    • Template System Overview
    • Template Variables
  • Advanced Features
    • WebSocket Support
    • External Bodies
    • Webhook Integration
    • Rate Limiting
    • Multi-Domain Support
  • Framework Integration
    • Using as a Library
  • Reference Documentation
    • Configuration Specification
    • FAQ
  • Community and Contribution
    • Documentation Refactor
    • Code of Conduct
  • Release Notes
    • Latest Changes
    • v0.4.0
    • v0.3.5
    • v0.3.4
    • v0.3.3.1
    • v0.3.3
    • v0.3.2
    • v0.3.1
    • v0.3.0
    • v0.2
    • v0.1
Powered by GitBook
On this page
  • What are templates?
  • Why use templates?
  • 🎯 Realistic Testing
  • 🔄 Dynamic Responses
  • 🧪 Flexible Testing
  • Template Variables
  • Path Parameters
  • Query Parameters
  • JSON Body Parameters
  • Request Headers
  • Built-in Functions
  • Common Patterns
  • Echo Responses
  • Personalized Responses
  • Search Results
  • API Keys and Authentication
  • Template Best Practices
  • 1. Use Meaningful Names
  • 2. Handle Missing Values
  • 3. Keep Templates Readable
  • 4. Use Consistent Formatting
  • Advanced Template Features
  • Nested Object Access
  • Conditional Content
  • Array Handling
  • Error Handling
  • What's Next?
  • Examples in Action
  • Need Help?

Was this helpful?

Edit on GitHub
  1. Templates

Template System Overview

Learn about moclojer's powerful template system that allows you to create dynamic responses based on request data, making your mocks more realistic and flexible.

moclojer's template system is what makes your mock APIs dynamic and realistic. Instead of returning the same static response every time, templates let you create responses that change based on the incoming request data.

What are templates?

Templates are special placeholders in your response configuration that get replaced with actual values when a request is processed. They use a simple {{variable}} syntax that's easy to read and write.

Example:

- endpoint:
    method: GET
    path: /users/:id
    response:
      body: >
        {
          "id": "{{path-params.id}}",
          "name": "User {{path-params.id}}",
          "timestamp": "{{now}}"
        }

When someone requests /users/123, the response becomes:

{
  "id": "123",
  "name": "User 123",
  "timestamp": "2024-01-15T10:30:00Z"
}

Why use templates?

🎯 Realistic Testing

Simulate how real APIs behave by returning different data based on the request:

- endpoint:
    method: GET
    path: /products/:category
    response:
      body: >
        {
          "category": "{{path-params.category}}",
          "products": [
            {"id": 1, "name": "{{path-params.category}} Product 1"},
            {"id": 2, "name": "{{path-params.category}} Product 2"}
          ]
        }

🔄 Dynamic Responses

Create responses that reflect the input data:

- endpoint:
    method: POST
    path: /orders
    response:
      body: >
        {
          "order_id": "ORD-{{json-params.user_id}}-{{now}}",
          "user_id": {{json-params.user_id}},
          "total": {{json-params.total}},
          "status": "confirmed"
        }

🧪 Flexible Testing

Test different scenarios without creating multiple endpoint configurations:

- endpoint:
    method: GET
    path: /api/status
    response:
      body: >
        {
          "environment": "{{query-params.env}}",
          "debug": {{query-params.debug}},
          "timestamp": "{{now}}"
        }

Template Variables

moclojer provides several types of template variables:

Path Parameters

Extract values from URL paths:

path: /users/:id/posts/:postId
body: >
  {
    "user_id": "{{path-params.id}}",
    "post_id": "{{path-params.postId}}"
  }

Query Parameters

Use URL query string values:

# For request: /search?q=javascript&limit=10
body: >
  {
    "query": "{{query-params.q}}",
    "limit": {{query-params.limit}},
    "results": []
  }

JSON Body Parameters

Access data from JSON request bodies:

# For POST with body: {"name": "John", "email": "john@example.com"}
body: >
  {
    "id": 123,
    "name": "{{json-params.name}}",
    "email": "{{json-params.email}}",
    "created_at": "{{now}}"
  }

Request Headers

Use values from HTTP headers:

body: >
  {
    "user_agent": "{{header-params.User-Agent}}",
    "authorization": "{{header-params.Authorization}}",
    "content_type": "{{header-params.Content-Type}}"
  }

Built-in Functions

Special functions for common needs:

body: >
  {
    "timestamp": "{{now}}",
    "random_id": "{{uuid}}",
    "server_time": "{{now}}"
  }

Common Patterns

Echo Responses

Return the same data that was sent:

- endpoint:
    method: POST
    path: /echo
    response:
      body: >
        {
          "received": {
            "name": "{{json-params.name}}",
            "email": "{{json-params.email}}"
          },
          "processed_at": "{{now}}"
        }

Personalized Responses

Create user-specific responses:

- endpoint:
    method: GET
    path: /profile/:username
    response:
      body: >
        {
          "username": "{{path-params.username}}",
          "welcome_message": "Hello, {{path-params.username}}!",
          "last_login": "{{now}}"
        }

Search Results

Dynamic search responses:

- endpoint:
    method: GET
    path: /search
    response:
      body: >
        {
          "query": "{{query-params.q}}",
          "page": {{query-params.page}},
          "results": [
            {"title": "Result 1 for {{query-params.q}}"},
            {"title": "Result 2 for {{query-params.q}}"}
          ]
        }

API Keys and Authentication

Handle authentication scenarios:

- endpoint:
    method: GET
    path: /protected
    response:
      body: >
        {
          "user": "authenticated",
          "api_key": "{{query-params.api_key}}",
          "access_level": "full"
        }

Template Best Practices

1. Use Meaningful Names

# Good
path: /users/:userId/orders/:orderId

# Less clear
path: /users/:id1/orders/:id2

2. Handle Missing Values

Provide defaults for optional parameters:

body: >
  {
    "page": {{query-params.page}},
    "limit": {{query-params.limit}},
    "default_message": "Page {{query-params.page}} with {{query-params.limit}} items"
  }

3. Keep Templates Readable

Break complex templates into logical sections:

body: >
  {
    "user": {
      "id": "{{path-params.id}}",
      "name": "{{json-params.name}}"
    },
    "metadata": {
      "created_at": "{{now}}",
      "source": "{{header-params.User-Agent}}"
    }
  }

4. Use Consistent Formatting

Follow JSON formatting rules:

# Strings need quotes
"name": "{{json-params.name}}"

# Numbers don't need quotes
"count": {{query-params.count}}

# Booleans don't need quotes
"active": {{json-params.active}}

Advanced Template Features

Nested Object Access

Access nested properties from JSON:

# For body: {"user": {"profile": {"name": "John"}}}
body: >
  {
    "welcome": "Hello, {{json-params.user.profile.name}}!"
  }

Conditional Content

Simple conditional logic:

body: >
  {
    "message": "{{#if query-params.admin}}Admin access granted{{else}}Regular user{{/if}}"
  }

Array Handling

Work with array data:

# For body: {"tags": ["javascript", "tutorial"]}
body: >
  {
    "first_tag": "{{json-params.tags.0}}",
    "tag_count": {{json-params.tags.length}}
  }

Error Handling

Templates gracefully handle missing data:

- endpoint:
    method: GET
    path: /user-info
    response:
      body: >
        {
          "user_id": "{{query-params.id}}",
          "note": "If id parameter is missing, it shows as empty string"
        }

What's Next?

Now that you understand the template system basics, dive deeper into specific areas:

Examples in Action

See templates in real-world scenarios:

Need Help?

PreviousReal-World ExampleNextTemplate Variables

Last updated 8 days ago

Was this helpful?

- Complete reference of all available variables

- Advanced techniques for dynamic responses

- Complex templating patterns and best practices

- Complete API with templates

- Product catalog with dynamic data

- User CRUD operations

- All available template variables

- Common template questions

- Community help

Template Variables
Dynamic Content
Advanced Templating
REST API Example
E-commerce API
User Management
Template Variables Reference
FAQ
GitHub Discussions