Haskell.org
Sign In Sign Up
Manage this list Sign In Sign Up

Keyboard Shortcuts

Thread View

  • j: Next unread message
  • k: Previous unread message
  • j a: Jump to all threads
  • j l: Jump to MailingList overview
newer
Re: [web-devel] Data and Typeable

style for html IDs in hamlet

older
Overhaul of wai-handler-devel

Max Cantor

8 Apr 2011 8 Apr '11
10:03 a.m.

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

... I'm wondering is there a datatype for IDs and Classes that I can use to make the IDs and Classes directly so that:
and
would fail to typecheck? In general, are there any good, safe ways to do this instead of just unwrapping to strings? max

0 0
Reply
Sign in to reply online Use email software

Show replies by date

Greg Weber

8 Apr 8 Apr
10:39 a.m.
New subject: style for html IDs in hamlet

We could have something like class DomId a where toDomId :: a -> Text class DomClass a where toDomClass :: a -> Text That hamlet calls on id and class insertions. Michael would no better if that is possible with the hamlet internals. On Fri, Apr 8, 2011 at 7:03 AM, Max Cantor wrote:

...

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

...

I'm wondering is there a datatype for IDs and Classes that I can use to make the IDs and Classes directly so that:

and
would fail to typecheck?

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

max _______________________________________________ web-devel mailing list web-devel@haskell.org http://www.haskell.org/mailman/listinfo/web-devel

0 0
Reply
attachment
  • attachment.html
Sign in to reply online Use email software

Max Cantor

10:43 a.m.
New subject: style for html IDs in hamlet

Its not really worth changing the API, just wondering if there is a way to do it with the current API. max On Apr 8, 2011, at 10:39 PM, Greg Weber wrote:

...

We could have something like

class DomId a where toDomId :: a -> Text

class DomClass a where toDomClass :: a -> Text

That hamlet calls on id and class insertions. Michael would no better if that is possible with the hamlet internals.

On Fri, Apr 8, 2011 at 7:03 AM, Max Cantor wrote: 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

...

I'm wondering is there a datatype for IDs and Classes that I can use to make the IDs and Classes directly so that:

and
would fail to typecheck?

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

max _______________________________________________ web-devel mailing list web-devel@haskell.org http://www.haskell.org/mailman/listinfo/web-devel

0 0
Reply
Sign in to reply online Use email software

Michael Snoyman

9 Apr 9 Apr
3:04 p.m.
New subject: style for html IDs in hamlet

On Fri, Apr 8, 2011 at 5:03 PM, Max Cantor wrote:

...

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

...

I'm wondering is there a datatype for IDs and Classes that I can use to make the IDs and Classes directly so that:

and
would fail to typecheck?

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

It's entirely possible to provide separate datatypes for IDs and classes at the Hamlet level. The problem is, you will either always need to use them in place of String/Text, or we'll need to have a separate typeclass for each and triple the number of instance declarations. My opinion: it's a good idea in theory, but will be more trouble than it's worth in practice. One recommendation for your existing code: instead of calling jqVal and idVal directly, declare instances of ToHtml and ToJavascript. Michael

0 0
Reply
attachment
  • attachment.html
Sign in to reply online Use email software
5155
Age (days ago)
5156
Last active (days ago)

List overview

Download

3 comments
3 participants

Add to favorites Remove from favorites

tags

participants (3)

  • Greg Weber
  • Max Cantor
  • Michael Snoyman

HyperKitty Powered by HyperKitty version 1.3.9.