Javascript rendering in tornado webserver
In following code snippet I'm trying to add JavaScript Files from Tornado server in HTML file in <HEAD>
tag.
DEBUG_SCRIPTS = ''' <script src="src/main.js" type="text/javascript"><开发者_StackOverflow社区;/script> '''
class Entries(tornado.web.UIModule):
def javascript_files(self):
return 'src/main.js'
class MainHandler(tornado.web.RequestHandler):
def get(self):
params = {}
params['CORE_SCRIPTS'] = DEBUG_SCRIPTS
path = os.path.join(os.path.dirname(__file__), 'index.html')
self.render(path, params=params)
by params['CORE_SCRIPTS'] = DEBUG_SCRIPTS
I was trying to add the <script>
tag in HTML but it gets parsed in text which generates
<script type="text/javascript" src="src/main.js"></script>
So I came across javascript_files()
method in tornado specs but I'm not getting any examples about its implementations. Can anyone help?
javascript files
only works with UIModules. Javascript files included that way are appended to the page just before the end of the body tag, though, not in the head tag.
If you really want the file included in the head tag, you can simply output the value of params['CORE_SCRIPTS']
in the head tag of your template:
{% raw params['CORE_SCRIPTS'] %}
精彩评论