YUI PHP Loader Beta Release

By Eric MiragliaSeptember 14th, 2009

The YUI PHP Loader is a server-side utility for loading YUI JavaScript and CSS; version 1.0.0 beta 1 is available for download from YUILibrary.com today.

PHP Loader, originally written by longtime YUI engineer Adam Moore and now developed and maintained by fellow Yahoo Chad Auld, has several key features that make it easier to use YUI in PHP-based applications:

  • Reliable, sorted loading of dependencies: You specify the version of YUI that you’re using, the modules you want to use, and PHP Loader outputs the requisite script and css tags for your implementation. Even if YUI’s dependency tree changes in a future version, your code won’t have to.
  • Support for performance best-practices: PHP Loader has three strategies to help you reduce HTTP requests — support for the Yahoo! CDN and its combo-handler (which aggregates YUI files into single HTTP requests on the fly), support for YUI’s rollup files, and (in the event you don’t want to serve YUI from Yahoo!’s servers) a lightweight combo-handler of its own. Server-side performance is fast as well, leveraging PHP’s APC cache.
  • Extensible metadata format: YUI PHP Loader ships with YUI library metadata (for both YUI 2 and YUI 3); however, the application is generic and can be extended to support your own custom JavaScript and CSS modules — whether or not they use YUI at all.

PHP Loader is simple to use:

include("loader.php");
$loader = new YAHOO_util_Loader("2.7.0");

//Configure your instance; for example, you can turn off rollups
$loader->allowRollups = false;

//Specify YUI components to load
$loader->load("yahoo", "dom", "event", "tabview", "grids", "fonts", "reset");

//Output the tags (this call would most likely be placed in the document head)
$loader->tags();

The above PHP script would output the following to the page:

<link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/2.7.0/build/fonts/fonts-min.css" />
<link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/2.7.0/build/grids/grids-min.css" />
<link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/2.7.0/build/reset/reset-min.css" />
<link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/2.7.0/build/tabview/assets/skins/sam/tabview.css" />
<script type="text/javascript" src="http://yui.yahooapis.com/2.7.0/build/yahoo/yahoo-min.js"></script>
<script type="text/javascript" src="http://yui.yahooapis.com/2.7.0/build/dom/dom-min.js"></script>
<script type="text/javascript" src="http://yui.yahooapis.com/2.7.0/build/event/event-min.js"></script>
<script type="text/javascript" src="http://yui.yahooapis.com/2.7.0/build/element/element-min.js"></script>
<script type="text/javascript" src="http://yui.yahooapis.com/2.7.0/build/tabview/tabview-min.js"></script>
*/

Leveraging the combo-handler on Yahoo!’s servers, you can flip the combine setting on…

$loader->combine = true;

…and end up with just a single HTTP request for CSS and one more for JavaScript:

<link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/combo?2.7.0/build/reset-fonts-grids/reset-fonts-grids.css&2.7.0/build/tabview/assets/skins/sam/tabview.css"> 

<script type="text/javascript" src="http://yui.yahooapis.com/combo?2.7.0/build/yahoo-dom-event/yahoo-dom-event.js&2.7.0/build/element/element-min.js&2.7.0/build/tabview/tabview-min.js"></script>

YUI PHP Loader links:

Welcoming a New YUI Contributor: Chad Auld

Chad Auld of the MiaCMS projectChad Auld has driven the release of PHP Loader, and big thanks are owed to him for taking ownership of this application and adding a series of fantastic features as he prepped it for this beta release. You may know Chad from his work on MiaCMS and the Sideline AIR application for Twitter search. We’ve been looking for an opportunity to collaborate with him for awhile, and we couldn’t be happier to have that happening on this project.

11 Comments

  1. No way. I’m minutes away from starting a project that this PHP Loader is absolutely perfect for.

    You should make a note somewhere that the script() and css() methods allow you to output the tag sets/combos in separate areas of your markup – I was thinking that I couldn’t use this at first until I actually downloaded it and looked at the source.

  2. Nice post. I’ll need to take a closer look.

    Btw, another PHP bundler for JavaScript and CSS files is here: http://www.subchild.com/2008/08/07/simple-javascript-and-css-file-bundler/

  3. Is there an equivalent library for java front-end?

  4. Josh:

    Is it as simple as

    $loader->script();
    $loader->css();

    ?

  5. Phaniraju,

    Not yet. We’d love for someone to fork the project on GitHub and build a Java version (http://github.com/yui/phploader ) — and ASP, etc.

    -Eric

  6. Geoff,

    Yep, it’s that simple.

  7. David M Patterson said:
    September 16, 2009 at 1:58 pm

    This rocks.
    I’ve been waiting for this one.
    Thanks for another great feature!

  8. Sounds interesting. Have not though tried the PHP Loader yet.

    Does it provide the option to output ccs-files before and js-files before ? (for best performance)

  9. [...] been a busy month here with YUI 2.8.0, YUI PHP Loader 1.0.0 beta 1, and YUI 3.0.0 all hitting the wires — and with our first public YUI developer conference, [...]

  10. [...] Auld (@chadauld) is a Yahoo! engineer best known for his work on MiaCMS and Sideline. Chad recently drove the open-source release of YUI’s PHP Loader (taking over from original developer Adam Moore [@admo]), and he came to [...]