Passing python string to javascript

4,799 views
Skip to first unread message

José Eloy

unread,
Jan 28, 2015, 6:04:23 PM1/28/15
to web...@googlegroups.com
Hello!

How can I pass a python string to javascript? I know how to pass an integer variable to javascript.

Example:

def pass_variable():
    variable = 25
    return dict(variable=variable)

In the view:
<script type="text/javascript">
   var v={{=variable}};
   console.log(v);
</script>

If I pass a integer variable, javascript receive it well, but if I pass a string, javascript doesn't recognize it.

Now, let's assume that we use variable = "HOLA"

I get the error: (using Firefox console)
---------------------------------------------------
ReferenceError: HOLA is not defined
var v = HOLA;
---------------------------------------------------
What am I doing wrong?

Regards.

lillian

unread,
Jan 28, 2015, 6:51:43 PM1/28/15
to web...@googlegroups.com
Jose,

You are missing quotes:

var v='{{=variable}}';

In your code js thinks HOLA is an object.  If you don't want to use quotes on the js side you need to use extra ones in python and prevent them from being escaped:

variable = XML("'HOLA'")

This will get you 'HOLA' in js.  If you don't use the XML() handler you will get &#x27;HOLA&#x27;

That's my assessment of the situation anyways.  Better answers are probably out there ;-)

lillian

Anthony

unread,
Jan 28, 2015, 7:30:19 PM1/28/15
to web...@googlegroups.com
You need to use quotes:

var v = '{{=variable}}';

After the template is processed, it will look like:

var v = 'HOLA';

Anthony

Manuele Pesenti

unread,
Jan 29, 2015, 3:00:26 AM1/29/15
to web...@googlegroups.com
Il 29/01/15 00:04, José Eloy ha scritto:
Hello!

How can I pass a python string to javascript? I know how to pass an integer variable to javascript.

Example:

def pass_variable():
    variable = 25
    return dict(variable=variable)

In the view:
<script type="text/javascript">
   var v={{=variable}};
   console.log(v);
</script>

just try this:


<script type="text/javascript">
   var v="{{=variable}}";
   console.log(v);
</script>

cheers

    M.
If I pass a integer variable, javascript receive it well, but if I pass a string, javascript doesn't recognize it.

Now, let's assume that we use variable = "HOLA"

I get the error: (using Firefox console)
---------------------------------------------------
ReferenceError: HOLA is not defined
var v = HOLA;
---------------------------------------------------
What am I doing wrong?

Regards.
--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups "web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Leonel Câmara

unread,
Jan 29, 2015, 7:43:15 AM1/29/15
to web...@googlegroups.com
There's also the rarely used ASSIGNJS helper which you can use for this.

Anthony

unread,
Jan 29, 2015, 8:43:22 AM1/29/15
to web...@googlegroups.com
We should probably document that.

Massimo Di Pierro

unread,
Jan 29, 2015, 9:47:05 AM1/29/15
to web...@googlegroups.com
Actually

{{=ASSIGNJS(v = variable)}}

or, equivalent,

var v = {{=XML(json.dumps(variable))}}; 

is the right thing to do. Not in the manual but is in 2.9.11 and beyond.

José Eloy

unread,
Jan 29, 2015, 1:43:41 PM1/29/15
to web...@googlegroups.com
Thanks guys for your answer.

Making var v="{{=variable}}" was the solution.

I didn't know the ASSIGNJS helper. Is not in the manual. It would be great if is included.

Regards.

Anthony

unread,
Jan 29, 2015, 3:34:36 PM1/29/15
to web...@googlegroups.com
Yes, this is the best approach because it automatically handles quoting strings (but leaving numbers unquoted), so you don't have to worry about the data type.

Anthony
Reply all
Reply to author
Forward
0 new messages