Using Licius + Hamlet + Julius in Servant

2019-07-09 05:19发布

问题:

I would like to use Shakespearean Templates (Licius + Hamlet + Julius) from the Yesod.But I have some difficulty with this. The following code from enter link description here works:

type TestAPI 
    = "tests" :> Get '[JSON] [Test]
    :<|> "test" :> Get '[JSON] Test
    :<|> "TestHTML.html" :> Get '[HTML] Page_TestHTML 

serverTestAPI :: ServerT TestAPI AppM
serverTestAPI = tests 
           :<|> test
           :<|> testHtml

data Page_TestHTML = Page_TestHTML

instance ToMarkup Page_TestHTML where
    toMarkup Page_TestHTML = builderHtml  

testHtml = return Page_TestHTML

builderHtml = [shamlet|
                $doctype 5
                <html>
                    <head>
                        <title>Greeting2
                <body>
                    <h2> Hello world HTML Qqqqq |]

But next code is not working:

data Page_TestHTML_2 = Page_TestHTML_2

instance ToMarkup Page_TestHTML_2 where
    toMarkup Page_TestHTML_2 = builderHtml_2

testHtml_2 = return Page_TestHTML_2

builderHtml_2 = do 
    $(luciusFile "templates/test/TestHTML2.lucius")
    $(shamletFile "templates/test/TestHTML2.hamlet")

How I can constructing together Licius + Hamlet + Julius for Servant (without whole Yesod)?

回答1:

I was able to solve the question. The following code works!

data Page_LoginHTML = Page_LoginHTML

instance H.ToMarkup Page_LoginHTML where
    toMarkup Page_LoginHTML = builderHtml  

loginHtml = return Page_LoginHTML

cssStyle :: Html 
cssStyle = toHtml $ renderCssUrl undefined 
    [cassius|
        .q-test-2 
            color: green
    |]

htmlBody :: Html 
htmlBody =   
    [shamlet|
        <h1> Hamlet Login Render
    |]

builderHtml = H.docTypeHtml $ do
    H.head $ do
        H.title "Login"
        H.style cssStyle
    H.body htmlBody

Maybe it will be useful for somebody.