Skip to content

Commit

Permalink
Added a simple Twig extension to simplify using Vite in a template
Browse files Browse the repository at this point in the history
  • Loading branch information
bryannielsen committed Nov 27, 2024
1 parent c7028a4 commit 8d9d624
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- Support for year, month, and day parameters on Channel Entries tag
- Enhanced support for many template tags including `exp:channel:form, exp:member:custom_profile_data, exp:member:edit_avatar, exp:member:edit_profile, exp:member:forgot_password_form, exp:member:forgot_username_form, exp:member:login_form, exp:member:logout_form, exp:member:memberlist, exp:member:member_search, exp:member:registration_form`
- Template Generator support for Twig and Blade template engines
- Twig extension to simplify using Vite in a template, e.g. `{{ vite('ee::assets/style.scss') | raw }}`

### Fixed

Expand Down
2 changes: 2 additions & 0 deletions src/CoilpackServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ public function boot()
Route::macro('templates', new Routing\TemplateRoute);
Route::mixin(new Routing\CoilpackRoutes);

\TwigBridge\Facade\Twig::addExtension(new \Expressionengine\Coilpack\View\Extensions\TwigVite);

$this->loadRoutesFrom(__DIR__.'/../routes/web.php');

Event::listen(function (\Illuminate\Routing\Events\RouteMatched $event) {
Expand Down
23 changes: 23 additions & 0 deletions src/View/Extensions/TwigVite.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Expressionengine\Coilpack\View\Extensions;

use Illuminate\Foundation\Vite;
use Twig\Extension\AbstractExtension;
use Twig\Markup;
use Twig\TwigFunction;

class TwigVite extends AbstractExtension
{
public function getFunctions(): array
{
return [
new TwigFunction('vite', [$this, 'vite']),
];
}

public function vite(string $resource): string
{
return new Markup((new Vite)->__invoke($resource), 'UTF-8');
}
}

0 comments on commit 8d9d624

Please sign in to comment.