Laravel integration

laravel-rust-format Alpha Laravel for Rust

Used in production across multiple projects.

Install laravel-rust-format alongside Laravel for Rust 0.2 (>=0.2.0, <0.3). The default minijinja feature enables the framework’s MiniJinja integration.

Register the provider

Add FormattingServiceProvider to the application builder. This is a fragment for an app’s configuration.

use laravel::Application;
use laravel_rust_format::{FormattingConfig, FormattingServiceProvider};

let _builder = Application::configure()
    .with_providers(|providers| {
        providers.add(FormattingServiceProvider);
    })
    .with_config(|config, _| {
        config.section_mut::<FormattingConfig>().currency = "EUR".into();
    });

The optional with_config callback above overrides the currency in Rust. You can also set defaults in the app environment.

VariableDefaultUsed by
FORMATTING_CURRENCYUSDnumber_currency when the code is omitted, empty or "0"
FORMATTING_TIMEZONEUTCTimestamp display in Carbon filters and the zone for php_date and the date constructors
FORMATTING_MAX_OUTPUT_BYTES8388608Raw formatter output before MiniJinja autoescaping
FORMATTING_MAX_COLLECTION_ITEMS100000Template collection output, including nested entries

The provider discovers FormattingConfig and validates its ISO 4217 currency code and time zone at boot. The number and relative-time helpers default to English and accept explicit locales. The template configuration is separate from the core’s explicit arguments and from the application’s own time zone.

Set FormattingConfig::limits to a core php::Limits value to configure budgets from Rust. Core functions accepting limits can reject oversized allocations during formatting. Other outputs are checked when the adapter receives them.

Use the application clock

Inside a Laravel request or application test context, diff_for_humans reads laravel::now() when other is omitted. Use app.travel(duration) or app.travel_to(timestamp) in a Laravel test to control that instant. An explicit other compares with the supplied date and uses Carbon’s comparison wording by default.

Use the helpers without the provider

Rust functions work by importing laravel_rust_format::{php, str, number, carbon, js} or the same modules from the core crate. For a standalone MiniJinja environment, register the filters directly using the framework’s MiniJinja re-export. Use FormattingConfig::default() or FormattingConfig::new(currency, timezone) and override its public fields as needed.

use laravel::view::minijinja::{Environment, context};
use laravel_rust_format::{FormattingConfig, register_filters};

let mut env = Environment::new();
register_filters(&mut env, &FormattingConfig::default());

let rendered = env.render_str(
    "{{ amount|number_format(2) }}",
    context! { amount => 1234.5 },
).unwrap();
assert_eq!(rendered, "1,234.50");

In a standalone environment, supply other to diff_for_humans. Disable the adapter’s default features if your app only needs the provider’s config and the Rust API. For projects without a Laravel for Rust dependency, install the core directly.

Community package, not affiliated with Laravel.