简体   繁体   English

龙卷风Web服务器中的Javascript渲染

[英]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. 在以下代码段中,我试图在<HEAD>标记的HTML文件中从Tornado服务器添加JavaScript文件。

DEBUG_SCRIPTS = ''' <script src="src/main.js" type="text/javascript"></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 通过params['CORE_SCRIPTS'] = DEBUG_SCRIPTS我试图在HTML中添加<script>标记,但是它被解析为文本,从而生成

&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. 因此,我在龙卷风规范中遇到了javascript_files()方法,但没有得到有关其实现的任何示例。 Can anyone help? 有人可以帮忙吗?

javascript files only works with UIModules . javascript files仅适用于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. 但是,以这种方式包含的Javascript文件会在body标签末尾之前添加到页面中,而不是在head标签中。

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: 如果您确实希望文件包含在head标签中,则只需在模板的head标签中输出params['CORE_SCRIPTS']的值即可:

{% raw params['CORE_SCRIPTS'] %}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM