Static files in a Snaplet

277 Views Asked by At

I'd like to serve some static files from a Snaplet in the Snap framework. How do I do that? The documentation is not exactly clear.

I know how to add routes and stuff, but I'm stuck at two problems:

  1. What would I need to pass to serveDirectory to serve files from the snaplet directory?
  2. How would I reference these static files in Heist templates of my snaplet? I obviously can't use absolute URLs, since I don't know the URL prefix my snaplet is eventually installed in the final application. In other words, how do I get an URL relative to the snaplet root URL in a Heist template?
1

There are 1 best solutions below

4
mightybyte On

Yes, serveDirectory is what you would use to serve static files. You might have a route like this:

route [("static", serveDirectory "myDir"), ...]

When you reference those files in templates, you have to use the route that you assigned. So if you had a file myDir/foo.js, then in a template, you'd refer to it with /static/foo.js.

If you don't know your snaplet's base URL, you can get it with the getSnapletRootURL function. Then you can make that available in your templates with a Heist splice.