Docs

Template

MLView is MonkeysLegion’s zero-dependency template engine.

It gives you Vue/Blade-style components & slots, a tiny AST compiler that turns markup into plain PHP, and a renderer with automatic disk caching—yet ships in well under 300 LOC.

1 · Installation

composer require monkeyscloud/monkeyslegion-template

Requires only PHP 8.4—no Twig, no extensions.

2 · Hello, World

<!-- resources/views/hello.ml.php -->
<h1>Hello, {{ $name }}!</h1>
use MonkeysLegion\Template\MLView;

$view = new MLView(base_path('resources/views'));

echo $view->render('hello', ['name' => 'Alice']);

The first call parses → compiles → caches the template; subsequent renders are just an include.

3 · Component Syntax

<x-card>
  <x-slot:title>
    Welcome, {{ $user->name }}
  </x-slot:title>

  <p>Your last login was {{ $user->lastLogin->diffForHumans() }}.</p>
</x-card>
  • <x-component> ↔ PHP class App\View\Components\Component.

  • <x-slot:name> fills the component’s $this->slot('name').

  • {{ … }} escapes htmlspecialchars(); {!! … !!} prints raw.

Defining a component

namespace App\View\Components;

use MonkeysLegion\Template\Component;

final class Card extends Component
{
    public function render(): string
    {
        return <<<ML
<div class="card shadow">
  <header class="card__header">{{ \$this->slot('title') }}</header>
  <main  class="card__body">  {{ \$this->slot('default') }}</main>
</div>
ML;
    }
}

Drop the file anywhere under app/View/ComponentsLoader auto-discovers it.

4 · Engine Pipeline

Step

Class

What happens

Load

Loader

Finds template file; returns source

Parse

Parser

Builds an AST of ComponentNode, SlotNode, & text nodes

Compile

Compiler

Converts AST into a cached .php runnable

Render

Renderer

Includes compiled file, passing $data array

All of this is wired in MLView, so you normally interact with one facade method: render($name, $data = []).

5 · Caching & Auto-reload

  • Compiled templates land in storage/cache/views (configurable).

  • The cache key is a checksum + mtime; edit the source file and MLView recompiles automatically.

  • Clear everything with ./vendor/bin/ml view:clear.

6 · Blade-style Helpers

Helper

Description

@if / @elseif / @endif

Control blocks

@foreach / @endforeach

Loops (auto-escapes inside {{ }})

@include('partial')

Inlines another template

@csrf

Outputs <input type="hidden" …> token (when the CSRF package is installed)

(All helpers compile down to raw PHP inside the cached file—no runtime cost.)

7 · Extending the Compiler

use MonkeysLegion\Template\Compiler;

Compiler::directive('datetime', function (string $expr): string {
    return "date('F j, Y', strtotime($expr))";
});

Now, in any template:

<span>@datetime($post->published_at)</span>

8 · DI Registration Snippet

return [
    MLView::class => fn () => new MLView(
        viewsPath:  base_path('resources/views'),
        cachePath:  base_path('storage/cache/views'),
    ),
];

Controllers, CLI commands, and even queue workers can all ask for MLView.

9 · Roadmap

  • Hot-module reloading in the dev server

  • Native Markdown component (<x-md>…</x-md>)

  • PHP-standalone components that skip template files entirely

Pull requests welcome—see the GitHub board for “help wanted” issues.

License

MIT © 2025 MonkeysCloud