I have written certain custom tags that accept HTML attributes
(typically id and class), so they can often be verbose in usage. My
small idea was to write a tag that renders its contents as a tag, but
the trouble is that I cannot figure out how to parse the tag body.

For multi-line use of a tag "formtag," this wrapper would be used as
follows:

{% tag %}
  formtag accounts.views.login id='test'
  classes='this is a lengthy list of css classes'
{% endtag %}

As opposed to

{% formtag accounts.views.login id='test' classes='this is a lengthy
list of css classes' %}

In render() below, what might I do with the built token to parse it
normally? Parser.parse seems to contain the logic necessary to find
the tag function and create the correct Node type, but I'm not sure
how to use it on its own.

@register.tag(name='tag')
def do_tag(parser, token):
  nodelist = parser.parse('endtag',)
  parser.delete_first_token()
  return TagNode(parser, nodelist)

class TagNode(template.Node):
  def __init__(self, parser, nodelist):
    self.parser = parser
    self.nodelist = nodelist

  def render(self, context):
    tagbody = ' '.join(self.nodelist[0].render(context).splitlines())
    token = template.Token(template.TOKEN_BLOCK, tagbody)
    return ''


Thomas Allen

On May 24, 1:52 pm, Dennis Kaarsemaker <den...@kaarsemaker.net> wrote:
> On ma, 2010-05-24 at 07:15 -0700, Thomas Allen wrote:
>
> > Thomas Allen wrote:
> > > Is that possible in a Django template? If my tag spans more than one
> > > line, it is rendered as plaintext.
>
> > Is this not possible?
>
> Correct. Tags should be on one line.
>
> --
> Dennis K.
>
> They've gone to plaid!
>
> --
> You received this message because you are subscribed to the Google Groups 
> "Django users" group.
> To post to this group, send email to django-us...@googlegroups.com.
> To unsubscribe from this group, send email to 
> django-users+unsubscr...@googlegroups.com.
> For more options, visit this group 
> athttp://groups.google.com/group/django-users?hl=en.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to