Are there beautiful way to add variable in script?

42 views
Skip to first unread message

Константин Комков

unread,
Jul 29, 2019, 6:18:53 AM7/29/19
to web2py-users
Are there beautiful way to add variable in script?
I know that ways:
1. It's very simple, but it's not useful to write many javascript code in controller.
controller.py
XML('<script type="text/javascript">console.log('+myVariable+');</script>'),
2. In that case I have 2 script files in HTML. First contain my variable, second contain javascript code and get variable from first. It's not good too.
controller.py
def index():
    jsScript
= XML('<script src="'+URL('static','js\myScript.js')+'" type="text/javascript"></script>')
    myVariable
= '[[1,1,"text text"],[1,2,"text text"],...,[12,20,"tex text"]]'
   
return dict(jsScript=jsScript,myVariable=myVariable)
index.html
<script>
    var myVariable = '{{=myVariable}}';
</script>
{{=jsScript}}
Also I can write all js code in first script tag, but that practice is not normal too. I want have python files with python code, html files with html and js files with javascript. I tried add {{=myVariable}} in js file, but it's not work, as I think becouse it's static folder.





Dave S

unread,
Jul 29, 2019, 3:57:15 PM7/29/19
to web2py-users

Check out the ASSIGNJS() helper.
<URL:http://web2py.com/books/default/chapter/29/05/the-views#ASSIGNJS>

/dps
 

Константин Комков

unread,
Jul 29, 2019, 4:58:03 PM7/29/19
to web...@googlegroups.com
Dave S, thank you, it's what I looked for. In russian Book that helper was missed.
Reply all
Reply to author
Forward
0 new messages