11

我目前正在按照Yesod Wiki 上的教程进行 Yesod试验。

首先,我使用创建了一个 yesod 应用程序yesod init,并创建了一个 Root 处理程序,该处理程序呈现一个名为的小部件文件homepage

getRootR = do
mu <- maybeAuth
defaultLayout $ do
    h2id <- lift newIdent
    setTitle "Home"
    addWidget $(widgetFile "homepage")

我在静态目录调用中有一个图像文件static/img/logo.png

触摸后Settings/staticFiles.hs,我成功地从default-layout.hamlet通过链接这个文件

<img src=@{StaticR img_logo_png}

现在问题出现了,因为我想在我的homepage小部件中包含这个静态文件,使用完全相同的代码行。编译时出现以下错误:

Handler/Root.hs:19:21:
    Not in scope: `img_logo_png'
    In the result of the splice:
      $(widgetFile "homepage")
    To see what the splice expanded to, use -ddump-splices
    In the first argument of `addWidget', namely
      `$(widgetFile "homepage")'
    In the expression: addWidget ($(widgetFile "homepage"))

所以我的问题是:如何在用 定义的小部件中链接静态资源widgetFile,为什么它在默认布局模板中的行为不同?

4

1 回答 1

6

您需要向 Handler/Root.hs 添加导入:

import Settings.StaticFiles

如果一个 hamlet 文件需要它,那么调用该 hamlet 文件的任何 handler.hs 文件都需要先导入它。default-layout.hamlet 不需要任何更改的原因是因为它在我相信 Application.hs 的某个地方被调用,它几乎可以导入所有内容,包括静态内容。

于 2011-10-20T04:52:11.193 回答