Search code examples
haskellyesod

How to convert Json to Html in yesod?


Is there some way in yesod-0.9.3 to convert Json to Html to include it into <script/> tag?

Using Yesod.Json and Data.Aeson I can get the value of a Json type, which I want to use as the value of a variable introduced in a <script> tag, that is, not to return RepJson, but to include it as a part of RepHtml.


Solution

  • Using fully qualified names...

    let txtVal = Data.Text.Lazy.Encoding.decodeUtf8 $ Data.Aeson.encode jsonVal
    

    then, inside your hamlet:

    <script>
      var v = #{txtVal}
    

    Note that this is a bit dangerous in that it doesn't check that the encoding was successful. Look at the Data.Text.Encoding module for more info.