Skip to content

Add Blade template engine support #47

Description

@sixmonkey

Add Blade template engine support

Summary

Add BladeAdapter as a second view engine alongside Twig. The adapter pattern introduced in refactor/views makes this straightforward — Blade is just another adapter that consumes _helpers, _directives and _shared from the View Factory.

Background

illuminate/view (already a dependency) ships Blade out of the box. The ViewServiceProvider now orchestrates engine-agnostic extensions via AbstractViewExtension — BladeAdapter just needs to map these to Blade's equivalents.

What needs to be done

BladeAdapter — src/View/Engines/Blade/BladeAdapter.php

Implements ViewAdapterInterface:

public function register(Application $app): void
{
    // BladeCompiler with cache path
    $app->singleton('blade.compiler', fn($c) => new BladeCompiler(
        $c['files'],
        $c['path.cache'] . '/Blade',
    ));
}

public function boot(Factory $view, Application $app): void
{
    $blade = $app['blade.compiler'];

    // _helpers → {{ helper($value) }}
    foreach ($view->getShared()['_helpers'] ?? [] as $name => $callable) {
        $blade->directive($name, fn($expr) => "<?php echo {$name}({$expr}); ?>");
    }

    // _directives → @directive
    foreach ($view->getShared()['_directives'] ?? [] as $name => $callable) {
        $blade->directive($name, fn($expr) => "<?php {$name}({$expr}); ?>");
    }

    // Register .blade.php as a view engine
    $view->addExtension(
        'blade.php',
        'blade',
        fn() => new CompilerEngine($blade, $app['files']),
    );

    // Shared variables available in Blade via View::share() automatically
}

Add to ViewServiceProvider::$adapters

protected array $adapters = [
    TwigAdapter::class,
    BladeAdapter::class, // 🆕
];

TemplateServiceProvider — Brain Hierarchy already supports both

$finder = new ByFolders($layoutPaths, 'twig', 'blade.php');

Already done in refactor/views — no changes needed.

Tests

  • BladeAdapterTest — registers blade.compiler, registers .blade.php extension
  • Integration test — .blade.php template renders correctly
  • Coexistence test — .twig and .blade.php can coexist in same project

Notes

  • illuminate/view already includes BladeCompiler and CompilerEngine — no new dependencies
  • _helpers mapping to Blade is slightly different from Twig (no pipe syntax) — document in views.md
  • fn global is Twig-only — no Blade equivalent needed (call PHP directly in Blade)
  • getGlobals() equivalent in Blade is View::share() — already handled via share() in AbstractViewExtension

Acceptance criteria

  • .blade.php templates render in the Layout directory
  • Helpers from AbstractViewExtension available in Blade as {{ helper($value) }}
  • Directives from AbstractViewExtension available as @directive
  • Shared variables from share() available in Blade templates
  • Twig and Blade coexist — both engines always active
  • Tests green
  • views.md updated with Blade examples

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    • Status
      Backlog

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions