与Yesod莎士比亚一起使用仆人(哈姆雷特,朱利叶斯,卢修斯) [英] Using Servant with Yesod shakespeare (Hamlet, Julius, Lucius)

查看:161
本文介绍了与Yesod莎士比亚一起使用仆人(哈姆雷特,朱利叶斯,卢修斯)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我试试:

 类型TestAPI 
=tests:>获取'[JSON] [测试]
:< |> Test.html:> Get'[HTML] Html

serverTestAPI :: ServerT TestAPI AppM
serverTestAPI =测试
:< |>测试
:< |> testHtml

tests :: AppM [Test]
tests = do return [Test 1Test 1
,Test 2Test 2
]

testHtml = [hamlet |
$ doctype 5
.........
|]

但是我得到错误!

解决方案

正如@Carsten所指出的,在这种情况下,你想要的是 shamlet 。关键是要实现 ToMarkup typeclass的正确实例。我建议您阅读莎士比亚模板上精彩介绍。一个可行的例子:

pre $ data $ Person $ Person
{firstName :: String
,lastName :: String
}派生类型

实例ToJSON Person

类型PersonAPI =persons:>获得'[JSON,HTML] [Person]

人:: [Person]
人=
[PersonIsaacNewton
,PersonAlbert 爱因斯坦
]

实例ToMarkup Person其中
toMarkup person = showPerson人

- 未正确执行
preEscapedToMarkup p = showPerson p

- 人员列表的HTML序列化
实例ToMarkup [Person]其中
toMarkup persons = showPersons个人

preEscapedToMarkup p = showPersons p

showPerson :: Person - > Html
showPerson p = [shamlet |
< body>
< p>这是我的页面。
< h1>#{firstName p}
|]

showPersons :: [Person] - > Html
showPersons p = [shamlet |
< body>
< p>这是我的页面。
$ forall person< - p
< h1>#{firstname person}
|]


How i can use shakespeare (from yesod) for servant webservices APIs?

I try:

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

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

tests :: AppM [Test]
tests = do return [ Test 1 "Test 1"
                  , Test 2 "Test 2"
                  ]

testHtml = [hamlet|
                $doctype 5
                 .........
                 |]

But I get error!

解决方案

As @Carsten points out, in this case what you want is shamlet. The key thing is implementing proper instances of the ToMarkup typeclass. I recommend you to read this excellent introduction on Shakespeare templates. A working example:

data Person = Person
  { firstName :: String
  , lastName  :: String
  } deriving Generic 

instance ToJSON Person

type PersonAPI = "persons" :> Get '[JSON, HTML] [Person]

people :: [Person]
people =
  [ Person "Isaac"  "Newton"
  , Person "Albert" "Einstein"
  ]

instance ToMarkup Person where
  toMarkup person = showPerson person

  -- this isn't properly implemented
  preEscapedToMarkup p = showPerson p

-- HTML serialization of a list of persons
instance ToMarkup [Person] where
  toMarkup persons = showPersons persons

  preEscapedToMarkup p = showPersons p

showPerson :: Person -> Html
showPerson p = [shamlet|
<body>
    <p>This is my page.
    <h1>#{firstName p}
|]

showPersons :: [Person] -> Html
showPersons p = [shamlet|
<body>
    <p>This is my page.
     $forall person <- p
      <h1>#{firstName person}
|]

这篇关于与Yesod莎士比亚一起使用仆人(哈姆雷特,朱利叶斯,卢修斯)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆