开发者

Javascript rendering in tornado webserver

开发者 https://www.devze.com 2023-04-02 11:09 出处:网络
In following code snippet I\'m trying to add JavaScript Files from Tornado server in HTML file in <HEAD> tag.

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

&lt;script type=&quot;text/javascript&quot; src=&quot;src/main.js&quot;&gt;&lt;/script&gt;

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'] %}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号