[web-devel] [Yesod] Creating a static link

Michael Snoyman michael at snoyman.com
Sun Feb 13 06:50:14 CET 2011


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 <michael at snoyman.com> wrote:
> On Sat, Feb 12, 2011 at 11:59 PM, Rafael Cunha de Almeida
> <rafael-lists at kontesti.me> 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
>>
>>        <img alt="" src=@{StaticR $ StaticRoute ["figure.png"] []}>
>>
>> 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:
>
>
>         <img alt="" src=@{StaticR figure_png}>
>
> I'm open to discussion regarding adding list notation to interpolations.
>
> Michael
>



More information about the web-devel mailing list