This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

daftspunky
daftspunky

This code, when used in a Component, will combine all the assets and add them to the front end where the {% scripts %} tag is used. The files should be located in the assets folder of the component.

    use System\Classes\CombineAssets;

    public function onRun()
    {
        $assets = [
            'assets/vendor/mustache/mustache.js',
            'assets/js/vendor/jquery.ui.widget.js',
            'assets/js/vendor/canvas-to-blob.js',
            'assets/vendor/load-image/js/load-image.js',
            'assets/vendor/load-image/js/load-image-ios.js',
            'assets/vendor/load-image/js/load-image-orientation.js',
            'assets/vendor/load-image/js/load-image-meta.js',
            'assets/vendor/load-image/js/load-image-exif.js',
            'assets/vendor/load-image/js/load-image-exif-map.js',
            'assets/vendor/file-upload/js/jquery.iframe-transport.js',
            'assets/vendor/file-upload/js/jquery.fileupload.js',
            'assets/vendor/file-upload/js/jquery.fileupload-process.js',
            'assets/vendor/file-upload/js/jquery.fileupload-image.js',
            'assets/vendor/file-upload/js/jquery.fileupload-audio.js',
            'assets/vendor/file-upload/js/jquery.fileupload-video.js',
            'assets/vendor/file-upload/js/jquery.fileupload-validate.js',
        ];

        $this->addJs(CombineAssets::combine($assets, $this->assetPath));
    }

Enjoy!

Last updated

d.negativa
Rob Ballantyne
Rob Ballantyne

I think this should read asset folder of the plugin rather than the component. It's very useful, thanks!

david19236
david19236

Exactly what i was looking for. Thanks!

skygdi
skygdi

Thank you very much. That is what I wanted.

joseph.d
joseph.d

I don't seem to be able to get this example to work. I receive the error:

The source "/plugins/namespace/pluginname/assets/script.js" is not in the root directory "/project/root/dir"

...even though /project/root/dir/plugins/namespace/pluginname/assets/script.js clearly exists in my project.

Here's a simplified version of my code. Obviously I have more assets:

    use System\Classes\CombineAssets;

    public function onRun()
    {
        $assets = [
            'assets/script.js',
        ];

        $this->addJs(CombineAssets::combine($assets, $this->assetPath));
    }
strk
strk

This should fix it

use System\Classes\CombineAssets;

public function onRun()
{
    $scripts = [
        'acme/myplugin/assets/main.js'
    ];

    $this->addJs(CombineAssets::combine($scripts, plugins_path())); 
}

Last updated

1-7 of 7

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.