-
Notifications
You must be signed in to change notification settings - Fork 48
-
Notifications
You must be signed in to change notification settings - Fork 48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to avoid toValue and toHtml? #88
Comments
I implemented the following TH code that does the job for me and is type-safe: {-# LANGUAGE TemplateHaskell #-}
module BlazeTH where
import Language.Haskell.TH
import Language.Haskell.TH.Lib
import Text.Blaze
import Text.Blaze.Internal
dash :: Q Exp -> Q Type -> [Name] -> Q [Dec]
dash f t = mapM $ \n -> funD (mkName $ nameBase n ++ "'")
[clause [varP x] (normalB
[| $(varE n) $ $f $(varE x) :: $t |]) []]
where x = mkName "x"
dashAttr = dash [|toValue|] [t|Attribute|]
dashTag = dash [|toMarkup|] [t|Markup|] Usage dashAttr ['href,'A.id,'A.style]
dashTag ['a,'b,'h1]
myLink url name = a' ! href' url $ name This solution needs #89 |
I'm not sure if avoiding using |
For me using EDIT: Aeson actually puts |
I was surprised by this just now, playing with Blaze for the first time. It felt like boilerplate to me too, as I thought Blaze would be merrily converting my types to attributes as appropriate, like with the literal String. Specifically I wanted to see how web-routes would integrate with Blaze, and thought I would just throw a route in the Blaze e.g. |
There is an easy way to get rid of it in your templates. Just define your
|
johannesgerer already shows my prior expectations in a technical way. I figured href would have the type signature that he details for href'. Why should String have all the easy living fun? It seemed natural that AttributeValue conversions are represented by toValue implementations, so I'd look to do that in a workaround module. But that module wouldn't be application specific, only my ToValue instances would be, and it raises the question as to why not provide this in Blaze itself. So I was throwing in a solidarity comment. Now that I know to use toValue and toHtml, then sure, I'd like to save some typing now. Perhaps I'll even grow to love those twins of typing. But to me as a first time user, my toX-less blaze already looked sweet (expressing everything necessary); it's this user experience that I wished to (overly by now) convey. |
The problem with attribute values is that they have wildly differing structures and acceptable values. |
How can I avaoid having to use toValue/toHtml so often. It comes up basically in any template, that has parameters. As an example take:
What do you think about providingth following dashed version of the tags (either manually or with e.g. Template Haskell):
They even work with literate strings, if OverloadedString is swithed off (as String is in ToMarkup and ToValue).
What do you think about that? Should I send a pull request?
The text was updated successfully, but these errors were encountered: