Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to include static javascript in a Yesod project?

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?

like image 385
Joost Avatar asked Nov 04 '12 11:11

Joost


2 Answers

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.

like image 105
Gabriel Riba Avatar answered Nov 15 '22 08:11

Gabriel Riba


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.

like image 27
Jani Hartikainen Avatar answered Nov 15 '22 06:11

Jani Hartikainen