Static files in Snaplet
I would like to serve up some static files from Snaplet in a Snap structure. How should I do it? The documentation is not entirely clear.
I know how to add routes and stuff, but I'm stuck with two problems:
- What do I need to pass in
serveDirectory
to serve files from the snaplet directory? - How can I reference these static files in the Heist templates of my snaplet? I obviously cannot use absolute urls since I don't know the URL prefix. My binding is eventually installed in the final application. In other words, how to get the url relative to the root url in the Heist pattern?
source to share
Yes, serveDirectory
this is what you would like to use for static files. You might have a route like this:
route [("static", serveDirectory "myDir"), ...]
When you link to these files in templates, you must use the route you assigned. Therefore, if you have a file myDir/foo.js
, then in the template you must refer to it with /static/foo.js
.
If you don't know your base url, you can get it using the getSnapletRootURL function . You can then make this available in your templates with the Heist spline.
source to share