The problem with your first "<script><json/></script>" approach is
that the parsing library has been designed to interpret everything
inside the <script> tag as plain text, so you can't have splices
there.  If we didn't do this, the parser would interpret the less-than
operator in javascript as the start of a new tag and you would get
parse errors.  Your approach is correct.  Your splices have to add the
script tags for you.  Greg's suggestion should work.  I have a splice
that I use for this.  Since it's a single tag I didn't bother with
using blaze.

scriptSplice :: Text
             -> (Text -> Text)
             -> Splice Application
scriptSplice scriptType transform = do
    node <- getParamNode
    return $ [Element "script" [("type", scriptType)] $
                [TextNode $ transform $ catTextNodes $ childNodes node]]

On Mon, Apr 25, 2011 at 7:45 AM, Gregory Collins
<[email protected]> wrote:
> On Mon, Apr 25, 2011 at 9:09 AM, Todd Porter <[email protected]> wrote:
>> Hi Snap team,
>>
>> am enjoying learning your framework.
>>
>> I am trying to dynamically inject some json data into a page: <script>var 
>> json = {} </script>.
>>
>> At first I tried bindString to support something like 
>> <script><json/></script> but Heist ignores (correctly I guess?) or 
>> alternatively escapes if I decide to pull the script tags into my string.
>>
>> So then tried creating the nodes using Blaze as follows:
>>
>> jsonSplice :: Splice Application
>> jsonSplice = return . renderHtml $ do
>>    H.script "var json = {}"
>>
>> but get the following:
>>
>> Couldn't match expected type `X.Node'
>>                with actual type `hexpat-0.19.6:Text.XML.Expat.Tree.NodeG
>>                                    [] BS.ByteString BS.ByteString'
>>    Expected type: Splice Application
>>      Actual type: TemplateMonad Application Forest
>>
>> Apologies if I am missing something obvious but this has me stumped, given 
>> the expected and actual types look equivalent to me.
>
> Problem seems to be that you are using Text.Blaze.Renderer.Hexpat, and
> heist doesn't use hexpat under the hood anymore; now we use xmlhtml.
> If you replace it with Text.Blaze.Renderer.XmlHtml (from the xmlhtml
> package), it should work (?).
>
> G
> --
> Gregory Collins <[email protected]>
> _______________________________________________
> Snap mailing list
> [email protected]
> http://mailman-mail5.webfaction.com/listinfo/snap
>
_______________________________________________
Snap mailing list
[email protected]
http://mailman-mail5.webfaction.com/listinfo/snap

Reply via email to