Add a couple more template tags like this and we'll have another programming language :)
Actually, recently I was also thinking about creating some similar tag: {% set object.whatever.whatever.property as variable %} One thing that can be done for shortening access paths and for temporary caching is creating methods for the objects, like this: def get_related_property(self): if not hasattr(self, '_related_property_cache'): self._related_property_cache = self.whatever.whatever.property return self._related_property_cache Then instead of {{ object.whatever.whatever.property }}, you can use {{ object.get_related_property }} and even if you do that multiple times, the database is accessed only at the first time of accessing the method. Regards Aidas Bendoraitis aka Archatas On 3/22/07, akonsu <[EMAIL PROTECTED]> wrote: > > hello, > > thanks for posting. > > te way i do it is by creating a variable: > > {% let email = company.contact.email %} > > after that you can use email variable just like any other. > > konstantin > > class LetNode(Node) : > def __init__(self, var, expr) : > self.var, self.expr = var, expr > > def render(self, context) : > try : > context[self.var] = resolve_variable(self.expr, context) > except VariableDoesNotExist : > context[self.var] = None > return '' > > LET_TAG_RE = re.compile(r'^\s*let\s+(\w+)\s*=\s*(\S+)\s*$') > > @register.tag > def let(parser, token) : > m = LET_TAG_RE.search(token.contents) > if not m : > raise TemplateSyntaxError, 'invalid syntax' > return LetNode(*m.groups()) > > > On Mar 22, 8:35 am, "Andrew Durdin" <[EMAIL PROTECTED]> wrote: > > Recently I've found myself repeating variables many times in > > templates, much like this: > > > > {% if company.contact.email %} > > <h3>Email address</h3> > > <a href='mailto: > > {{ company.contact.email }}'>{{ company.contact.email }}</a> > > {% endifvalue %} > > > > This repetition is annoying, as well as inefficient if company.contact > > involves a ForeignKey lookup. > > > > I've created a pair of tags, "ifvalue" and "ifnotvalue", to > > encapsulate this behaviour: > > > > {% ifvalue company.contact.email as email %} > > <h3>Email address</h3> > > <a href='mailto:{{ email }}'>{{ email }}</a> > > {% endifvalue %} > > > > The full code for the tag is athttp://www.djangosnippets.org/snippets/124/ > > > > Andrew > > > > > --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---