2

I'm building a simple application using Yesod and I'm having a hard time bringing in external files. Here is my code:

{-# LANGUAGE OverloadedStrings     #-}
{-# LANGUAGE QuasiQuotes           #-}
{-# LANGUAGE TemplateHaskell       #-}
{-# LANGUAGE TypeFamilies          #-}
import           Yesod

data GomokuServer = GomokuServer

mkYesod "GomokuServer" [parseRoutes|
/ HomeR GET
|]

instance Yesod GomokuServer

getHomeR :: Handler Html
getHomeR = defaultLayout $ do 
        $(hamletFile "./src/templates/home.hamlet")
        $(luciusFile "./src/templates/home.lucius")

main :: IO ()
main = warp 3000 GomokuServer

It works great when I use quasiquotes, or if I replace hamletFile with whamletFile, but otherwise it won't compile because it can't find hamletFile or luciusFile. I'm using Yesod version 1.4 and I thought those methods where imported with the core Yesod package. Are they not?

Pedro Hoehl Carvalho
  • 2,183
  • 2
  • 19
  • 25

1 Answers1

3

Why don't you just use whamletFile? Most of the time you want a widget.

It seems hamletFile is not re-exported.

λ import Yesod
λ :t hamletFile

<interactive>:1:1: error:
    • Variable not in scope: hamletFile
    • Perhaps you meant ‘whamletFile’ (imported from Yesod)

If you really need it, you could bring it in from Text.Hamlet.

FWIW the Yesod scaffolding defines a function

widgetFile :: String -> Q Exp
widgetFile = (if appReloadTemplates compileTimeAppSettings
                then widgetFileReload
                else widgetFileNoReload)
              widgetFileSettings

And then I just use this everywhere like $(Settings.widgetFile "homepage") which by default brings in the hamlet, lucius, and cassius files for "homepage". You can see more info at Overriding-widgetFile on the Yesod wiki

Ben
  • 574
  • 3
  • 12
  • Can I use `whamletFile` to import external `.lucius` files? I was under the impression it would only work with `.hamlet` files. I ended up importing using Text.Hamlet. – Pedro Hoehl Carvalho Oct 20 '17 at 21:36
  • You cannot use `whamletFile` to import lucius files, but you can use the `widgetFile` function which will import all 3 hamlet, lucius, and cassius files. – Ben Oct 21 '17 at 01:35
  • But `widgetFile` is not included in the core yesod library, right? I would have to add yesod-scaffold to my dependency list, which seems like an overkill, considering that’s the only function I’ll use. – Pedro Hoehl Carvalho Oct 21 '17 at 01:47
  • The yesod-scaffold repo is used by `stack new` to generate new project templates. I don't think you can even include it as a dependency with cabal/stack. You could just copy the function into your project though: https://github.com/bitemyapp/ghcjs-starter-project/blob/master/backend/Settings.hs#L85-L105 – Ben Oct 21 '17 at 21:01