YUI 2.8: Learning the Library
上QQ阅读APP看书,第一时间看更新

Installing the YUI

The YUI is not an application in its own right, and it doesn't need to be installed as such. Getting started with the YUI is extremely simple; you first choose whether to download all of the source files from Yahoo! and use them locally as part of your websites' hierarchy, or whether to use the URLs provided on the YUI developer pages to reference the library files stored on Yahoo's web servers.

These are the exact same files that are used in many different interface implementations across the Yahoo! websites and as such can be depended on for being almost continuously available.

Another benefit of using Yahoo's or Google's CDN is that their networks are global in nature, with servers running in many geographically distinct parts of the world. Being able to serve library files from a location closer to your visitors' location results in a better response from your application; this is good news for your visitors and therefore good news for your business.

Additionally, as I mentioned earlier, there are different versions of each of the working files in the library including a "minified" file that has been stripped of whitespace and comment blocks. The Yahoo! servers provide these minified versions of the files, but in addition, they also serve the files in a GZIP format, making the files up to 90% smaller and therefore, much more efficient for transportation across the Internet. Finally, Yahoo! also helps the cache hit rates by issuing expires headers with expiration dates set far in the future. But best of all, these benefits are all provided for free.

If you've decided that you want to download the YUI in its entirety, you'll find a link on the YUI home page at

So as far as installing the library goes, the most that you'll need to do is to download the library to your computer and unpack it to a local directory where you can easily find the files, assets, and resources that you require for your current project, and if you choose to let Yahoo! host the files for you, you won't even need to do that.

Creating an offline library repository

All the examples in this book fetch the library components from the Yahoo servers. If you are unable to access those you may create your own repository. So you will need to create a new folder on your hard drive called yuisite. This folder is where all of our examples will reside.

Just as some of the examples included in the downloaded files do, we will use PHP for server-side scripting, thus this folder needs to be created somewhere under your web server root and where PHP scripts can be executed. It is worth mentioning that YUI is not tied to PHP in any way. Every sample PHP script in this book as well as any provided with the examples in the YUI library download can be replaced by equivalent files in any other server platform. We use PHP simply because it is popular, simple and, most important, free, but any other server platform would do just as well.

Inside this folder, create another new folder called yui. When you unpack the library, you will see a folder called build inside it. You will need to copy and paste the entire build directory into the yui folder that you have just created. You may, if you wish, copy the whole library to the yui folder, but our examples only require the build directory.

To use the examples with this offline repository, the code for the examples needs to be changed. All the <script> and <style> tags that point to the Yahoo! servers need to be changed to point to the location of the offline repository, whichever it might be. While the examples in the code bundle point to the Yahoo servers, the printed version of them might point to this off-line repository just to keep the URLs short.

It is important that this structure is correct; otherwise none of the examples that we create as we progress through this book will work. A common indicator that library files are not accessible is a JavaScript error message stating that YAHOO is undefined.