包含小部件一次?
我正在使用 yesod 0.9.3 和脚手架网站。有没有什么方法可以将某些小部件仅包含到结果页面中一次(或者最好使某些小部件仅包含一次),就像 addScript
和 addStylesheet
那样?我可以在页面处理程序中调用此类(依赖)小部件,但这打破了(依赖)小部件作为独立实体的整个想法,您只需在需要时调用它即可。
示例:
addCommonStyle :: Widget
addCommonStyle = toWidgetHead [lucius|.some-class {background: yellow}|]
styledP :: Text -> Widget
styledP t = do
addCommonStyle
[whamlet|<p .some-class>#{t}|]
getTestR :: Handler RepHtml
getTestR = defaultLayout $ do
styledP "First paragraph"
styledP "Second paragraph"
此结果在 HTML 中包含指向样式表的链接
.some-class{background:yellow}.some-class{background:yellow}
,即 addCommonStyle 被包含两次。
I'm using yesod 0.9.3 with scaffolded site. Is there any way to include some widget into resulting page only once (or, preferably, make some widget includable only once), like addScript
and addStylesheet
do? I can call such (dependency) widgets in the page handler, but this breaks the whole idea of (dependent) widget as a self-contained entity which you just call when you need it.
Example:
addCommonStyle :: Widget
addCommonStyle = toWidgetHead [lucius|.some-class {background: yellow}|]
styledP :: Text -> Widget
styledP t = do
addCommonStyle
[whamlet|<p .some-class>#{t}|]
getTestR :: Handler RepHtml
getTestR = defaultLayout $ do
styledP "First paragraph"
styledP "Second paragraph"
This result in HTML with link to stylesheet containing
.some-class{background:yellow}.some-class{background:yellow}
that is, addCommonStyle is included twice.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最简单的解决方案是将
addCommonStyle
放入Foundation.hs
文件中defaultLayout
的定义中。如果您并不总是需要它,您可以按照支架
defaultLayout
中的模式创建自己的布局函数,其中包括addCommonStyle
调用。easiest solution is to just put
addCommonStyle
in the definition ofdefaultLayout
in theFoundation.hs
file.If you dont always need it, you can just make your own layout function following the pattern in the scaffolded
defaultLayout
which includes theaddCommonStyle
call.