I have a local static Javascript file I want to include site-wide. Preferably I want all static Javascript files to be combined into a single file, but I want to manage these files separately. Where should static Javascript files be included in a Yesod project?
There is a more specific solution.
There is a folder for static Javascript files at ''static/js''.
There is a more specific widget command for Javascript: addScript
For every static file, the Static subsite generates a "::Route Static" symbol, replacing the non-identifier characters "/-." with '_' in the filepath from ''static/''.
Sample:
import Yesod.Widget (addScript)
-- given a file path of static/js/my-script.js
addScript $ StaticR js_my_script_js
If your project is partially compiled, you may get an error like:
Foundation.hs:...: Not in scope: `js_my_script_js'
Then either
unix touch the StaticFiles.hs module which generate the route symbols by calling staticFiles
or clean your build and rebuild it, and it disappears.
Cheers!
You may read about it also at my/(yours) wikipedia article. Feel free to correct and complete it.
You can use addWidget
in your defaultLayout
function to add it to the widgets, which are combined into a single file.
For example,
defaultLayout widget = do
...
pc <- widgetToPageContent $ do
$(widgetFile "mywidget")
... other stuff here ...
...
Now, simply put a file called mywidget.julius
to the templates directory and it should be automatically included on all pages.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With