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:
- What would I need to pass to
serveDirectoryto serve files from the snaplet directory? - 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?
Yes,
serveDirectoryis what you would use to serve static files. You might have a route like this: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.