daun / laravel-latte
Use Latte templates in Laravel views
Requires
- php: ^8.1
- laravel/framework: ^9.0 || ^10.0 || ^11.0
- latte/latte: ^3.0
Requires (Dev)
- larastan/larastan: ^2.9
- laravel/pint: ^1.14
- mockery/mockery: ^1.6
- orchestra/testbench: ^8.21
- pestphp/pest: ^2.34
- pestphp/pest-plugin-laravel: ^2.3
This package is auto-updated.
Last update: 2024-12-20 17:02:17 UTC
README
Add support for the Latte templating engine in Laravel views.
Features
- Render
.latte
views - Latte engine configurable via facade
- Translation provider to access localized messages
- Laravel-style path resolution when including partials
- Extensive test coverage
Installation
composer require daun/laravel-latte
Requirements
- PHP 8.1+
- Laravel 9/10/11
Usage
Installing the composer package will automatically register a Service Provider with your Laravel app.
You can now render Latte files like you would any other view. The example below will render the
view at resources/views/home.latte
using Latte.
Route::get('/', function() { return view('home'); });
Configuration
The package will read its configuration from config/latte.php
. Use Artisan to publish and
customize the default config file:
php artisan vendor:publish --provider="Daun\LaravelLatte\ServiceProvider"
Localization
The package includes a custom translator extension that acts as a bridge to Laravel's translation
service. It allows using any translations registered in your app to be used in Latte views, using
either the _
tag or the translate
filter:
{_'messages.welcome'} {('messages.welcome'|translate)}
You can pass in parameters as usual:
{_'messages.welcome', [name: 'Adam']} {('messages.welcome'|translate:[name: 'Adam'])}
Translate using a custom locale by passing it after or in place of the params:
{_'messages.welcome', [name: 'Mary'], 'fr'} {_'messages.welcome', 'fr'}
Pluralization works using the transChoice
filter.
{('messages.apples'|transChoice:5)}
Path resolution
The package includes a custom loader that allows including partials from subdirectories using Laravel's dot notation to specify folders.
{* resolves to /resources/views/partials/slideshow/image.latte *} {include 'partials.slideshow.image'}
To specifically include files relative to the current file, prefix the path with ./
or ../
:
{include './image.latte'}
Default layout
If you require a common layout file for all views, you can define a default layout in
config/latte.php
. Any views without a specifically set layout will now be merged into that layout.
[ // /resources/views/layouts/default.latte 'default_layout' => 'layouts.default' ]
Configuring Latte
Extensions
To extend Latte and add your own tags, filters and functions, you can use the extensions
array
in config/latte.php
to supply a list of Latte extensions to register automatically.
[ 'extensions' => [ \App\View\Latte\MyExtension::class ] ]
Facade
You can also directly access and configure the Latte engine instance from the Latte
facade.
Modify its config, add custom filters, etc.
use Daun\LaravelLatte\Facades\Latte; use Illuminate\Support\ServiceProvider; class AppServiceProvider extends ServiceProvider { public function boot() { Latte::addFilter('plural', fn($str) => Str::plural($str)); } }
Events
If you need to be notified when the Latte engine is created, listen for the LatteEngineCreated
event to receive and customize the returned engine instance.
use Daun\LaravelLatte\Events\LatteEngineCreated; use Illuminate\Support\ServiceProvider; class AppServiceProvider extends ServiceProvider { public function register() { Event::listen(function (LatteEngineCreated $event) { $event->engine->setAutoRefresh(true); }); } }