Friday, April 09, 2004

For the last few weeks I've been working on the intranet of the DNPM here in Brasilia.

And one of the things that really strikes me is how much I enjoy
web-style programming. Which is kind of a surprise to me as I'd got so out of sympathy with the environment in my last job, and lost so much confidence, that I was wondering whether I ever wanted to, or could work on web-sites again. But, now, I'm having what, in day-job terms, you could call a damned good time.

And one of the things that is making me very happy is applying a little pattern that I'm fond of, to several of the pages. It's trivial, but I feel so good about using it, I'm going to share it with you.

Say I have a page which pulls out a query from the database, selected by a couple of criteria, such as ''year'' and ''district''. I now want to add a navigation control that can take you forward to the next year, or backwards to the previous one. But I want to keep the other parameters for this page the same.

I use a standard routine to get my variables. As the DNPM is hostage to Microsoft, the site uses ASP (which wouldn't be my first choice, clearly ;-) and the code is in Visual Basic, but you can probably imagine the equivalent in your scripting language of choice ...


function getVar (varName, default)
dim x
if request(varName) <> "" then
x = request(varName)
session(varName) = x
elseif
session(varName) <> "" then
x = session(varName)
else
x = default
end if
getVar = x
end function


If you aren't familiar with VB notation, the only confusing thing is likely to be the "getVar = x" at the end, which is basically "return x".

What this does is looks on the request for something, if it finds it, returns it, and puts it on the session. If it doesn't find it, looks for it on the session, and if it doesn't find it there, returns the default.

I tend to server-side include this routine as part of a standard library. After which I can just say

 year = getVar("year", year(date)) 


To instantiate the local variable year with a year either from the request (highest priority), session (next priority) or current date (lowest priority)

What I then do, should be fairly obvious. To have a link to the previous and next year, I simply add :

[ <a href="./query.asp?year=<%=year-1%>"><%=year-1%></a> |

<a href="./query.asp?year=<%=year+1%>"><%=year+1%></a> ]


Like I say, it's kind of trivially simple. But it has a profile that appeals to me :

* it's a ''very'' simple thing (literally one line of extra HTML added to a page if you're already using the getVar routine)

* it has a big effect. It starts to turn a rather static query page into a movable ''window'' which you can slide across the data-space, making the site feel more interactive and ''flow'' better.

You can add navigation across further dimensions. It's not quite so simple if your dimension isn't a numerical sequence. But you only need to set up a function to map numbers to names, eg. :


function nextStateName(i)
dim a
a = Array("Rio Grande do Sul", "Santa Caterina", "Parana", "Sao Paulo", "Rio do Janiero") ' etc
i = i + 1
if i > ubound(a) then
i = lbound(a)
end if
nextState = a(i)
end function



then set up a counter and do something like this


<a href="./query.asp?state=<%=state+1%>"><%=nextStateName(stateNum)%></a>


Like I say, I'm in love with web programming again. :-)

No comments: