{ "info": { "author": "AlexCLeduc", "author_email": "", "bugtrack_url": null, "classifiers": [ "License :: OSI Approved :: MIT License", "Operating System :: OS Independent", "Programming Language :: Python :: 3" ], "description": "# `django_template_block_args`\n\n\n## Motivation\n\nThis tool makes it easier to \"componentize\" your markup. Specifically, it makes it easier to create template tags that receive *blocks* as arguments. \n\n\nWe've all written dialog markup that looks like this:\n\n```django\n
\n
\n
\n Content goes here!\n
\n
\n```\n\nif you've written this more than once, you probably tried to create a template tag\n\n\n```python\n# templatetags.py\n#...\n@register.inclusion_tag('alert.html')\ndef dialog(type,content):\n return { \n \"type\": type,\n \"content\": content,\n }\n\n```\n\n```django\n
\n
\n
\n
{{content}}
\n
\n```\n\nthis would allow you to simply write \n```django\n{% dialog 'success' 'Content goes here!' %} \n```\n\nIn 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 calling `mark_safe` on it. \n\n## Usage\n\ndjango_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 builtin `register.inclusion_tag`. You can think of them as extensions to django's builtin `inclusion_tag`\n\n### `register_composed_template(register, template_name, [takes_context=False])`\n\nWorking from our dialog example above, here's how we can pass blocks as arguments: \n\n```python\n# templatetags.py\nfrom django_template_block_args import register_composed_template\n#...\n@register_composed_template(register,'alert.html')\ndef dialog(type):\n return { \n \"type\": type,\n }\n\n```\n\n```django\n{% with var=True %}\n{% dialog \"success\" %}\n Content goes here! No need to escape HTML , you can use template tags, filters and even access the surrounding template's context. \n {% if var %}\n {% dialog \"success\" %} And recursion! {% enddialog %}\n {% endif %}\n{% enddialog %}\n```\n\nNote that we didn't pass nor receive the `context` argument as in the first example. That's because the child-block argument will automatically be merged into the target template's context as `content`. Make sure not to call any of your context variables content, because they will be overwritten by this default. \n\n\n### `register_composed_template_with_blockargs(register, template_name, block_names, [takes_context=False])`\n\n`register_composed_template` only allows passing a single block, this other function allows passing *multiple* blocks, and naming them.\n\nThis 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:\n\n```django\n
\n {% if card_header %}\n
\n {{card_header}}\n
\n {% endif %}\n
\n {{card_body}}\n
\n
\n\n```\n\nIn order to pass blocks into `card_header` and `card_footer`, our python and consumer-template should look like:\n\n```python\n# templatetags.py\nfrom django_template_block_args import register_composed_template\n#...\n@register_composed_template(register,'alert.html', block_names=(\"card_header\", \"card_body\"))\ndef card(type):\n # note that block-args automatically get passed to the template\n return {\"type\":type}\n\n```\n\n```django\n{% card \"success\" %}\n {% blockarg 'card_body' %}\n I'm a card-body!\n {% endblockarg %}\n {% blockarg 'card_header' %}\n I'm a card-header!\n {% endblockarg %}\n{% enddialog %}\n```\n\njust like the first example, the header and footer blocks can also contain markup, template tags, and access the surrounding context.\n\n\n## Installation\n\n```bash\npip install django-template-block-args\n```\n\nThis 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.\n\n\n## developing \n\n1. The repo contains the package's module, and an example django project \n\n\n## Contributing/Next steps\n1. Write tests\n2. A short list of examples to replace the docs above \n3. Better error signaling\n * Make sure people avoid using template content in between a custom-tag and its block-args\n * should not use block-names or content in the function", "description_content_type": "text/markdown", "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/AlexCLeduc/django-template-block-arg", "keywords": "", "license": "", "maintainer": "", "maintainer_email": "", "name": "django-template-block-args", "package_url": "https://pypi.org/project/django-template-block-args/", "platform": "", "project_url": "https://pypi.org/project/django-template-block-args/", "project_urls": { "Homepage": "https://github.com/AlexCLeduc/django-template-block-arg" }, "release_url": "https://pypi.org/project/django-template-block-args/0.0.5/", "requires_dist": null, "requires_python": "", "summary": "", "version": "0.0.5" }, "last_serial": 5692930, "releases": { "0.0.4": [ { "comment_text": "", "digests": { "md5": "049fe11384974565f9685abf1d8c380d", "sha256": "3d3dcf5f88f7e2f3bf30d8ab6f834fe6f2acce1a70ade5272e75ec03558db7c1" }, "downloads": -1, "filename": "django-template-block-args-0.0.4.tar.gz", "has_sig": false, "md5_digest": "049fe11384974565f9685abf1d8c380d", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 6190, "upload_time": "2019-08-17T17:17:23", "url": "https://files.pythonhosted.org/packages/40/c2/5fae382c6716675cc76f1ff9789ef670497bdde8120fb0e37601664e6127/django-template-block-args-0.0.4.tar.gz" } ], "0.0.5": [ { "comment_text": "", "digests": { "md5": "e6e4084c852cd5182ee291be34e4fde3", "sha256": "66d73bf07dbba467682eabc09e0bcd774a548037aa71e65401c00c8b1088e97c" }, "downloads": -1, "filename": "django-template-block-args-0.0.5.tar.gz", "has_sig": false, "md5_digest": "e6e4084c852cd5182ee291be34e4fde3", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 6116, "upload_time": "2019-08-17T22:36:25", "url": "https://files.pythonhosted.org/packages/75/3c/e4ae50ab1033758f3d415ad62686f0a0260865c15b93c7f079e8a90378c7/django-template-block-args-0.0.5.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "e6e4084c852cd5182ee291be34e4fde3", "sha256": "66d73bf07dbba467682eabc09e0bcd774a548037aa71e65401c00c8b1088e97c" }, "downloads": -1, "filename": "django-template-block-args-0.0.5.tar.gz", "has_sig": false, "md5_digest": "e6e4084c852cd5182ee291be34e4fde3", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 6116, "upload_time": "2019-08-17T22:36:25", "url": "https://files.pythonhosted.org/packages/75/3c/e4ae50ab1033758f3d415ad62686f0a0260865c15b93c7f079e8a90378c7/django-template-block-args-0.0.5.tar.gz" } ] }