Getting started

oxblade Alpha Laravel for Rust

Add the oxblade engine and Serde JSON to render Blade templates with JSON data in a standalone Rust program.

[dependencies]
oxblade = "0.2"
serde_json = "1"

Render a template on its own

An Environment renders views from a loader. MemoryLoader holds views in memory. FilesystemLoader reads .blade.html files from a directory. Create resources/views/welcome.blade.html with Hello {{ name }}.

use oxblade::{engine::Environment, loader::FilesystemLoader};
use serde_json::json;

let env = Environment::builder(FilesystemLoader::new("resources/views")).build();
let html = env.render_json("welcome", &json!({ "name": "Ada" }))?;

Build the environment once and reuse it: it caches compiled templates between renders.

The quick start shows a layout, a loop and a component in one program.

Use it in a Laravel for Rust app

Add the adapter next to the engine:

[dependencies]
oxblade = "0.2"
oxblade-laravel = "0.2"

Register the service provider after the framework's default view engine, so that views with a .blade.html file render through oxblade:

use oxblade_laravel::{BladeEngine, BladeServiceProvider};

providers.add(BladeServiceProvider::new(|_| BladeEngine::new()));

Views live in resources/views, as .blade.html files. route(), url(), asset(), @csrf, @method, @auth, @can and the other Laravel helpers read the current request. The adapter supports laravel-rust 0.2 and is tested with 0.2.0.

config(key) exposes app.name, app.env, app.url and app.debug by default. Register additional public values with BladeEngine::allow_config. A key outside the allow-list returns a render error.

Register component families

Use EnvironmentBuilder::component_prefix to handle a family of tags with one callback. For example, a badge handler serves both <x-badge-new /> and <x-badge-sale />. The callback receives the full name without x-, the suffix after the prefix and hyphen, and the component context. Read supplied values from context.data(), the attribute bag from context.attributes(), and rendered slots from context.slots().

The callback can write to the bounded output buffer and return PrefixComponentRender::Direct, or return PrefixComponentRender::View with a view name. The view receives the same props, attributes and slots. A dynamic component name follows the same resolution order. Exact aliases and class components take priority, followed by the longest matching prefix and then anonymous component views. An app can use the same callback with BladeEngine::component_prefix.

Extend Blade from a package

Call register_blade_extensions from the package's service provider configuration hook. The callback receives the application's final configuration at boot.

use laravel::{config::{Config, Env}, foundation::ServiceProvider};
use oxblade::extend::PrefixComponentRender;
use oxblade_laravel::register_blade_extensions;

pub struct BadgeProvider;

impl ServiceProvider for BadgeProvider {
    fn config(&self, config: &mut Config, _: &Env) {
        register_blade_extensions(config, |blade, _| {
            blade
                .component_prefix("badge", |_, suffix, _, out| {
                    out.raw("<strong>")?;
                    out.escaped(suffix)?;
                    out.raw("</strong>")?;
                    Ok(PrefixComponentRender::Direct)
                })
                .directive("badge_label", "Badges")
        });
    }
}

In an app that uses BladeServiceProvider, add providers.add(BadgeProvider) to use <x-badge-new /> and @badge_label(). Blade builds package registrations once at boot in provider order. Later packages can replace named directives, components and helpers. The application's own BladeEngine registrations and .extend(..) callbacks apply last.

Move PHP Blade views across

The oxblade command converts .blade.php views to .blade.html, rewriting PHP expressions as Rust ones and marking anything that needs a person with a TODO comment:

cargo install --locked oxblade-cli --version 0.2.0
oxblade migrate resources/views --out resources/views
oxblade lang lang --out lang

oxblade lang converts PHP language files (lang/{locale}/{group}.php) into the JSON files the adapter loads. See the CLI reference for all commands and options.

When something renders differently

The conformance suite compares oxblade output with Laravel 13.39.0 for the checked-in cases. Rust-style expressions use strict comparisons and Boolean conditions. A literal attribute echo such as <x-card title="{{ name }}" /> escapes Tom & Jerry to Tom &amp; Jerry when the component forwards the title in its attribute bag. Both engines match on that path. If the component declares the title as a prop and echoes it into the body, Laravel emits Tom &amp;amp; Jerry and oxblade emits Tom &amp; Jerry.

Collections in 0.1 differ from Laravel's in two places:

  • group_by and key_by turn integer keys into strings, so sort_keys() puts '10' before '2'.
  • On a list, group_by(selector, true) indexes each group's items from zero instead of keeping their original positions.

Both are listed in the spec's collection notes and will change in a later release.

To report a difference, open an issue with the .blade.php file, its data and both outputs. See template safety for different output contexts.