包含小部件一次?

发布于 2024-12-18 01:37:36 字数 705 浏览 2 评论 0原文

我正在使用 yesod 0.9.3 和脚手架网站。有没有什么方法可以将某些小部件仅包含到结果页面中一次(或者最好使某些小部件仅包含一次),就像 addScriptaddStylesheet 那样?我可以在页面处理程序中调用此类(依赖)小部件,但这打破了(依赖)小部件作为独立实体的整个想法,您只需在需要时调用它即可。

示例:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白况 2024-12-25 01:37:36

最简单的解决方案是将 addCommonStyle 放入 Foundation.hs 文件中 defaultLayout 的定义中。

如果您并不总是需要它,您可以按照支架 defaultLayout 中的模式创建自己的布局函数,其中包括 addCommonStyle 调用。

easiest solution is to just put addCommonStyle in the definition of defaultLayout in the Foundation.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 the addCommonStyle call.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文