I am trying to figure out how to emit Javascript code using standalone Julius outside yesod:
{-# LANGUAGE QuasiQuotes #-}
import qualified Data.Text.Lazy.IO as LazyIO
import Text.Julius
main = do
let delta = 1 :: Int
LazyIO.putStrLn $ renderJavascript $ [julius|
function f(x) {
return x + #{delta};
}
|] undefined
But I am getting this error:
t2.hs:8:48:
No instance for (ToJavascript Integer)
arising from a use of ‘toJavascript’
In the expression: toJavascript delta
...
Please help. I have no idea what it needs, I have just started looking at Julius. If I remove the interpolation then it renders the text successfully.
Try this:
Explanation:
The error message is saying that
delta
needs to have aToJavascript
instance. Looking up theToJavascript
class shows that these instances are defined by default:The lack of an
Int
(orInteger
) instance explains the error message.However, there is a
Value
instance, and by usingtoJSON
from the Aeson library we can turn anInt
into aValue
.