[Yesod] Creating a static link

From what I could gather, the way to create a link to a static page,
Hello,
like a picture is:
StaticR $ StaticRoute ["figure.png"] []
I wanted to add a static picture in my site's template so I added
to my template. Unfortunately, that didn't work. It seems like creating
lists inside @{} or even #{} is impossible. For instance
#{head ["test"]}
doesn't work either.
Is that a bug? Or is it how it's supposed to work?

On Sat, Feb 12, 2011 at 11:59 PM, Rafael Cunha de Almeida
Hello,
From what I could gather, the way to create a link to a static page, like a picture is:
StaticR $ StaticRoute ["figure.png"] []
I wanted to add a static picture in my site's template so I added
to my template. Unfortunately, that didn't work. It seems like creating lists inside @{} or even #{} is impossible. For instance
#{head ["test"]}
doesn't work either.
Is that a bug? Or is it how it's supposed to work?
You could go either way on that one. The content of an interpolation
is specifically *not* a fully-powered Haskell expression. The question
is what syntax do we want to include. I had not thought of including
list notation, and I don't see any technical barrier to adding it. As
with most template systems, Hamlet needs to strike a balance between
power and simplicity: I could go either way on this specific one.
However, for your use case, I would recommend a different approach:
Yesod.Helpers.Static provides a Template Haskell function calls
staticFiles. If you include it at the top level of your code like
this:
staticFile "static"
It will create an identifier for each and every file in your static
folder. In that case, you could replace your Hamlet code with:
I'm open to discussion regarding adding list notation to interpolations.
Michael

Sorry to respond to myself, but I didn't explain *why* I recommended
using staticFiles:
* It avoids typos: if you accidently type in "figure.gif" instead of
"figure.png", you won't find out till runtime. If you type in
figure_gif, the compiler will let you know something's wrong.
* It can make your site serve faster. staticFiles automatically
appends a hash of the file's content in the query string, so you can
set expiration dates in the distant future for static files and know
users always get the most recent content.
* If you enable -Wall, the compiler can let you know when there are
files in your static folder you are not using.
On Sun, Feb 13, 2011 at 6:40 AM, Michael Snoyman
On Sat, Feb 12, 2011 at 11:59 PM, Rafael Cunha de Almeida
wrote: Hello,
From what I could gather, the way to create a link to a static page, like a picture is:
StaticR $ StaticRoute ["figure.png"] []
I wanted to add a static picture in my site's template so I added
to my template. Unfortunately, that didn't work. It seems like creating lists inside @{} or even #{} is impossible. For instance
#{head ["test"]}
doesn't work either.
Is that a bug? Or is it how it's supposed to work?
You could go either way on that one. The content of an interpolation is specifically *not* a fully-powered Haskell expression. The question is what syntax do we want to include. I had not thought of including list notation, and I don't see any technical barrier to adding it. As with most template systems, Hamlet needs to strike a balance between power and simplicity: I could go either way on this specific one.
However, for your use case, I would recommend a different approach: Yesod.Helpers.Static provides a Template Haskell function calls staticFiles. If you include it at the top level of your code like this:
staticFile "static"
It will create an identifier for each and every file in your static folder. In that case, you could replace your Hamlet code with:
I'm open to discussion regarding adding list notation to interpolations.
Michael

On Sun, Feb 13, 2011 at 1:14 PM, Felipe Almeida Lessa
On Sun, Feb 13, 2011 at 3:50 AM, Michael Snoyman
wrote: * If you enable -Wall, the compiler can let you know when there are files in your static folder you are not using.
Besides enabling -Wall, you'd need an explicit export list, right?
Cheers! =)
Yes, good point. I'm not even sure if you *should* use this approach in all projects, I was just pointing out some of the possible benefits staticFiles can offer. Michael

Michael Snoyman
You could go either way on that one. The content of an interpolation is specifically *not* a fully-powered Haskell expression.
Why is that? I have a little experience with RoR, there I could do something like this: - @people.each do |person| = person.name I think you don't want to have a fully-powered Haskell expression to avoid people adding too much code to the view (thinking in MVC pattern). However, it's not like people can't do it just because it's a little harder. Meanwhile, there are a lot of useful things that could be done if all (or most) haskell syntax was allowed to be interpolated. Instead of having special syntax like $forall, wouldn't it be better to just provide monad tranformers to allow for those sort of loops? Something like Control.Monad.LoopWhile module provides. That way hamlet would need only to provide a ${} syntax that allow for adding haskell code which will be evaluated but won't print anything to the site. That way things could work a lot like in ruby, which I think makes things a lot simpler. Allowing for a new sort of interpolation and any sort of haskell expression to be interpolated, hamlet wouldn't need to create its own flow control statements. The user could write their views freely using only haskell syntax when code logic is required. []'s Rafael
participants (3)
-
Felipe Almeida Lessa
-
Michael Snoyman
-
Rafael Cunha de Almeida