funkybob / django-sniplates

Template snippet libraries for Django
MIT License
57 stars 18 forks source link

nested_widget enhancement (with code) #66

Open davidmcnabnz opened 4 years ago

davidmcnabnz commented 4 years ago

Submitting patched version of sniplates.py (0.7.0) for your consideration for future releases.

Intent: to overcome 2 issues with 'nested_widget':

  1. The name was not properly communicating its semantics/capabilities, and
  2. It was not supporting injection of named blocks, only named variables. Named variables can prove awkward for injecting significant chunks of content.

Effects of modification:

  1. Preserve backward compatibility
  2. Introduce a new tag 'widblock'/'endwidblock', which is synonymous to 'nested_widget'/'endnested' but more concise, and uses the same NestedWidget class as nested_widget
  3. Enhance NestedWidget to support context injection via named blocks inside a widblock (and nested_widget) body.

How this is done, in NestedWidget.render(), is:

  1. pick out the block objects from the nodelist, render and save them separately
  2. inject these block objects into the context under named keys, in the same way as the body content in the node gets injected under the key "content"
  3. render the tag body into context key 'content' the same way as before, but without the nested blocks

Example usage:

{% widblock 'myapp:foo' %} {% block introText %} ...add styled intro text, with links... {% endblock %} {% block bodyGraph %} ...include a graph template... {% endblock %} ... optional body of this widget block, to be injected as variable 'content' ... {% endwidblock %}

Thank you for your effort in developing, sharing and maintaining sniplates. I look forward to making heavy use of it in my current and future projects.

Cheers David (aum108 on freenode)

sniplatesCustom.py.txt