CodeIgniter Forums
Asset Loader Library - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Addins (https://forum.codeigniter.com/forumdisplay.php?fid=34)
+--- Thread: Asset Loader Library (/showthread.php?tid=73060)

Pages: 1 2


Asset Loader Library - MGatner - 03-14-2019

Hi all! I'm a long-time user with many live CI3 projects. Managing libraries across them got to be almost as much work as each project, so I developed five "zero conf" libraries to add core functions I needed which could be dropped in and used without setup. I never got around to publishing them, but as I rewrite them for CI4 I'll be making them available. The first library:

Tatter/Assets - "Zero conf" asset loader for CodeIgniter 4

Basic usage:

1. Run: `> composer require tatter/assets`
2. Put CSS & JS files in: public/assets
3. Add in head tag: `helper("tatter\assets"); css();`
4. Add to footer: `js();`


Since this is my first I am eager for feedback, suggestions, and contributions particularly on the "install/update - setup - use" pipeline which will be the same across all five libraries.
Thanks for reading!

EDIT: GitHub link as suggested - https://github.com/tattersoftware/codeigniter4-assets


RE: Asset Loader Library - MGatner - 03-15-2019

Okay people, you clearly don’t understand how much you need this! We’re talking *automatic*, route-specific asset loading! Just drop your JS and CSS files into the folder corresponding to your controller and method and even parameters, or in the asset root to be loaded every time - done!
Go try it, star it, post some feedback here, and send me a pull request or comment with your awesome ideas, mmmmkay?


RE: Asset Loader Library - PaulD - 03-16-2019

Hi,

I like your approach to loading assets according to the route requested.

I have a simple asset library where the controller sets the required assets as required, and my layout library adds them to header or footer as required.

I have never really seen loading assets as an issue and I am sure lots of people have their own solutions. As I said though, I like the route specific assets idea you are using.

Paul


RE: Asset Loader Library - includebeer - 03-16-2019

I don't understand what problem this library tries to solve. How hard is it to load assets? Why would I want to depend on an external library and learn how to use it for a task as simple as loading assets?


RE: Asset Loader Library - MGatner - 03-16-2019

@PaulD that's a great idea using the Controller! I wasn't big on controller extensions in CI3 so I'm still getting used to them in CI4 - might be a good alternative (or addition) to the $routes variable in the optional config file.

EDIT: Is your solution open sourced anywhere that I could look at incorporating parts?


RE: Asset Loader Library - MGatner - 03-16-2019

(03-16-2019, 03:43 PM)includebeer Wrote: I don't understand what problem this library tries to solve. How hard is it to load assets? Why would I want to depend on an external library and learn how to use it for a task as simple as loading assets?

Valid question. A lot of people put all their assets together and minify them so they can load them in a single call on every page, in which case this library probably isn't applicable. The problems I originally was solving in my first version of this:

1. The need to load specific styles and scripts for certain pages, not others
2. The need to use a single header/footer template that could somehow have assets "injected" into them
3. The need for cache resets on assets updates (using timestamped parameter suffixes)

After numerous iterations of the original CI3 library I settled on the directory structure version because it made it easier for me to control which files were loaded while not trying to "track" them anywhere else (though the $routes variable offers this additional functionality).
Thanks for the question! Would you share how you handle assets?


RE: Asset Loader Library - includebeer - 03-16-2019

All my controllers extends a base controller. In there I have 2 private arrays for JS and CSS files. I have functions to inject the right filenames in these array for different needs. Let say I need jQuery and Font Awesome, my controller method will call some function that will add the JS and CSS files required for jQuery and FontAwesome. When I'm ready to call the view, I call a function that creates the links to be included in the view. Then the view simply output that variable that contains the HTML snippet to include the JS and CSS files. So all I have in my controller will be something like this->addJquery(),this->addFontAwesome(), this->getHeader().


RE: Asset Loader Library - MGatner - 03-16-2019

An elegant solution! Sounds similar to what PaulD is doing, and definitely a solid way to use CI4’s native setup. Thanks for sharing it.


RE: Asset Loader Library - dave friend - 03-17-2019

(03-15-2019, 04:45 PM)MGatner Wrote: Okay people, you clearly don’t understand how much you need this!

Ha, good one Smile No, really. A good pitch and a very interesting approach.

In my case, I would have paid more attention to your first post if you had also included a link to the GitHub page.
I was a long way from wanting to do an install just to check out the code - so I ignored the Packagist link. Just say'n.


RE: Asset Loader Library - MGatner - 03-17-2019

Ah thanks for the feedback! I’m new to Packagist so I assumed that anything publically available on Packagist could take you to a repo - was that an incorrect assumption?
Anyways thanks for checking it out! Already working on the next library, for user message handling.

EDIT: Added a GitHub link to the original!