如果您循环遍历数据集,从小部件加载 html 的最佳方法是什么?
即在一个小村庄文件中
$forall Entity id val <- collection
^{myWidget (p1 val) (p2 val)}
如果您循环遍历数据集,从小部件加载 html 的最佳方法是什么?
即在一个小村庄文件中
$forall Entity id val <- collection
^{myWidget (p1 val) (p2 val)}
请注意,Hamlet 模板只能嵌入 hamlet 模板。如果要嵌入小部件,请使用whamlet
.
另外,是的 - 您可以将参数传递给小部件。演示该概念的示例堆栈脚本:
#!/usr/bin/env stack
{- stack
--resolver lts-9.0
--install-ghc
runghc
--package yesod-core
--package yesod
--package shakespeare
-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeFamilies #-}
import Yesod
data App = App
mkYesod "App" [parseRoutes|
/ HomeR GET
|]
instance Yesod App
data Person = Person { id :: Int, name :: String, email :: String } deriving (Show, Eq, Ord)
collections = [Person 1 "Sibi" "sibi@domain.com", Person 2 "Michael" "michael@domain.com"]
whamlet1 :: Widget
whamlet1 = [whamlet| <p>hello
$forall Person _ pname pemail <- collections
^{whamlet2 pname pemail}
|]
whamlet2 :: String -> String -> Widget
whamlet2 pname pemail = [whamlet| <h1> #{pname} #{pemail} |]
getHomeR = defaultLayout $ do
setTitle "My Page Title"
toWidget [lucius| h1 { color: green; } |]
whamlet1
main = warp 3000 App
Yesod 书上的小部件章节更深入地解释了它们。