Assets module for UserFrosting

Installs: 46 617

Dependents: 0

Suggesters: 0

Security: 0

Stars: 4

Watchers: 7

Forks: 0

Open Issues: 1


6.2.0 2021-04-21 01:14 UTC

This package is auto-updated.

Last update: 2024-04-06 06:03:06 UTC


Latest Version Software License Join the chat at Donate

Branch Build Coverage Style

Assets is a library originally created for UserFrosting 4 to make it much easier to reference frontend assets in both production and development contexts.

Out of the box it can:

  • Provide an easy way to generate an absolute url to an asset via a locator.
  • Provide a basic level of integration with gulp-bundle-assets and complete integration with @userfrosting/gulp-bundle-assets, making it easy to reference asset bundles.
  • Integrate via a common interface with virtually any bundling system.
  • Integrate with Slim to serve assets that are inaccessible from the public folder, in the development context.
  • Perform url-to-path transformations. Useful for making debuggable URLs that can be reverted back to the path to be used by the Slim asset server.
  • Integrate with your preferred (and extendable) templating engine to provide easy access to asset bundles (that get wrapped with the appropriate tags) and individual assets.


composer require userfrosting/assets


To use Assets, you will need:

  • An instance of ResourceLocator, where you can add your desired search paths.
  • The locator scheme (if it exists) you wish to look for assets in.
  • The base url (used in generating URLs to assets).
  • The base path (used in trimming the absolute path returned by the locator).

use UserFrosting\UniformResourceLocator\ResourceLocator;
use UserFrosting\Assets\Assets;

$basePath = __DIR__;
$baseUrl = '';
$locator = new ResourceLocator($basePath);
$locator->registerStream('assets', '', [

$assets = new Assets($locator, 'assets', $baseUrl);

Optionally 1 more argument can be passed into the Assets constructor, an instance of PrefixTransformer.

Have a look at UserFrosting in dev mode to see this in action!

Asset Bundles

To access asset bundles from an Assets instance, it must first be passed an instance of AssetBundlesInterface via addAssetBundles. The following example demonstates how to integrate with @userfrosting/gulp-bundle-assets (and by extension gulp-bundle-assets). Note that raw bundles are only supported when their configuration is defined as JSON.

Directory Tree

│ └asset-bundles.json


  "bundle": {
    "js/main": {
      "scripts": [
    "css/main": {
      "styles": [


use UserFrosting\Assets\GulpBundleAssetsRawBundles;

// Load asset bundles.
$assetBundles = new GulpBundleAssetsRawBundles('../build/asset-bundles.json');

// Send loaded asset bundles to Assets instance.

// Grab an asset bundle.
// Outputs ["js/bootstrap-3.3.1.js", "js/crud.js"]

Compiled bundles can be used in much the same way, except using GulpBundleAssetsCompiledBundles and the bundlers result file.

If using the original gulp-bundle-assets you'll need to include an additional setting options->result->type->styles/scripts="plain" in each bundle.

The Template Plugin

The template plugin is easy initialized by giving it the Assets instance, and simply gets passed into the templating engine environment of your choice to be used.

use UserFrosting\Assets\AssetsTemplatePlugin;

$assetsPlugin = new AssetsTemplatePlugin($assets);

// Some code that passes it to Twig rendering environment.
{# Gets replaced at runtime with the following. Additional argument is optional. #}
{{ assets.js("js/main", { defer: true }) }}
<script src="" defer="true"></script>
<script src="" defer="true"></script>

Style Guide