08.08.08

$(), python/cheetah and javascript/prototype

Posted in Web authoring, javascript, python at 10:06 am by karl

This caught me out this morning. Cheetah uses $var, ${var}, $(var) or $[var] as a placeholder for the variable var. Prototype uses $("id") as a turbo-charged replacement for document.getElementById("id") . This can cause problems with inline javascript in cheetah templates, as cheetah will try to substitute the $("elementId") in this code:

<script language="javascript" type="text/javascript">
var el = $("elementId")
</script>

The solution I’m using at the moment is to rewrite the above as:

<script language="javascript" type="text/javascript">
var el = $ ("elementId")
</script>

Note the space between the $ and the (”elementId”). Cheetah ignores $s that are followed by whitespace and javascript pretty much ignores all whitespace., so it seems to work.

Leave a Comment

You must be logged in to post a comment.