Blog posts from July 2009...

Testing for undefined variables in JavaScript

There are all manner of tests for undefined in JavaScript it seems. if typeof(foo) == 'undefined' or foo === undefined. So when should you use which?

The following shell example explains:


deryck@domino:~$ js
js> foo === undefined;
typein:10: ReferenceError: foo is not defined
js> typeof(foo) == 'undefined';
true
js> var foo;
js> foo === undefined;
true
  

In browser, there is also the window.foo === undefined construction, instead of typeof(foo), but the latter seems clearer in code to me.

Link | Posted by deryck on July 23, 2009 | 0 comments

Launchpad Available As Free Software

Today Launchpad source code has been made available as free/open source software under the GNU AGPLv3. This is a very exciting day for those of us working on Launchpad. I am quite proud that I was actually awake last night when the announcement appeared.

I noticed Jono Lange changed the topic in #launchpad on irc.freenode.net somewhere near midnight my local time. Almost immediately after, I found Karl Fogel's email to our Launchpad list announcing the open sourcing. I'm generally in bed that late; however, I was trying to get a merge proposal submitted before I went to bed, and then, I had some hacking to do on a personal project I'm working on. I am glad circumstances contributed to my being awake when the moment of open sourcing came.

I would really encourage any free/open source developer to look closely at Launchpad for project hosting (or just code hosting, bug tracking, mailing lists, etc. or any combination thereof). The code is freely available, and you'll find a group of developers at Canonical who are excited about seeing Launchpad succeed as both a great collaboration platform and a free software project itself.

Link | Posted by deryck on July 21, 2009 | 0 comments

An if-substring-in-string Django Template Construction

Here's a quick tip for Django template hackers. It's a known fact of Django templates that the syntax is purposefully limited. I've been living with the need for an if-substring-in-string construction. Of course, I could write a custom template tag, but work is quite busy. So on a whim and a 10 minute break I tried this yesterday, and it worked well for me. Take a look and let me know what you think.

First, the problem.

Bookmark Formatting

I have a custom app for pulling in my Delicious bookmarks and formatting them as link posts here on this site. I include the HTML I want in the original bookmark and have a bit of template code to insert preview images generated by ShrinkTheWeb. When I link a YouTube video, I do a quick cut-n-paste of the video to embed the player, and in these cases, I don't want to include the preview image. In Python, this would be super simple:


if 'http://www.youtube.com/' not in link_url:
    show_thumb()

However, Django templates don't have a similiar syntax.

A Solution Using {% with %}

So here's what I did:


{% with link_url|slice:":23" as short_url %}
  {% ifnotequal short_url "http://www.youtube.com/" %}
    My ShrinkTheWeb image goes here.
  {% endifnotequal %}
    Other HTML common to all links goes here.
{% endwith %}

What do others think? A decent solution? There are some issues; if I want to use multiple video sites, for example. At that point, I would be better to use link categories or a custom tag. But for a 10 minute fix, I think it's quite nice.

Link | Posted by deryck on July 3, 2009 | 4 comments