PHPackages                             userfrosting/vite-php-twig - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Templating &amp; Views](/categories/templating)
4. /
5. userfrosting/vite-php-twig

ActiveLibrary[Templating &amp; Views](/categories/templating)

userfrosting/vite-php-twig
==========================

Vite function for Twig templates

1.1.0(1y ago)211.4k↑1112.5%1MITPHPPHP ^8.1CI passing

Since Jun 29Pushed 1y ago2 watchersCompare

[ Source](https://github.com/userfrosting/vite-php-twig)[ Packagist](https://packagist.org/packages/userfrosting/vite-php-twig)[ Docs](https://github.com/userfrosting/vite-php-twig)[ RSS](/packages/userfrosting-vite-php-twig/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (4)Dependencies (10)Versions (5)Used By (0)

Vite Manifest Support for PHP &amp; Twig
========================================

[](#vite-manifest-support-for-php--twig)

[![Version](https://camo.githubusercontent.com/cee1bdfe2e7fddd480ef9ebf3a5d36d78bf0b153453fe0ed7b14624967ca4091/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f72656c656173652f7573657266726f7374696e672f766974652d7068702d747769673f736f72743d73656d766572)](https://github.com/userfrosting/vite-php-twig/releases)[![PHP Version](https://camo.githubusercontent.com/67b59877746aa7768c6ccb01287c1b64b99dc5a9e392c212acd56cc4edff148a/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d253545382e312d627269676874677265656e)](https://camo.githubusercontent.com/67b59877746aa7768c6ccb01287c1b64b99dc5a9e392c212acd56cc4edff148a/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d253545382e312d627269676874677265656e)[![License](https://camo.githubusercontent.com/074b89bca64d3edc93a1db6c7e3b1636b874540ba91d66367c0e5e354c56d0ea/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e737667)](LICENSE)[![Build](https://camo.githubusercontent.com/a0d599a7419ffd07f0cebbf9f9cc2c8deb08580952f057a1bd1c68c84a41899c/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f7573657266726f7374696e672f766974652d7068702d747769672f4275696c642e796d6c3f6c6f676f3d676974687562)](https://github.com/userfrosting/vite-php-twig/actions)[![Codecov](https://camo.githubusercontent.com/916d51ae7fe8013bbb06a9dba612cae339a7dce4cbf25becd2eb8a860bc17e7b/68747470733a2f2f636f6465636f762e696f2f67682f7573657266726f7374696e672f766974652d7068702d747769672f6272616e63682f6d61696e2f67726170682f62616467652e737667)](https://app.codecov.io/gh/userfrosting/vite-php-twig/branch/main)[![StyleCI](https://camo.githubusercontent.com/52d8ed56bb72d6bc1316b0a9bc11d7d48ce3b6012a1e7fb01a49af976d196cfc/68747470733a2f2f6769746875622e7374796c6563692e696f2f7265706f732f3832313934353034352f736869656c643f6272616e63683d6d61696e267374796c653d666c6174)](https://github.styleci.io/repos/821945045)[![PHPStan](https://camo.githubusercontent.com/683775fedc810afb1813d423badf656f48bb6fbab6867f04cc74d0a77bdfbe2b/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f7573657266726f7374696e672f766974652d7068702d747769672f5048505374616e2e796d6c3f6c6162656c3d5048505374616e)](https://github.com/userfrosting/vite-php-twig/actions/workflows/PHPStan.yml)[![Donate](https://camo.githubusercontent.com/b7dc459938f321309f24018c6839ab1a99d6f0233f063a741ce7be1f607b88f5/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f446f6e6174652d4275792532304d6525323061253230436f666665652d626c75652e737667)](https://ko-fi.com/lcharette)

Vite Manifest function for PHP &amp; Twig Templates. Allows [Vite manifest](https://vitejs.dev/guide/backend-integration) integration in PHP &amp; Twig Templates without Symfony. Optimized for PHP-DI style containers.

Inspired by [kellerkinderDE/vite-encore-bundle](https://github.com/kellerkinderDE/vite-encore-bundle) &amp; [PHP-Vite](https://github.com/mindplay-dk/php-vite).

Installation
------------

[](#installation)

```
composer require userfrosting/vite-php-twig

```

Documentation &amp; Usage
-------------------------

[](#documentation--usage)

### Using standalone

[](#using-standalone)

```
$manifest = new ViteManifest('.vite/manifest.json');

// Get files for `views/foo.js` entry
$manifest->getScripts('views/foo.js'); // Scripts
$manifest->getStyles('views/foo.js'); // Styles
$manifest->getImports('views/foo.js'); // Preloads

// Render HTML tags for `views/foo.js` entry
$manifest->renderScripts('views/foo.js'); // Scripts
$manifest->renderStyles('views/foo.js'); // Styles
$manifest->renderPreloads('views/foo.js'); // Preloads

// If you have multiple entry point scripts on the same page, you should pass them in a single call to avoid duplicates - for example:
$manifest->getScripts('views/foo.js', 'views/bar.js');
```

Tip

`ViteManifest` implements `\UserFrosting\ViteTwig\ViteManifestInterface` if you prefer to type-hint against interfaces, for use with dependency injection.

### Using with Twig

[](#using-with-twig)

Important

Requires Twig 3 or newer

Vite writes a `manifest.json` file that contains all of the files needed for each [Rollup Input](https://rollupjs.org/configuration-options/#input) or "entry". To reference entries in Twig, you need to add the `ViteTwigExtension` extension to the Twig Environment. This accepts a `ViteManifest`, which itself accepts the path to the `manifest.json`.

```
use UserFrosting\ViteTwig\ViteManifest;
use UserFrosting\ViteTwig\ViteTwigExtension;
use Twig\Environment;
use Twig\Loader\FilesystemLoader;

$manifest = new ViteManifest('.vite/manifest.json');
$extension = new ViteTwigExtension($manifest);

// Create Twig Environment and add extension
$loader = new FilesystemLoader('./path/to/templates');
$twig = new Environment($loader);
$twig->addExtension($extension);
```

Now, to render all of the `script` and `link` tags for a specific "entry" (e.g. `views/foo.js`), you can:

```
{{ vite_js('views/foo.js') }}
{{ vite_css('views/foo.js') }}
{{ vite_preload('views/foo.js') }}
```

If you have multiple entry point scripts on the same page, you should pass them in a single call to avoid duplicates - for example:

```
{{ vite_js('views/foo.js', 'views/bar.js') }}
```

Standalone CSS, SCSS, SASS, and LESS files can also be rendered directly, as long as they are defined in your Vite config as a rollup input.

```
{{ vite_css('my_theme.less') }}
```

### Vite Default Port

[](#vite-default-port)

By default, Vite will use port `5173`. However, if the port is already being used, Vite will automatically try the next available port so this may not be the actual port the server ends up listening on. Since a PHP application doesn't know which port is being used by Vite, the port can be forced in the `vite.config.js` file inside your project's root directory using [`server.strictPort`](https://vitejs.dev/config/server-options#server-strictport) and [`server.port`](https://vitejs.dev/config/server-options#server-port):

```
server: {
    strictPort: true,
    port: 3000,
},
```

For more information on how to configure Vite, see the [official documentation](https://vitejs.dev/config/).

### ViteManifest Options

[](#vitemanifest-options)

```
$manifest = new ViteManifest(
    manifestPath: '.vite/manifest.json',
    basePath: 'dist/',
    serverUrl: 'http://[::1]:5173/',
    devEnabled: true,
);
```

- `manifestPath` - string: Points to the Vite `manifest.json` file created for the production build. Optional if you're using the dev server.
- `basePath` - string: Public base path from which Vite's published assets are served. The assets paths will be relative to the `outDir` in your Vite configuration. It could also point to a CDN or other asset server if you are serving assets from a different domain.
- `serverUrl` - string: The Vite server URL, including port. Can be used to specify a non-default port if used.
- `devEnabled` - bool: Indicates whether the application is running in development mode (i.e. using Vite server). Defaults to false.

See Also
--------

[](#see-also)

- [Changelog](CHANGELOG.md)
- [License](LICENSE)

References
----------

[](#references)

- [Vite manifest](https://vitejs.dev/guide/backend-integration)
- [kellerkinderDE/vite-encore-bundle](https://github.com/kellerkinderDE/vite-encore-bundle)
- [PHP-Vite](https://github.com/mindplay-dk/php-vite)

###  Health Score

37

—

LowBetter than 83% of packages

Maintenance44

Moderate activity, may be stable

Popularity30

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity52

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 76.9% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~84 days

Total

4

Last Release

435d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/a58517f6c9aea65a660cb28e0fe5e3139696c6d5a7c96f91576aff0f0267c750?d=identicon)[lcharette](/maintainers/lcharette)

---

Top Contributors

[![lcharette](https://avatars.githubusercontent.com/u/2566513?v=4)](https://github.com/lcharette "lcharette (10 commits)")[![imagoiq](https://avatars.githubusercontent.com/u/12294151?v=4)](https://github.com/imagoiq "imagoiq (3 commits)")

---

Tags

twigvite

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/userfrosting-vite-php-twig/health.svg)

```
[![Health](https://phpackages.com/badges/userfrosting-vite-php-twig/health.svg)](https://phpackages.com/packages/userfrosting-vite-php-twig)
```

###  Alternatives

[twig/extra-bundle

A Symfony bundle for extra Twig extensions

91292.0M315](/packages/twig-extra-bundle)[twig/intl-extra

A Twig extension for Intl

36663.2M221](/packages/twig-intl-extra)[rcrowe/twigbridge

Adds the power of Twig to Laravel

9105.9M50](/packages/rcrowe-twigbridge)[twig/string-extra

A Twig extension for Symfony String

21946.0M133](/packages/twig-string-extra)[twig/cssinliner-extra

A Twig extension to allow inlining CSS

23018.5M55](/packages/twig-cssinliner-extra)[symfony/ux-twig-component

Twig components for Symfony

21814.8M162](/packages/symfony-ux-twig-component)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
