Templates in multiple directories

1,072 views
Skip to first unread message

Matthew Turk

unread,
Apr 5, 2012, 4:31:18 PM4/5/12
to python-...@googlegroups.com
Hi there,

I'm somewhat new to Tornado, but I was wondering if it was possible to render templates from a web.Application from multiple directories.  For instance, if I have a systemwide template directory, is there a way to then override with a user-specific template file?

Thanks for any ideas,

Matt

Dmitri Cherniak

unread,
Apr 5, 2012, 4:41:08 PM4/5/12
to python-...@googlegroups.com
You can use subdirectories within your template directory. just call

self.render("your_sub_dir/template.html", **kwargs)

it's just a path you need to pass in so you can alter the string as you wish.

Joe Meyer

unread,
Apr 5, 2012, 4:47:19 PM4/5/12
to python-...@googlegroups.com
Hi Matt,

I'm a new user and jus

t brute forced this for this by defining the template directory at run time based on whats on the file system.

Eg:

if os.path.isfile(os.path.join(TEMPLATE_TOP,self.client,self.page+".tt")):
                 tt_file = os.path.join(client,self.page+".tt")
            elif os.path.isfile(os.path.join(TEMPLATE_TOP,self.page+".tt")):
                tt_file = self.page+".tt"
            else:
                tt_file = "default.tt"

Cheers,

Joe

Alek Storm

unread,
Apr 5, 2012, 5:08:28 PM4/5/12
to python-...@googlegroups.com
So you have a directory structure like this:

system/
    one.html
    two.html

some_user/
    two.html
    three.html

And you want:

self.render("some_user/one.html") -> contents of system/one.html
self.render("some_user/two.html") -> contents of some_user/two.html
self.render("some_user/three.html") -> contents of some_user/three.html

Right? I'd create a subclass of template.BaseLoader that overrides resolve_path() to return "system/<template>" if "<user>/<template>" does not exist. Then store an instance of it as self.loader in your Application constructor, and in a RequestHandler, call self.write(self.app.loader.load(path).generate(*args, **kwargs)), rather than self.render(path, *args, **kwargs).

Alek

Ben Darnell

unread,
Apr 5, 2012, 11:30:23 PM4/5/12
to python-...@googlegroups.com
On Thu, Apr 5, 2012 at 2:08 PM, Alek Storm <alek....@gmail.com> wrote:
> Right? I'd create a subclass of template.BaseLoader that overrides
> resolve_path() to return "system/<template>" if "<user>/<template>" does not
> exist. Then store an instance of it as self.loader in your Application
> constructor, and in a RequestHandler, call
> self.write(self.app.loader.load(path).generate(*args, **kwargs)), rather
> than self.render(path, *args, **kwargs).

Actually, if you use the template_loader Application setting (I really
need to document all the recognized settings), self.render() will just
work. You probably want to use self.render() instead of calling
Template.generate() whenever you can, since RequestHandler.render() is
responsible for setting up part of the template namespace.

-Ben

Brian Jones

unread,
Jul 21, 2012, 3:10:16 PM7/21/12
to python-...@googlegroups.com
On Thu, Apr 5, 2012 at 11:30 PM, Ben Darnell <b...@bendarnell.com> wrote:

Actually, if you use the template_loader Application setting (I really
need to document all the recognized settings),j


Just a bump. I found this comment by searching for 'possible settings' after (re)-discovering that there actually is a template_path setting separate from the static_path setting. A complete list would be so totally awesome. I bet I've duplicated effort and reinvented wheels a dozen times only to find something in settings could do it for me. :-o 

If and when such a document ever exists, please post to the list.
Thanks!

brian

 
-Ben

>
> Alek
>
> On Thu, Apr 5, 2012 at 3:41 PM, Dmitri Cherniak <dmi...@gmail.com> wrote:
>>
>> You can use subdirectories within your template directory. just call
>>
>> self.render("your_sub_dir/template.html", **kwargs)
>>
>> it's just a path you need to pass in so you can alter the string as you
>> wish.
>>
>>
>> On Thu, Apr 5, 2012 at 4:31 PM, Matthew Turk <matth...@gmail.com>
>> wrote:
>>>
>>> Hi there,
>>>
>>> I'm somewhat new to Tornado, but I was wondering if it was possible to
>>> render templates from a web.Application from multiple directories.  For
>>> instance, if I have a systemwide template directory, is there a way to then
>>> override with a user-specific template file?
>>>
>>> Thanks for any ideas,
>>>
>>> Matt
>>
>>
>



--
Brian K. Jones
My Blog          http://www.protocolostomy.com
Follow me      http://twitter.com/bkjones
Reply all
Reply to author
Forward
0 new messages