Cool, thanks for the tip.

I took your latter suggestion and below is a rewrite of the
django.template.loader.get_template method.  The only additional
requirement is the FS_DIR constant that I import from my settings file
(which I was already using in settings.py to clean up my TEMPLATE_DIR
path declarations).

def get_template(template_name, widget_name):
    from django.conf import settings
    return _get_template(template_name, dirs=[settings.FS_DIR +
'widgets/' + str(widget_name)])

def _get_template(template_name, dirs=None):
 
'''
    Returns a compiled Template object for the given template
name,
    handling template inheritance
recursively.

    The dirs argument is a list of directory
paths
    '''
    from django.template.loader import find_template_source,
get_template_from_string

    source, origin = find_template_source(template_name, dirs)
    template = get_template_from_string(source, origin, template_name)
    return template


On Feb 12, 12:26 am, Malcolm Tredinnick <[EMAIL PROTECTED]>
wrote:
> On Mon, 2008-02-11 at 16:55 -0800, Peter wrote:
> > I have a bunch of widgets that I access dynamically and they render
> > their own parts of a page.  I would like to give these widgets the
> > ability to use templates.  Furthermore, these are not installed apps,
> > so I do not have access to the
> > "django.template.loaders.app_directories.load_template_source" loader
> > type.
>
> > So the widgets are like this:
>
> > project/
> >   |_widgets/
> >        |_widget1/
> >        |_widget2/
>
> > I initially decided to just make a templates directory in this
> > "widgets" folder where each widget could add its own folder of
> > templates -- sounds reasonable.
>
> > However, I'm curious about... WhatifI let these widgets put
> > templates directly in their own folders and I declare that project/
> > widgets is atemplatedirectory?  This seems a bit blasphemous, but
> > ideologies aside -- are there computational issues with this approach
> > (e.g. longer search for resolving templates, strange conflicts?)
>
> Just do it. There's no problem here. TEMPLATE_DIRECTORIES is just a list
> of directories that is searched by the filesystem loader for files that
> match the name you want to load. So the only drawback is that those
> directories are searched every time you load atemplate. Might be an
> issue, might not be.
>
> Alternatively,ifyou want to super-efficient and tricky, your widgets
> could load theirtemplatestrings manually: you can pass a "dirs"
> argument to the find_template_source() function, so you could manually
> alter the directories to search when loading the templates. Using a
> combination of find_template_source() and get_template_from_string(),
> just as get_template() does, means you can create aTemplateobject
> instance, loading the source from a restricted, custom set of
> directories. Have a poke around in django/templates/loader.py for
> details.
>
> Regards,
> Malcolm
>
> --
> The only substitute for good manners is fast 
> reflexes.http://www.pointy-stick.com/blog/
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to