import json

from django.http import HttpResponse

from wagtail.utils.compat import render_to_string


def render_modal_workflow(request, html_template, js_template, template_vars=None):
    """
    Render a response consisting of an HTML chunk and a JS onload chunk
    in the format required by the modal-workflow framework.
    """
    response_keyvars = {}

    if html_template:
        html = render_to_string(html_template, template_vars or {}, request=request)
        response_keyvars['html'] = html

    if js_template:
        js = render_to_string(js_template, template_vars or {}, request=request)
        response_keyvars['onload'] = js

    return HttpResponse(json.dumps(response_keyvars), content_type="application/json")

@@ -183,24 +183,25 @@
 ne):%0A    %22%22%22
+%22
 %0A    Render 
@@ -352,18 +352,18 @@
 yvars = 
-%7B%7D
+%5B%5D
 %0A%0A    if
@@ -491,23 +491,48 @@
 vars
-%5B'html'%5D = 
+.append(%22'html': %25s%22 %25 json.dumps(
 html
+))
 %0A%0A  
@@ -658,23 +658,93 @@
 vars
-%5B'onload'%5D = js
+.append(%22'onload': %25s%22 %25 js)%0A%0A    response_text = %22%7B%25s%7D%22 %25 ','.join(response_keyvars)
 %0A%0A  
@@ -765,27 +765,16 @@
 esponse(
-json.dumps(
 response
@@ -774,24 +774,20 @@
 esponse_
-keyvars)
+text
 , conten
@@ -798,23 +798,22 @@
 pe=%22
-application/json
+text/javascript
 %22)%0A

Revert "Tidy up render_modal_workflow"