![]() |
Xataface
2.0alpha2
Xataface Application Framework
|
There are two ways to include custom CSS files with your module and its actions:
Using the Dataface_Application::addHeadContent() has the benefit of loading your CSS file at the beginning of the page load rather than at the end of it. Therefore your styles will be picked up immediately.
The Dataface_CSSTool is closely integrated with Dataface_JavascriptTool so if your CSS file is related to a javascript file and needs to be bundled with it, then it may be easier to reference the file using the require-css declaration directly from a javascript file (and this will use the Dataface_CSSTool class).
Another benefit of using the Dataface_CSSTool is that it allows you to bundle multiple small CSS files together at run-time in a network-friendly compressed format. This might allow you to organize your CSS files more logically and make it easier to maintain them moving forward (because it decouples the production format from the development format).
For this tutorial we are going to use the Dataface_CSSTool to include a style-sheet with our hello_world action. We are just going to create some simple styles to demonstrate the steps involved in loading a custom CSS file.
We begin by creating a css directory inside our module's directory. It will be located at:
Further we're going to add a directory structure underneath this css directory as a namespacing mechanism (so that we know that our CSS paths as referenced by the CSS tool will be unique. We'll use the same convention as we used for our javascript files and templates:
Now that we have our directory structure let's create a CSS file at
With the following contents:
So all we're doing here is changing the color of the "world" part of "Hello World" so that it is red.
css Directory with the CSS ToolIn our hello_world action we now need to register our css directory. We do this with the following code:
The Dataface_CSSTool::addPath() method takes 2 parameters:
css directorycss directoryNow that we have added the css directory to the paths in the CSS Tool, we just need to import the CSS File. We will do this using the require-css javascript directive at the beginning of the hello_world.js javascript file:
Our hello_world action now looks like:
And our hello_world.js script now looks like:
Point your web browser to the hello_world action again and it should look like:
Notice that the word "world" is now in red.
1.8.1.2