Skip to content
Abdelkarim Mateos Sanchez edited this page Dec 12, 2025 · 3 revisions

Laravel Mustache Resolver

A powerful, framework-agnostic mustache template resolver for PHP applications with first-class Laravel integration.

Overview

Laravel Mustache Resolver provides a flexible and extensible system for resolving mustache-style template expressions like {{User.name}}, {{Device.status}}, or complex compound expressions with conditions and math operations.

Key Features

  • Framework Agnostic: Core functionality works without Laravel
  • First-class Laravel Integration: Service provider, facade, and Eloquent support
  • Extensible Resolver Pipeline: Add custom resolvers for your domain
  • Compound Expressions: USE clause syntax for complex queries
  • Temporal Expressions: Time-based conditions, CRON support, weekday/weekend checks
  • 26 Built-in Formatters: Date/time, numeric, and string transformations
  • Safe Math Expressions: Arithmetic without eval()
  • Condition Validation: Throw exceptions when conditions fail
  • Full Test Coverage: 526+ tests with strict typing

Quick Start

use AichaDigital\MustacheResolver\Core\Context\ResolutionContext;
use AichaDigital\MustacheResolver\Core\Pipeline\PipelineBuilder;

// Build a resolution pipeline
$pipeline = PipelineBuilder::default()->build();

// Create context with your data
$context = ResolutionContext::fromArray([
    'User' => $user,           // Eloquent model
    'Device' => $device,       // Another model
    'config' => $configArray,  // Array data
]);

// Parse and resolve a template
$parser = new MustacheParser();
$tokens = $parser->parse('Hello {{User.name}}, your device {{Device.serial}} is {{Device.status}}');

foreach ($tokens as $token) {
    $value = $pipeline->resolve($token, $context);
    // Replace in template...
}

Table of Contents

  1. Installation
  2. Basic Usage
  3. Resolvers
  4. Compound Expressions
  5. Formatters
  6. Math Expressions
  7. Temporal Expressions
  8. Configuration
  9. Laravel Integration
  10. Testing
  11. API Reference

Supported Expression Types

Type Example Description
Model {{User.name}} Access model attributes
Relation {{User.department.name}} Traverse relationships
Collection {{User.posts.*.title}} Map over collections
Variable {{$myVariable}} Context variables
Dynamic {{Device.$config.field}} Dynamic field access
Null Coalesce {{User.nickname ?? 'Guest'}} Default values
Function {{now()}} Function calls
Compound USE {var} => {{expr}} && ... Complex expressions
Temporal {{TEMPORAL:isDue('weekday')}} Time-based conditions
Now {{NOW:format('Y-m-d')}} Current datetime
Today {{TODAY:startOfDay}} Today's date helpers

Requirements

  • PHP 8.2 or higher
  • Laravel 10, 11, or 12 (optional)

License

This package is licensed under the AGPL-3.0-or-later license.

Contributing

Contributions are welcome! Please see our contribution guidelines before submitting a pull request.


Next: Installation

Clone this wiki locally