on master help sign in
* updated elm-yesod for URL interpolation
3d4bbebVincent Ambo
elm-yesod/Language/Elm/Yesod.hs+28−14
@@ -1,4 +1,4 @@
1−{-# LANGUAGE QuasiQuotes #-}
1+{-# LANGUAGE QuasiQuotes, TypeSynonymInstances #-}
2{- | This module provides a function for compiling Elm source code into a Yesod widget.
2{- | This module provides a function for compiling Elm source code into a Yesod widget.
3 In order to use this with your Yesod app, you need to define a defaultLayout like
3 In order to use this with your Yesod app, you need to define a defaultLayout like
4 function that embeds the elm-min.js file /in the <head> tag/.
4 function that embeds the elm-min.js file /in the <head> tag/.
@@ -15,32 +15,46 @@
15
15
16 A full example implementation is provided in the examples folder of the Elm github repository.
16 A full example implementation is provided in the examples folder of the Elm github repository.
17-}
17-}
18−module Language.Elm.Yesod (elmWidget) where
18+module Language.Elm.Yesod (
19+ elmWidget
20+ , ElmUrl) where
19
21
22+import Control.Monad (liftM)
20import Text.Blaze (preEscapedToMarkup)
23import Text.Blaze (preEscapedToMarkup)
21import Text.Julius
24import Text.Julius
22−import Yesod.Widget (toWidgetHead, toWidgetBody, GWidget)
25+import Yesod.Core
26+import Yesod.Handler
27+import Yesod.Widget
23import Language.Elm
28import Language.Elm
24import Language.Elm.Quasi
29import Language.Elm.Quasi
25
30
26import qualified Data.Text as TS
31import qualified Data.Text as TS
27import qualified Data.Text.Lazy as TL
32import qualified Data.Text.Lazy as TL
28
33
29−-- |toWidget takes some Elm code in String format and produces a widget. Usage example:
34+instance ElmSource (ElmUrl url) where
30−--
35+ toHtml = undefined
31−-- > toWidget [elmFile|elm-source/somePage.elm|]
36+ toParts = undefined
32−elmWidget :: ElmSource a
37+
38+
33− => a -- ^ The Elm source code
39+elmWidget :: ElmUrl (Route master) -- ^ Elm source code
34− -> GWidget sub master ()
40+ -> GHandler sub master (GWidget sub master())
35−elmWidget source =
41+elmWidget source = do
42+ urlF <- getUrlRenderParams
43+ return $ mkElmWidget source urlF
44+
45+
46+mkElmWidget :: ElmUrl (Route master) -- ^ Elm source code
47+ -> RenderFn (Route master) -- ^ URL rendering function
48+ -> GWidget sub master ()
49+mkElmWidget source urlFn =
36− let (html, css, js) = toParts source in
50+ let (html, css, js) = toParts (urlFn, source) in
37 do toWidgetHead css
51 do toWidgetHead css
38 toWidgetHead [julius| #{js} |]
52 toWidgetHead [julius| #{js} |]
39 toWidgetBody html
53 toWidgetBody html
40
54
55+
41-- | Return type of template-reading functions.
56-- | Return type of template-reading functions.
42type ElmUrl url = (url -> [(TS.Text, TS.Text)] -> TS.Text) -> Elm
57type ElmUrl url = (url -> [(TS.Text, TS.Text)] -> TS.Text) -> Elm
43
58
44−-- | render with route interpolation
59+-- | Readability synonym for render function
45−renderElmUrl :: (url -> [(TS.Text, TS.Text)] -> TS.Text) -> ElmUrl url -> TL.Text
60+type RenderFn url = (url -> [(TS.Text, TS.Text)] -> TS.Text)
46−renderElmUrl r s = renderElm $ s r
elm/src/Language/Elm.hs+8−2
@@ -43,9 +43,15 @@ instance ElmSource TL.Text where
43 toParts = toPartsHelper . TL.unpack
43 toParts = toPartsHelper . TL.unpack
44 toHtml elmL title = generateHtml elmL title . TL.unpack
44 toHtml elmL title = generateHtml elmL title . TL.unpack
45
45
46+-- | (urlRenderFn, urlRenderFn -> Elm)
47+instance ElmSource (t, t -> Elm) where
48+ toParts (f, s) = toPartsHelper $ TL.unpack $ renderElm $ s f
49+ toHtml elmL title (f, s) = generateHtml elmL title $ TL.unpack $ renderElm $ s f
50+
51+-- | to be used without URL interpolation
46instance ElmSource (t -> Elm) where
52instance ElmSource (t -> Elm) where
47− toParts s = toPartsHelper $ TL.unpack $ renderElm $ s undefined
53+ toParts s = toPartsHelper $ TL.unpack $ renderElm $ s undefined
48− toHtml elmL title s = generateHtml elmL title $ TL.unpack $ renderElm $ s undefined
54+ toHtml l t s = generateHtml l t $ TL.unpack $ renderElm $ s undefined
49
55
50
56
51-- build helper to avoid boilerplate repetition
57-- build helper to avoid boilerplate repetition