[web-devel] style for html IDs in hamlet

Max Cantor mxcantor at gmail.com
Fri Apr 8 16:03:50 CEST 2011

In an effort to get more type-checked goodness in hamlet, I started using string constants for all my dom IDs and classes.  We're not using the newIdent function because I want control over what the IDs and classes will be named. 

I wrote up a little typeclass as such:

class CustomIdentString a where jqVal :: a -> String; idVal :: a -> String

newtype CustomIdentCls = MkCls String 
instance CustomIdentString CustomIdentCls where
  jqVal (MkCls s) = "." ++ s; idVal (MkCls s) = s;

newtype CustomIdentId = MkId String 
instance CustomIdentString CustomIdentId where
  jqVal (MkId s) = "#" ++ s; idVal (MkId s) = s;

This way, I can refer to divs in javascript using:
$("#{jqVal myId}").foo..

And create the elements in hamlet using
<div ##{idVal myId}> ...

I'm wondering is there a datatype for IDs and Classes that I can use to make the IDs and Classes directly so that:
<div .#{foo myId}> and <div ##{foo myCls}> 
would fail to typecheck?  

In general, are there any good, safe ways to do this instead of just unwrapping to strings?


More information about the web-devel mailing list