Skip to content

Conversation

alanjds
Copy link
Contributor

@alanjds alanjds commented Dec 19, 2013

I needed a template directory schema where the flavor prefix cames after the last slash, like in: /appname/{flavor}-template_name.html

Then created the FLAVOURS_TEMPLATE_NAMEGETTER, to be able to have such schema. Example:

# settings.py
def _mobile_template_getter(flavour, template_name):
    folder, sep, filename = template_name.rpartition('/')
    return u'{folder}{sep}{flavour}-{filename}'.format(**locals())

FLAVOURS_TEMPLATE_NAMEGETTER = _mobile_template_getter

What you think?

@gregmuellegger
Copy link
Owner

Hi @alanjds, what's the reason that you don't use the suggested template structure like: {flavour}/appname/template_name.html for your purposes?

@alanjds
Copy link
Contributor Author

alanjds commented Jan 9, 2014

The designer feels that having both templates on the same folder helps him know what templates exist and what ones needs to be created

@gregmuellegger
Copy link
Owner

I'm not very inclined to include your suggest change. Do you think it's ok for you to subclass the template Loader class and override the prepare_template_name method? I guess that's a nice and easy way to deal with your issue. Or are there any reasons against this approach that I might overlook at the moment?

@alanjds
Copy link
Contributor Author

alanjds commented Jan 9, 2014

Hum... I dont really think that one should subclass the Loader to have non-standard schemes. This is the reason why FLAVOURS_TEMPLATE_PREFIX exists IMHO.

If I subclass the Loader in my project and do not call super(), and the logic changes on upstream, then I will have to keep attention to its code, like I would need in a monkey-patch. If I subclass and do call super(), I will have to process/split/match some templatename that django-mobile maybe had already changed.

All this seems to be a lot harder then attaching a function that hooks the template name getter logic.

Is there some caveat on accepting this change? Is there some "cleaning" that I could provide to ease the maintenance in the future?

@ToxicWar
Copy link

subclass the template Loader class and override the prepare_template_name method

from django_mobile.loader import Loader as BaseLoader
class Loader(BaseLoader):
    def prepare_template_name(self, template_name):
        template_name = list(template_name.rpartition('/'))
        name = list(template_name[-1].rpartition('.'))
        name.insert(1, '-%s' % get_flavour())
        del(template_name[-1])
        template_name += name
        return ''.join(template_name)

When used this Loader happening looping. To avoid this, you need to override __init__ method.
Something like:

def __init__(self, *args, **kwargs):
    loaders = []
    templates_loader = list(settings.FLAVOURS_TEMPLATE_LOADERS)
    templates_loader.pop(templates_loader.index('some_app.loader.Loader'))
    for loader_name in templates_loader:
        loader = find_template_loader(loader_name)
        if loader is not None:
            loaders.append(loader)
    self.template_source_loaders = tuple(loaders)
    super(BaseLoader, self).__init__(*args, **kwargs)

@alanjds
Copy link
Contributor Author

alanjds commented Jan 29, 2014

@ToxicWar Looks like a lot more code than I need to do with this PR merged. But thanks for pointing me an example.

@gregmuellegger gregmuellegger force-pushed the master branch 2 times, most recently from e7c2eef to 1edf2f8 Compare August 19, 2014 19:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants