【问题标题】:Link to a static file from Hamlet从 Hamlet 链接到静态文件
【发布时间】:2011-12-11 02:44:34
【问题描述】:

我目前正在通过tutorial on the Yesod Wiki 尝试 Yesod。

首先,我使用 yesod init 创建了一个 yesod 应用程序,并创建了一个 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 定义的小部件中链接静态资源,为什么它在默认布局模板中的行为不同?

【问题讨论】:

    标签: haskell yesod


    【解决方案1】:

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

    import Settings.StaticFiles
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-03
      • 2017-06-23
      • 2015-05-06
      • 2013-04-06
      • 1970-01-01
      • 2018-12-18
      • 2013-04-27
      相关资源
      最近更新 更多