widgetFile in scaffolded site

This commit is contained in:
Michael Snoyman 2010-11-19 11:37:54 +02:00
parent 92ab8ee889
commit dd693a96a4
3 changed files with 27 additions and 10 deletions

View File

@ -16,7 +16,5 @@ getRootR = do
defaultLayout $ do
h2id <- newIdent
setTitle "~project~ homepage"
addCassius $(cassiusFile "homepage")
addJulius $(juliusFile "homepage")
addWidget $(hamletFile "homepage")
addWidget $(widgetFile "homepage")

View File

@ -8,6 +8,7 @@ module Settings
( hamletFile
, cassiusFile
, juliusFile
, widgetFile
, connStr
, ConnectionPool
, withConnectionPool
@ -22,7 +23,9 @@ import qualified Text.Cassius as H
import qualified Text.Julius as H
import Language.Haskell.TH.Syntax
import Database.Persist.~upper~
import Yesod (MonadInvertIO)
import Yesod (MonadInvertIO, addWidget, addCassius, addJulius)
import Data.Monoid (mempty)
import System.Directory (doesFileExist)
-- | The base URL for your application. This will usually be different for
-- development and production. Yesod automatically constructs URLs for you,
@ -98,23 +101,39 @@ connectionCount = 10
-- used; to get the same auto-loading effect, it is recommended that you
-- use the devel server.
toHamletFile, toCassiusFile, toJuliusFile :: String -> FilePath
toHamletFile x = "hamlet/" ++ x ++ ".hamlet"
toCassiusFile x = "cassius/" ++ x ++ ".cassius"
toJuliusFile x = "julius/" ++ x ++ ".julius"
hamletFile :: FilePath -> Q Exp
hamletFile x = H.hamletFile $ "hamlet/" ++ x ++ ".hamlet"
hamletFile = H.hamletFile . toHamletFile
cassiusFile :: FilePath -> Q Exp
#ifdef PRODUCTION
cassiusFile x = H.cassiusFile $ "cassius/" ++ x ++ ".cassius"
cassiusFile = H.cassiusFile . toCassiusFile
#else
cassiusFile x = H.cassiusFileDebug $ "cassius/" ++ x ++ ".cassius"
cassiusFile = H.cassiusFileDebug . toCassiusFile
#endif
juliusFile :: FilePath -> Q Exp
#ifdef PRODUCTION
juliusFile x = H.juliusFile $ "julius/" ++ x ++ ".julius"
juliusFile = H.juliusFile . toJuliusFile
#else
juliusFile x = H.juliusFileDebug $ "julius/" ++ x ++ ".julius"
juliusFile = H.juliusFileDebug . toJuliusFile
#endif
widgetFile :: FilePath -> Q Exp
widgetFile x = do
let h = unlessExists toHamletFile hamletFile
let c = unlessExists toCassiusFile cassiusFile
let j = unlessExists toJuliusFile juliusFile
[|addWidget $h >> addCassius $c >> addJulius $j|]
where
unlessExists tofn f = do
e <- qRunIO $ doesFileExist $ tofn x
if e then f x else [|mempty|]
-- The next two functions are for allocating a connection pool and running
-- database actions using a pool, respectively. It is used internally
-- by the scaffolded application, and therefore you will rarely need to use

View File

@ -24,7 +24,7 @@ import System.Directory
import qualified Data.ByteString.Lazy as L
import Web.Routes.Site (Site (formatPathSegments))
import Database.Persist.GenericSql
import Settings (hamletFile, cassiusFile, juliusFile)
import Settings (hamletFile, cassiusFile, juliusFile, widgetFile)
import Model
import Data.Maybe (isJust)
import Control.Monad (join)