# Getting started (oxblade)

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

- Package: oxblade (Laravel for Rust, alpha), crates: `oxblade`, `oxblade-laravel`, `oxblade-macros`, `oxblade-mail`, `oxblade-cli`
- Maintenance: active; Latest release: 0.2.0 (2026-12-05); Framework: laravel-rust 0.2.x; MSRV: Rust 1.99 or later; Licence: MIT
- Links: [Repository](https://github.com/bowerbird-si/oxblade), [crates.io](https://crates.io/crates/oxblade/0.2.0), [docs.rs](https://docs.rs/oxblade/0.2.0), [Changelog](https://oss.bowerbird.si/oxblade/docs/changelog/), [Contributing](https://github.com/bowerbird-si/oxblade/blob/c7fab7e68e7ad4df560796ee140e438944d26598/CONTRIBUTING.md), [Security policy](https://github.com/bowerbird-si/oxblade/blob/c7fab7e68e7ad4df560796ee140e438944d26598/SECURITY.md)
- Based on: [Laravel Blade](https://github.com/illuminate/view): The template language this package implements: Blade's markup, directives and rendered HTML (illuminate/view, MIT).
- Community package, not affiliated with Laravel.
- HTML version: https://oss.bowerbird.si/oxblade/docs/getting-started/
- Source: https://github.com/bowerbird-si/oxblade/blob/c7fab7e68e7ad4df560796ee140e438944d26598/docs/getting-started.md

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

```toml
[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 }}`.

```rust
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](https://github.com/bowerbird-si/oxblade#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:

```toml
[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:

```rust
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.

```rust
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:

```shellscript
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](https://oss.bowerbird.si/oxblade/docs/cli/) 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](https://github.com/bowerbird-si/oxblade/issues) with the
`.blade.php` file, its data and both outputs. See [template safety](https://oss.bowerbird.si/oxblade/docs/template-safety/) for
different output contexts.
