このような簡単な例を考えると
glyphicon :: Text -> Widget
glyphicon name = toWidget [hamlet|<span class="glyphicon glyphicon-#{name}">|]
foo :: ToMarkup a => a -> Widget
foo content = toWidget [hamlet|<div class="foo">#{content}</div>|]
Yesod に と の両方を実行できる組み込みのメカニズムはfoo "some text"
ありfoo (glyphicon "pencil")
ますか? Text と Widget の両方を Widget に変換するカスタム型クラスを使用して、これを回避することができました
class MakeWidget a where
makeWidget :: a -> Widget
instance MakeWidget Widget where
makeWidget = id
instance MakeWidget Text where
makeWidget x = toWidget [hamlet|#{x}|]
glyphicon :: Text -> Widget
glyphicon name = toWidget [hamlet|<span class="glyphicon glyphicon-#{name}">|]
foo :: MakeWidget a => a -> Widget
foo content = toWidget [whamlet|<div class="foo">^{makeWidget content}</div>|]
^{foo "hello"}
特に、あいまいな型のために実行することさえできず、^{foo (T.pack "hello")}
代わりに実行する必要があるためです。
Text
両方とWidget
別の内部を埋め込むより良い方法はありWidget
ますか?