views:

42

answers:

1

I have the following inclusion tag:

@register.inclusion_tag('bouts/fighter/_fighter_bout_list.html')
def fighter_bout_list(list, header, fighter, has, empty):
    return {
            'list' : list,
            'header': header,
            'fighter': fighter,
            'has' : has,
            'empty' : empty,
    }

To use it, I can include the following in my template:

{% fighter_bout_list wins "Wins" fighter "beat" "has no wins!" %}

However, I would like to make my tag readable so it is easier to see what the code is doing. Ideally, I'd like to use this for my input:

{% fighter_bout_list list=wins header="Wins" fighter=fighter has="beat" empty="has no wins!" %}

What is the best way (or even just a good way!) to do this?

+1  A: 

Changing the Django template system to do what you want would be extremely difficult; if you want to do that, start by completely understanding the django.template module. Frankly, I wouldn't recommend creating a tag that used a different way of handling parameters than all of the other existing tags -- it would prove confusing to the users that have to work with the tags.

But if you insist on doing this anyways, you just need to have your custom template tag parse its parameters individually to provide the service. Something like this should do the trick:

def fixit( argument ):
    """Strip off any leading 'word=' noise words from argument"""
    result = argument.split('=')[-1]
    if result[0] == '"' and result[-1] == '"': 
        result = result[1:-2]
    return result

@register.inclusion_tag('bouts/fighter/_fighter_bout_list.html')
def fighter_bout_list(list, header, fighter, has, empty):
    return {
            'list' : fixit(list),
            'header': fixit(header),
            'fighter': fixit(fighter),
            'has' : fixit(has),
            'empty' : fixit(empty),
    }

Edited

This code is still positional -- I'm not suggesting that you can use this to do true keyword arguments. I added code to deal with the presence of quotes, but I still haven't tested this -- it's just a suggestion.

Again, I'd strongly recommend against this approach. The point behind a template tag is to do something that can't be done easily with the existing template tags, but it's still part of a template, and is really there to be a tool for someone technical enough to use an HTML editor. I'm trying to picture a use case for your proposed tag where it will be used more than a couple of times in a site. A little documentation and an example would be a lot cheaper than trying to implement keyword arguments for a template tag in a system that doesn't use keyword arguments.

Craig Trader
+1 for both including warning and answering.
Manoj Govindan
Cheers for the response. I think it will do what I'm looking for. I'll try it. :-)
Brian Kessler
Tried it, but it didn't work. I initially thought there were problems with "list" and "fighter" which are not strings, but even when I refactored the code that those two arguments would remain positional, I got an error starting:Caught VariableDoesNotExist while rendering: Failed lookup for key [header="Wins"] in u'....The entire traceback has been posted to http://dpaste.com/232926/
Brian Kessler