Commits

dp wiz committed 8a4dda8

Use template context. Add some helpers. Update to bs2.0.2, remove clunky bits.

Comments (0)

Files changed (1)

Layout/Bootstrap/Starter.hs

 import Data.Monoid (mempty)
 import Control.Monad (msum)
 
+import Data.Maybe (fromMaybe)
+import qualified Data.Text as T
+import qualified Data.HashMap as HM
+
 import Text.Blaze.Html5
 import qualified Text.Blaze.Html5 as H
 import Text.Blaze.Html5.Attributes
 import qualified Text.Blaze.Html5.Attributes as A
 
-template :: String -> Html -> Html
-template templateTitle templateBody = do
-    docType
-    html ! lang "en" $ do
-        H.head $ do
-            meta ! charset "utf-8"
-            H.title $ toHtml templateTitle
-            meta ! name "description" ! content ""
-            meta ! name "author" ! content ""
+type Context = HM.Map T.Text T.Text
 
-            --  Le HTML5 shim, for IE6-8 support of HTML elements
-            -- [if lt IE 9]>
-            --       <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
-            --     <![endif]
+template :: Context -> Html -> Html
+template context templateBody =
+    let ctx key = fromMaybe "" $ HM.lookup key context
 
-            --  Le styles
-            link ! href "../assets/css/bootstrap.css" ! rel "stylesheet"
-            H.style "body {\n        padding-top: 60px; /* 60px to make the container go all the way to the bottom of the topbar */\n      }"
-            link ! href "../assets/css/bootstrap-responsive.css" ! rel "stylesheet"
+        static kind path = toValue $ T.concat [ctx "STATIC_URL", kind, "/", path]
 
-            --  Le fav and touch icons
-            link ! rel "shortcut icon" ! href "images/favicon.ico"
-            link ! rel "apple-touch-icon" ! href "images/apple-touch-icon.png"
-            link ! rel "apple-touch-icon" ! sizes "72x72" ! href "images/apple-touch-icon-72x72.png"
-            link ! rel "apple-touch-icon" ! sizes "114x114" ! href "images/apple-touch-icon-114x114.png"
+        css key = static "css" key
+        img = static "img"
+        script srcURL = H.script ! src (static "js" srcURL) $ mempty
 
-        body $ do
-            H.div ! class_ "navbar navbar-fixed-top" $ H.div ! class_ "navbar-inner" $ H.div ! class_ "container" $ do
-                a ! class_ "btn btn-navbar" $ do
-                    H.span ! class_ "icon-bar" $ mempty
-                    H.span ! class_ "icon-bar" $ mempty
-                    H.span ! class_ "icon-bar" $ mempty
+    in do
+        docType
+        html ! lang "en" $ do
+            H.head $ do
+                meta ! charset "utf-8"
+                H.title $ toHtml $ ctx "title"
+                meta ! name "description" ! content ""
+                meta ! name "author" ! content ""
 
-                a ! class_ "brand" ! href "/" $ toHtml templateTitle
+                --  Le HTML5 shim, for IE6-8 support of HTML elements
+                -- [if lt IE 9]>
+                --       <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
+                --     <![endif]
 
-                H.div ! class_ "nav-collapse" $ ul ! class_ "nav" $ do
---                    li ! class_ "active" $ a ! href "/" $ "Home"
-                    li $ a ! href "/api" $ "API"
-                    li $ a ! href "/contact" $ "Contact"
+                --  Le styles
+                link ! href (css "bootstrap.min.css") ! rel "stylesheet"
+                H.style "body { padding-top: 60px; }"
 
-            H.div ! class_ "container" $ templateBody
+                --  Le fav and touch icons
+                link ! rel "shortcut icon" ! href (img "favicon.ico")
+                link ! rel "apple-touch-icon" ! href (img "/apple-touch-icon.png")
+                link ! rel "apple-touch-icn" ! sizes "72x72" ! href (img "apple-touch-icon-72x72.png")
+                link ! rel "apple-touch-icon" ! sizes "114x114" ! href (img "apple-touch-icon-114x114.png")
 
-            --  Le javascript
-            --  ==========================================================
-            --  Placed at the end of the document so the pages load faster
-            mapM_ asset ([ "jquery.js" ] ++ extraBootstrapModules)
+            body $ do
+                H.div ! class_ "navbar navbar-fixed-top" $ H.div ! class_ "navbar-inner" $ H.div ! class_ "container" $ do
+                    a ! class_ "btn btn-navbar" $ do
+                        H.span ! class_ "icon-bar" $ mempty
+                        H.span ! class_ "icon-bar" $ mempty
+                        H.span ! class_ "icon-bar" $ mempty
 
-asset :: String -> Html
-asset srcURL = script ! src (toValue $ assetPrefix srcURL) $ mempty
+                    a ! class_ "brand" ! href "/" $ toHtml (ctx "title")
 
-assetPrefix :: String -> String
-assetPrefix (path :: String) =  "/assets/js/" ++ path
+                    H.div ! class_ "nav-collapse" $ ul ! class_ "nav" $ do
+                        li $ a ! href "/api" $ "API"
+                        li $ a ! href "/contact" $ "Contact"
 
-extraBootstrapModules = [
-    "bootstrap-transition.js",
-    "bootstrap-alert.js",
-    "bootstrap-modal.js",
-    "bootstrap-dropdown.js",
-    "bootstrap-scrollspy.js",
-    "bootstrap-tab.js",
-    "bootstrap-tooltip.js",
-    "bootstrap-popover.js",
-    "bootstrap-button.js",
-    "bootstrap-collapse.js",
-    "bootstrap-carousel.js",
-    "bootstrap-typeahead.js" ]
+                H.div ! class_ "container" $ templateBody
+
+                --  Le javascript
+                --  ==========================================================
+                --  Placed at the end of the document so the pages load faster
+                mapM_ script [ "jquery.js", "bootstrap.js" ]