Skip to content
Daniel Spors edited this page Nov 4, 2020 · 1 revision

WDF is great in resource management. By resources we mean assets like images, javascript, css, less and stuff. In fact, you may define a complete folder to be a resource-folder and even define for which type of resources it shall be used.

There are two basic differences in how resources are handled:

  • direct delivery
  • processed delivery

In direct delivery WDF will create an URL for the resource that will be used by the browser. In processed delivery, the resource identifier is passed to the WdfResource class which then processes it. The first is blazing fast, the latter is useful when the resource folder is not located in the docroot.

To define a resource folder just tell WDF about it:

add_resource_dir('</path/to/folder>','<relative URL part>');
prepend_resource_dir('</path/to/folder>','<relative URL part>');
add_wdfresource_dir('</path/to/folder>');
prepend_wdfresource_dir('</path/to/folder>');

See the function definitions for details about their parameters. Once the resource folders are defined you can access any resources URL with it's relative name like this: resFile('icons/favicon.ico').

Automatic resources

All this said, there may be no need to handle resources manually. WDF automatically adds resources to the HTML output, that follow the naming scheme: <classname>.(js|css|less). In other words: When your Controller is named 'Home' and your resource folder is 'res' you may just create a file named res/home.js and place your JS logic in there. It will automatically be loaded. Same for res/home.css and best: res/home.less because WDF contains a LESS preocessor.

The resource loader moves up the inheritance tree, so you may also add a file res/controllerbase.less with all global definitions, that will be loaded when a subclass of ControllerBase is rendered (see templating for details about this).

Clone this wiki locally