PHP code example of rumenx / php-assets

1. Go to this page and download the library: Download rumenx/php-assets library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

rumenx / php-assets example snippets


use Rumenx\Assets\Asset;

// Add assets
Asset::add('style.css');
Asset::add('theme.less');
Asset::add('app.js');
Asset::add(['extra.js', 'extra2.js'], 'footer');

// Add inline style or script
Asset::addStyle('body { background: #fafafa; }');
Asset::addScript('console.log("Hello!");');

// Output in your template
Asset::css();      // <link rel="stylesheet" ...>
Asset::less();     // <link rel="stylesheet/less" ...>
Asset::js();       // <script src=...></script>
Asset::styles();   // <style>...</style>
Asset::scripts();  // <script>...</script>

// Use cachebuster (file-based)
Asset::setCachebuster(__DIR__.'/cache.json');

// Use cachebuster (function-based)
Asset::setCacheBusterGeneratorFunction(function($file) {
    return md5($file);
});

// Custom domain or prefix
Asset::setDomain('https://cdn.example.com/');
Asset::setPrefix('X-');

use Rumenx\Assets\Asset;

// In your controller or anywhere in your Laravel app
Asset::add('main.css');
Asset::add('main.js');

// In your Blade template
{!! Asset::css() !!}
{!! Asset::js() !!}

use Rumenx\Assets\Asset;

// In your controller
Asset::add('main.css');
Asset::add('main.js');

// In a Twig template
{{ asset_css()|raw }}
{{ asset_js()|raw }}

// src/Twig/AssetExtension.php
use Rumenx\Assets\Asset;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;

class AssetExtension extends AbstractExtension
{
    public function getFunctions(): array
    {
        return [
            new TwigFunction('asset_css', fn() => Asset::css()),
            new TwigFunction('asset_js', fn() => Asset::js()),
        ];
    }
}
bash
composer