Table of content
- 1. Abstract
- 2. License
- 3. Methods for presenting structured web pages
- 4. The idea behind Diable
- 5. Basis
- 6. Tutorial and examples
- 7. Limitations
- 8. TODO
- 9. Help, feedback, suggestions
- 10. Download
- 11. Links
1. Abstract
Diable stands for « Diable Is A Boxed Layout Engine. » (yes, this is Unix style definition...)
This is a small PHP script which purpose is to help creating web pages. It produces a bit of Javascript, specifically designed to dynamically compute the position of the components of your page. The purpose of this software is to easily compose web pages, using PHP, HTML and a bit of javascript. Currently there is two main solutions to organize data with HTML : tables and divs. However, as far as I can see, none of these thow methods is fully satisfying. In the following paragraphs I will explain why tables and divs are restrictive methods, and what might be a better solution. Diable try to implement this solution, which is explained in details in the next paragraph.
2. License
Diable is licensed under the GPL Version 2. You are free to use, modify and redistribute this software, provided that you follow the terms of this license. Read it please, GPL is not free source, it is open source.
3. Methods for presenting structured web pages
a. Tables
The main interest of tables is to allow a very structured page composition. With rows, cells and various tricks like colspan and rowspan it is
possible to define parts like a top banner, a left menu, a footer, etc. With the use of variable dimensions like <table width="100%"
height="100%">, it is even possible to have the document automatically fit the size of the browser.
However working with tables leads inevitably to some headache. Either the result varies between browsers, or it gradually becomes very hard to
change the layout of the document, since moving a single element often implies to review the entire structure. If you ever try to create some
reasonnably complex page and make it evolve, then you surely see what I mean...
But after all, in my opinion, the biggest problem is that the content is tightly linked to the container. It is hard to split the information and the visual appearance. CSS lets you modify fonts, colors and other various things, but not the global arrangement of the document.
b. Divs
Divs are somewhat easier to use than tables. With style sheets one can specify for each div element every visual parameter, including size and position. It is easy to separate content and disposition, but this time the document is most of the time very static. Each block position is carefully computed with a fixed screen size, no matter what the size of the browser window is. Everything is frozen and it is often frustrating to see those big margins at the left and right of the window, and to be forced to scroll up and down endlessly. Actually this is the most common web page layout. And although moving an element is simpler than with tables, it means however to recompute manually each div position. Liquid design can be achieved with divs but, here too, it rapidly becomes very hard to maintain.
c. The best of two worlds
A better method would use the best of tables and divs :
- Structured composition : no complex thing without a minimum of structuration
- Easy to modify : I don't want to recreate everything each time I change a comma
- Fits the window, and automatically resize when the window is resized : because this is the natural way
- Separation of content and presentation : "theming" is nice
Ok, I recognize that it is utopia ! But as I am rather utopian I started to think at an alternative. It is called Diable and explained in the next paragraph.
4. The idea behind Diable
Diable is largely inspired from the Qt library (Trolltech) layout mechanisms. Do you know the Qt UI designer ? In a few words this tool is designed to visually create GUI, as Visual Basic (but far better of course). With Qt Designer you just have to put your buttons, combo-box, text field, etc. on a form, and group them in layouts. The Qt library will compute the position and size of each object for you, depending on simple rules. These rules are of the form « this text field will grow as long as it has space. », or « this button has a fixed size of 60 pixels. ». It's magic, intuitive and simple. If you don't know Qt yet I suggest you to have a look at it, it is worth spending some time on it. See links. (other layout systems exist but I am tired to have to use a compass to dispose my components...)
It would be really cool to dispose of such a tool when creating web pages. It is so natural that I wonder why it is not yet used everywhere. After googling a bit I wasn't able to find anything that looks like that (maybe I didn't search enough ?). So I began to code something with the following rules in mind :
- liquid : this is the first and primary goal
- ease of use : I don't want to re-invent the wheel, and especially a square wheel. So if the result is harder to use than the existing tools then it's bad.
- cross browsers : utopia again, but eh, you won't change me.
- precision : I wan't to be able to position objects precisely, with pixel precision
How to implement this ? In my opinion there is not many ways. If the layout of the page should be computed when the user resizes the window, either the web browser must manage this event, or we must manage it ourself. As the browser is not able to do that, we are going to do it ourself. Furthermore computation shall occur on the client side. It is out of the question to reload the document each time the user moves the mouse. So if it is client side I don't see anything else than javascript. But the less javascript I use, the better I am. So I decided to use PHP on the other end of the pipe, on the server side, to realize processing that could be done elsewhere from javascript.
On the server side we build our pages, with the help of some PHP functions, provided by Diable. These functions will later produce some simple javascript code, designed to manipulate position and dimension of div elements, on the client side.
5. Basis
This paragraph explains the basis needed to use Diable. You may also follow the tutorial presented below to get some examples of what is explained here.
a. Overall mechanism
With Diable the page is composed of non overlapping boxes. Each box can contain other boxes and the box containing all other boxes is the page. We define the width or the height of each box with three possible values :
- fixed size in pixels : this box will always have the same width or height, regardless of its content

- minimum : the box will be compressed so that it does not occupy more place that needed by its content (the sum of the minimum size of all its inner boxes)

- minimum expanding : as minimum, but the box will grow if there is room for growing

Several boxes can be grouped together in larger boxes :
- horizontally : the boxes are laid on a single horizontal line

- vertically : the boxes are laid on a single vertical line

- on a grid : the boxes are laid in a grid, with eventually empty cells

Finally we define the last type of box : the div box. This is the smallest unit of data presented on the screen. This type of box is used to reference
a div element in the HTML document.
Let's consider the following illustration. It shows how a rather complex web page can be viewed as a set of boxes (either horizontal, vertical,
grid or div).
These sort of composition seems rather close to the table model, but with a major difference : the structure is not expressed in the document itself. It is expressed with grouping and size rules. These rules are later used to compute the position and size of each div element.
b. PHP classes
Six small PHP classes are used to define a page layout :
- PageLayout : it defines the whole page. It is the special box that contains every other boxes.
- DivLayout : used to reference a div element on the HTML page.
- HorizontalLayout : used to align several boxes on an horizontal line, in a single box.
- VerticalLayout : used to align several boxes on a vertical line, in a single box.
- GridLayout : used to align several boxes on a grid, in a single box.
- SpacerLayout : not mentioned before, the spacer is used to fill as much free space as possible, like a spring.
The complete API of these classes will not be put here, take a look at the examples, and the PHP source file.
c. Required HTML components
First of all, your document should follow the following doctype :
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
This is mandatory, otherwise I.E. won't compute correct values.
Then your document body must catch the onLoad and onResize events. This is our javascript entry point.
<html>
In the function
<body onLoad="my_function()" onResize="my_function()">
...
</body>
<script language="javascript">
function my_function() {
...
}
</script>
</html>
my_function() we will call a special fonction whose aim is to manage the position and size of the different
boxes of the page. See examples for more.
A major difficulty with HTML, Javascript and web browsers is to determine the size of the client window. All browsers react differently and
unfortunately, determining the right window size is a mandatory condition for the proper execution of Diable.
I didn't find a common way to solve the problem on the Internet, just some scripts dealing with two or three browsers, at most. So I propose
my own, of which I am quite proud... Instead of accessing various properties of the window, and testing the name of the browser to know what
do, I let the browser display an element which size is 100%, and then I read the actual size of this element. It works the same way with all
browsers, youpi :-). So lets write it in HTML :
<html>
<body style="height: 100%; width: 100%; padding: 0px; margin: 0px;">
<div id="content" style="position:absolute; top: 0px; left: 0px; width: 100%; height:100%;"></div>
</body>
</html>
Note that these settings are required for automatic resizing, but you are free not to use width: 100%; height:100%; if you don't want the content to be resized automatically.
The example 01 below gather all these required components.
6. Tutorial and examples
- 01 - hello world !
- 02 - two horizontal boxes
- 03 - three vertical boxes
- 04 - four boxes in a grid
- 05 - spacers
- 06 - a concrete example
7. Limitations
Well... currently there are some limitations. Although Diable works well and the same way with most web browsers (konqueror, mozilla, I.E.), the following major problems are still not resolved:
- Barely alpha quality. This software is quite experimental, the API is far from being clean and fixed, and is evolving on a moving ground (HTML+javascript+browsers discrepancies).
- As long as the page is not completely loaded by the web browser, the layout is not computed. In fact the "onLoad" event is not fired until the page is completely loaded and positions are not computed. As a results all div element will appear layered on the top left of the screen, in a giant mess. Suddenly, when the onLoad event is triggered, all div elements will find their way to the right place. This could surprise a bit the reader. A solution is to put an opaque div, covering the full page and initially on the top of the layers stack, with the text "Loading, please wait..". The last step of the load script would be to hide this div, after the layout has been computed. However if the page is rather long to load the visitor will have to wait in front of a rather cold message. Note that the browser will tipically fire the onLoad event before all images are loaded, so the layout will be computed before everything is loaded, reducing the mess life time.
-
You cannot use external style sheet to describe box properties of your div elements. In other words if you want to define width, height, border,
margin and padding properties for a div element, you must define them inline, not in a
<style>block. Why ? Simply because browsers won't handle external CSS the same way. At the heart of the problem is the value of the style property, accessed from Javascript. This value won't take into account the CSS properties not defined inline, even if these properties are actually applied. In short there should be two distinct style properties on a object : style (the style specified in the document) and currentStyle (the style computed with all gathered CSS properties). Unfortunately the currentStyle is not standard and not two browsers uses it the same way. As a result it is impossible to compute the correct dimension of an object because we don't have acces to the box properties of the div.
With a scripting language it is rather easy to cope with this limitation. You can define constants containing the style properties and use these constants as you would have done with the<class>attribute. This has the advantage of gathering all styles properties in a single location. But it is only a simple workaround, since the CSS selector is much more powerfull and does not need the<class>attribute. Another drawback of inline styles is that the page is a little heavier. - It is rather hard to debug : javascript doesn't help in this task. I am rather annoyed with this point as I think it is critical for a developer to known rapidly and exactly where the problem is.
8. TODO
Lots of things, I have got plenty of idea, among these :
- Add support for inner div layout : for the moment div can not overlap. It would be cool to define divs and inner divs and then to be able to position them recursively with diable the same way we do for other elements.
- Improve the API : I am not satisfied with what I have done currently because we tend to define the page from the inner boxes to the bigger boxes, and it is not very intuitive. I am working on a sort of tree building. Furthermore, associated with inner div layout functionnality it could dramatically simplify things. I dislike the PageLayout object and I hope it won't appear in the next version.
- Manage non inline CSS box properties : currently some functions exists for reading a property in a transparent way but it works for too few browsers and especially not with the browser I use...
- Add support for other server side programming language : I like PHP but this is not always the best choice (what about CGI written in assembly ?)
- Add better doc, examples and translations : scheduled for 2053.
9. Help, feedback, suggestions
I am very interested in getting feedback. Does it works for you ? Do you think it is worth spending some kilobytes on your hard drive ? Have some ideas for improvement ? Drop me an e-mail. See home page for contact.
10. Download
- diable-0.1.tar.gz : 2006-02-12, first public version
11. Links
- Trolltech : the famous and wonderful toolkit for GUI and application development, from which the layout engine idea of Diable came from
- The CSS2 box model (W3C) : this is an essential document if you want to understand boxes
- Phototheque : built with Diable