Package: django-template-block-args
django_template_block_argsMotivationThis tool makes it easier to "componentize" your markup. Specifically, it makes it easier to create template tags that receiveblocksas arguments.We've all written dialog markup that looks like this:<div class="dialog dialog-success"><div class="dialog-success-icon"></div><div class="dialog-content">Content goes here!</div></div>if you've written this more than once, you probably tried to create a template tag# templatetags.py#...@register.inclusion_tag('alert.html')defdialog(type,content):return{"type":type,"content":content,}<div class="dialog dialog-{{type}}><div class="dialog-{{type}}-icon"></div><div class="dialog-content">{{content}}</div></div>this would allow you to simply write{%dialog'success''Content goes here!'%}In your django templates, making it much more D.R.Y. The problem here is the inflexibility of what kind of content you can pass. If you want additional markup in the dialog, you're stuck repeating the markup everywhere or assembling markup within python strings and callingmark_safeon it.Usagedjango_template_block_args comes to the rescue by allowing you to pass entire blocks of template to a helper. It provides two functions, the simpler one covers most use-cases. This behaves a lot like django's builtinregister.inclusion_tag. You can think of them as extensions to django's builtininclusion_tagregister_composed_template(register, template_name, [takes_context=False])Working from our dialog example above, here's how we can pass blocks as arguments:# templatetags.pyfromdjango_template_block_argsimportregister_composed_template#...@register_composed_template(register,'alert.html')defdialog(type):return{"type":type,}{%withvar=True%}{%dialog"success"%}Content goes here! No need to escape <strong> HTML </strong>, you can use template tags, filters and even access the surrounding template's context.{%ifvar%}{%dialog"success"%}And recursion!{%enddialog%}{%endif%}{%enddialog%}Note that we didn't pass nor receive thecontextargument as in the first example. That's because the child-block argument will automatically be merged into the target template's context ascontent. Make sure not to call any of your context variables content, because they will be overwritten by this default.register_composed_template_with_blockargs(register, template_name, block_names, [takes_context=False])register_composed_templateonly allows passing a single block, this other function allows passingmultipleblocks, and naming them.This can become useful when you want to populate a template with multiple pieces of text. For instance, we might have a card template component that looks like this:<div class="card card-{{type}}">{%ifcard_header%}<div class="card-header">{{card_header}}</div>{%endif%}<div class="card-body">{{card_body}}</div></div>In order to pass blocks intocard_headerandcard_footer, our python and consumer-template should look like:# templatetags.pyfromdjango_template_block_argsimportregister_composed_template#...@register_composed_template(register,'alert.html',block_names=("card_header","card_body"))defcard(type):# note that block-args automatically get passed to the templatereturn{"type":type}{%card"success"%}{%blockarg'card_body'%}I'm a card-body!{%endblockarg%}{%blockarg'card_header'%}I'm a card-header!{%endblockarg%}{%enddialog%}just like the first example, the header and footer blocks can also contain markup, template tags, and access the surrounding context.Installationpipinstalldjango-template-block-argsThis package consists of 2 simple functions whose only dependencies are built-in django. All you need is to import these functions into your existing templatetags module. No need to change anything in your django settings module.developingThe repo contains the package's module, and an example django projectContributing/Next stepsWrite testsA short list of examples to replace the docs aboveBetter error signalingMake sure people avoid using template content in between a custom-tag and its block-argsshould not use block-names or content in the function