embedded_css, embedded_javascript, javascript_files, or css_file

140 views
Skip to first unread message

docyes

unread,
Sep 22, 2009, 7:04:08 PM9/22/09
to Tornado Web Server
I sifted through src for how to implement these, but it's a bit
unclear.

Any help on howto do this within the context of a template would be
very helpful and appreciated!

Tzury Bar Yochay

unread,
Sep 23, 2009, 9:09:58 AM9/23/09
to Tornado Web Server
at http://www.tornadoweb.org/documentation#ui-modules
you can see an example for overriding embedded_css()
I assume that same applied for embedded_javascript(), javascript_files
(), and css_files()

Note that as mentioned at there Module CSS and JavaScript will be
included once no matter how many times a module is used on a page. CSS
is always included in the <head> of the page, and JavaScript is always
included just before the </body> tag at the end of the page.

class Entry(tornado.web.UIModule):
def embedded_css(self):
return ".entry { margin-bottom: 1em; }"

def render(self, entry, show_comments=False):
return self.render_string(
"module-entry.html", show_comments=show_comments)

Carl

unread,
Sep 23, 2009, 10:36:31 AM9/23/09
to python-...@googlegroups.com
That part is clear.

What's not clear is howto use these methods in a template file. Is
this possible and what is the syntax?

Thanks,
Carl

Casey

unread,
Sep 23, 2009, 1:45:53 PM9/23/09
to Tornado Web Server
Hi Carl, sorry for the confusion.

The ui_modules model is that the template files are html only, no
javascript or css.

The css and javascript only need to be included once, no matter how
many times the module is used, so we separate those out and insert
them in the proper place in the page (in the head for css, the end of
the body for javascript).

To specify the javascript and css, you can either put short snippets
in the uimodule with embedded_css and embedded_javascript (for one-
liners), or if they're longer, create separate .css and .js files and
list them in css_files and javascript_files.

Does that clarify things? If you provide details of the kind of
ui_module you're having trouble with, maybe I or somebody can give you
a more concrete example.

Casey

On Sep 23, 7:36 am, Carl <doc...@gmail.com> wrote:
> That part is clear.
>
> What's not clear is howto use these methods in a template file. Is
> this possible and what is the syntax?
>
> Thanks,
> Carl
>
> On Wed, Sep 23, 2009 at 6:09 AM, Tzury Bar Yochay
>
>
>
> <afro.syst...@gmail.com> wrote:
>
> > athttp://www.tornadoweb.org/documentation#ui-modules

Douglas Stanley

unread,
Sep 23, 2009, 1:58:46 PM9/23/09
to python-...@googlegroups.com
So now I'm curious. Does the templating system "parse" the html from
templates and inject css in the <HEAD></HEAD> section, and similarly
just before the body? What if you wanted javascript in a different
location (like inside the head tags)?

Thanks,
Doug

Casey

unread,
Sep 23, 2009, 2:22:41 PM9/23/09
to Tornado Web Server
That's correct Douglas, take a look at the template rendering
function: http://github.com/facebook/tornado/blob/master/tornado/web.py#L331

If you want javascript inside the head tags, the easiest way is
probably to add a {% block js_head %}{% end %} there, then fill in
that block from your templates. See the "title" example at the top of
template.py: http://github.com/facebook/tornado/blob/master/tornado/template.py

The javascript and css placement of uimodules is designed to support
the most common and highest performance cases, but you can just
include <script> and <style> tags in them directly also.

And lastly, if you want to use the js and css uimodules scheme but
have the js in the head, you could change the line in web.py linked
above from html.rindex('</body>') to html.index('</head>').

Casey

On Sep 23, 10:58 am, Douglas Stanley <douglas.m.stan...@gmail.com>
wrote:

docyes

unread,
Sep 23, 2009, 4:54:48 PM9/23/09
to Tornado Web Server
I love the </body> insertion for JS, thanks for that.
Reply all
Reply to author
Forward
0 new messages