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:
🔄 Dynamic Responses
Create responses that reflect the input data:
🧪 Flexible Testing
Test different scenarios without creating multiple endpoint configurations:
Template Variables
moclojer provides several types of template variables:
Path Parameters
Extract values from URL paths:
Query Parameters
Use URL query string values:
JSON Body Parameters
Access data from JSON request bodies:
Request Headers
Use values from HTTP headers:
Built-in Functions
Special functions for common needs:
Common Patterns
Echo Responses
Return the same data that was sent:
Personalized Responses
Create user-specific responses:
Search Results
Dynamic search responses:
API Keys and Authentication
Handle authentication scenarios:
Template Best Practices
1. Use Meaningful Names
2. Handle Missing Values
Provide defaults for optional parameters:
3. Keep Templates Readable
Break complex templates into logical sections:
4. Use Consistent Formatting
Follow JSON formatting rules:
Advanced Template Features
Nested Object Access
Access nested properties from JSON:
Conditional Content
Simple conditional logic:
Array Handling
Work with array data:
Error Handling
Templates gracefully handle missing data:
What's Next?
Now that you understand the template system basics, dive deeper into specific areas:
Template Variables - Complete reference of all available variables
Dynamic Content - Advanced techniques for dynamic responses
Advanced Templating - Complex templating patterns and best practices
Examples in Action
See templates in real-world scenarios:
REST API Example - Complete API with templates
E-commerce API - Product catalog with dynamic data
User Management - User CRUD operations
Need Help?
Template Variables Reference - All available template variables
FAQ - Common template questions
GitHub Discussions - Community help
Last updated
Was this helpful?