Fwd: Built In Template Tag Request

8 views
Skip to first unread message

Paul Kenjora

unread,
Apr 27, 2016, 1:20:21 PM4/27/16
to django...@googlegroups.com
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

Reply all
Reply to author
Forward
0 new messages