Wednesday, April 27, 2016

Fwd: Built In Template Tag Request

Hello,

  I sometimes have to pass more complex content into an include or another tag as a variable.  The {% with %} tag and filters aren't enough to for example render an HTML button and pass it into another include.

  I'd like to request a {% with_html variable %} tag in the built in tag set.  Like the one below....

@register.tag
def with_html(parser, token):
  nodelist = parser.parse(('endwith_html',))
  parser.delete_first_token()
  return WithHTML(nodelist, token.contents.split()[1])

class WithHTML(template.Node):
  def __init__(self, nodelist, variable):
    self.nodelist = nodelist
    self.variable = variable
  def render(self, context):
    context[self.variable] = self.nodelist.render(context)
    return ''

A use case would be for example...

    {% with_html extra %}
      {% if row.0.ingredient_set.count %}{{ row.0.ingredient_set.count }} Ingredients{% endif %}
      {% if row.0.step_set.count %}<br/>{{ row.0.step_set.count }} Steps{% endif %}
    {% endwith_html %}
    {% with item_title=row.0.title item_description=row.0.description item_image=row.0.image item_extra=extra item_link=row.0.link item_button=newsletter.recipe_button %}
      {% include "newsletter/email_full.html" %}
    {% endwith %}

This type of tag would help keep the code more DRY in my opinion since the rendering logic for the complex content doesn't need extra files and I can now make includes more generic.  It would be a great compliment to the already existing with tag.

--
- Paul Kenjora
- 602-214-7285

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAEH8JE07Kcumv_TaMY-vSnLN3UaM3k8HScB5oT1Oi%3Dt%3DjcsF1Q%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment