With the recent publication of the Digital Britain report and Gordon Brown calling on Tim Berners-Lee to help open up access to government data, there’s been a lot of talk about public organisations opening up data.
Most of this has focused around APIs and linked data (something I’m very passionate about already), but there’s much simpler ways to get data out there that might be useful to those people who might have a website, know their way a bit round HTML, but aren’t willing to get their hands dirty with XML, JSON or RDF.
I’m talking of course, about widgets, these are little bits of (often Javascript) code that people can copy and paste into their own websites. These can then be used by local blogs, newspapers or other websites to publish your content in a quick and easy way.
I’ve had a go at doing a few widgets, publishing food safety inspections and planning applications as widgets and I’m looking to do more in the future. Fellow blogger and techy Philip John has even turned one of our widgets into a WordPress-compatible widget, which I’m proudly sporting on the right here.
Essentially, what you’re doing is pulling information from a database (using PHP or whatever server-side technology you fancy) and outputting it as javascript code, wrapping it all in a document.write function. You can also use inline styling to style your widget, making sure you override any potential styles that might be overridden by the user’s stylesheet. A quick and dirty example is below:
<?php
$query = "SELECT * FROM database";
$result = mysql_query($query) or die(mysql_error());
echo "document.write('";
echo "<h2 style=\"font-size: 18px; font-weight: bold;\">Planning Applications for Foo on Bar Council</h2>";
while ($row = mysql_fetch_array($result)) {
echo "<p><a href=\"".$row['link']."\">".$row['address']."</a>";
echo "<p>".$row['proposal']."</p>";
}
echo "');";
?>
Note: It’s very important that you make sure you get rid of all line breaks when you actually publish your content, otherwise you’ll get a Javascript error and the content won’t show up.
Once you’ve done this, you can then upload the script and then call it in a script tag, like below:
<script type="text/javascript" src="http://www.mysite.com/myscript.php"></script>
And here’s one I made earlier!:
Really, the only limit is your imagination, so get out there and widgetise! It’s not a replacement for APIs, but it’s a good alternative for folks who might not be comfortable with the big stuff.
(P.S. The John Smith’s is there because it’s got a widget in the can, I’m not advocating the drinking of John Smith’s – it’s horrible stuff. Give me a proper cask ale any day)