链接到哈姆雷特的静态文件 [英] Link to a static file from Hamlet
问题描述
我目前正在按照 Yesod Wiki上的教程进行Yesod实验。
I'm currently experimenting with Yesod by following the tutorial on the Yesod Wiki.
首先,我使用 yesod init
创建了一个yesod应用程序,并创建了一个根处理程序,该处理程序呈现一个名为首页
:
First I created a yesod application using yesod init
, and created a Root handler that renders a widget file called homepage
:
getRootR = do
mu <- maybeAuth
defaultLayout $ do
h2id <- lift newIdent
setTitle "Home"
addWidget $(widgetFile "homepage")
我在静态目录下有一个图像文件,名为 static / img / logo.png
I have an image file in the static directory call static/img/logo.png
在触摸 Settings / staticFiles.hs
后,我成功地将此文件从 default-layout.hamlet
via
After touching Settings/staticFiles.hs
, I successfully managed to link this file from default-layout.hamlet
via
<img src=@{StaticR img_logo_png}
现在问题就出现了,我想将这个静态文件包含在我的主页
小部件中,u唱完全一样的代码行。编译时出现以下错误:
The problem occurs now that I want to include this static file in my homepage
widget, using exactly the same line of code. The following error occurs at compilation:
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
定义的小部件中的静态资源,以及它为什么表现正常不同的是在默认的布局模板?
So my question is: how do I link static resources in widgets defined with widgetFile
, and why does it behave differently in the default layout template?
推荐答案
您需要添加一个导入到Handler / Root.hs:
You need to add an import to Handler/Root.hs:
import Settings.StaticFiles
如果一个hamlet文件需要它,那么调用该hamlet文件的任何handler.hs文件都需要先导入它。之所以default-layout.hamlet不需要任何改变,是因为它在某个地方被调用,我相信Application.hs几乎包含了所有的东西,包括静态东西。
If a hamlet file requires it then whichever handler.hs file that calls that hamlet file will need to import it first. The reason why default-layout.hamlet doesn't require any changes is because it is called somewhere in I believe Application.hs which has imports for pretty much everything, including static stuff.
这篇关于链接到哈姆雷特的静态文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!