user1771188
user1771188

Reputation:

How to pass value of javascript to Mako template

I'm planning on server's cherrypy with programming of python and mako. Now i have one problem because i know to pass value of Mako template to Javascript with

<script type="text/javascript"> 
var contapara=${input_nparams}; 
</script>

and viceversa? It 's possible apply viceversa Mako<---JAvascript?

Thanks

Edit:

Because I have one variable of Mako (kwargs) that contains the data of all forms sent. The user enters a word that is the "clave" and is stored in Javascript. After this I pass in Mako to search in variable (kwargs). Call the function's MAko. After exist this I open one windows with the textarea (new form) and I have to write data. AFter the user change data and send the form.

Upvotes: 2

Views: 3534

Answers (1)

Sheena
Sheena

Reputation: 16212

Make a view that takes in the arguments that you would want to send to mako, then in your javascript use ajax. for example:

jQuery.ajax(
    {
        url       : url_of_the_mako_view,
        data      : {foo:bar,foo2:bar2},
        type      : 'POST',
        success   : function(data)
        {
            /*
             when the mako template is rendered by your view then the result will
             be passed to this function in the variable data
            */
        }
    }
)

If you are after something a little more like a page redirect then you can do something more like this:

  1. make a form that redirects to the right place
  2. put in a hidden field
  3. populate the hidden field with a json string representing the arguments you want to pass to mako
  4. submit the form Then in the view that gets the form data you need to turn the json into a dictionary and pass that to mako

Upvotes: 1

Related Questions