{ "info": { "author": "tell-k", "author_email": "ffk2005@gmail.com", "bugtrack_url": null, "classifiers": [ "Environment :: Web Environment", "Framework :: Django", "Framework :: Django :: 1.10", "Framework :: Django :: 1.11", "Framework :: Django :: 2.0", "Intended Audience :: Developers", "License :: OSI Approved :: MIT License", "Operating System :: OS Independent", "Programming Language :: Python", "Programming Language :: Python :: 3", "Programming Language :: Python :: 3.4", "Programming Language :: Python :: 3.5", "Programming Language :: Python :: 3.6" ], "description": "We often want to render different HTML templates for phones, tablets, and desktop browsers. Or for AB testing. ``django-variatmpl`` make it easy. By setting ``request.variant``, you can render the template according to that ``request.variant``. This library is heavily inspired by `Action Pack Variants `_.\n\n\n|travis| |coveralls| |version| |license|\n\nQuick start\n=============\n\n1. Install ``django-variantmpl``\n\n.. code-block:: bash\n\n $ pip install django-variantmpl\n\n2. Change ``django.shortcuts.render`` to ``variantmpl.shortcuts.render`` in your views.\n\n* And set ``request.variant`` property.\n\n.. code-block:: python\n\n # views.py --\n\n # from django.shortcuts import render\n from variantmpl.shortcuts import render # <- add\n\n def sample(request):\n\n # Set variant value\n request.variant = 'v2'\n\n return render(request, 'index.html')\n\n3. Prepare variant templates.\n\n.. code-block:: bash\n\n $ echo 'sample v1' > templates/index.html\n $ echo 'sample v2' > templates/index+v2.html\n\n4. Confirm ``views.sample`` display in your browser.\n\n* You can see **sample v2**. \n* It is the result of loading the template(``index+v2.html``) based on ``request.variant``.\n\nFeatures\n=========\n\nrender\n--------\n\nUse instead of ``django.shortcuts.render``.\n\n.. code-block:: python\n\n # views.py --\n\n from variantmpl.shortcuts import render\n\n def sample(request):\n request.variant = 'v2'\n\n # Actually \"index+v2.html\" is rendered\n return render(request, 'index.html')\n\n\nrender_to_response\n--------------------\n\nUse instead of ``django.shortcuts.render_to_response``.\n\n.. code-block:: python\n\n # views.py --\n\n from variantmpl.shortcuts import render_to_response\n\n def sample(request):\n\n # Actually \"index+v2.html\" is rendered\n return render_to_response(request, 'index.html', variant='v2')\n\nYou can set ``variant`` as a keyword argument.\n\nrender_to_string\n--------------------\n\nUse instead of ``django.template.loader.render_to_string``.\n\n.. code-block:: python\n\n # views.py --\n\n from django.http import HttpResponse\n\n from variantmpl.template.loader import render_to_string\n\n def sample(request):\n request.variant = 'v2'\n\n # Actually \"index+v2.html\" is rendered\n content = render_to_string('index.html', request=request)\n return HttpResponse(content)\n\n\nTemplateResponse\n--------------------\n\nUse instead of ``django.template.response.TemplateResponse``.\n\n.. code-block:: python\n\n # views.py --\n\n from django.views.generic import TemplateView\n from variantmpl.template.response import TemplateResponse\n\n class SampleView(TemplateView):\n template_name = 'sample/index.html'\n response_class = TemplateResponse # Replace response class\n\n def get(self, request, **kwargs):\n request.variant = 'v2'\n\n # Actually \"index+v2.html\" is rendered\n return super().get(request, **kwargs)\n\n sample = SampleView.as_view()\n\nMonkey patching Django's functions/classes\n-----------------------------------------------\n\nIt is difficult to rewrite all code with large codes already to ``variantmpl`` code. In such a case, you can apply Monkey patch to Django's functions/classes.\n\n**Caution** : This feature is experimental. This may be deleted in the future if unexpected bad effects occur.\n\n.. code-block:: python\n\n # settings.py --\n\n SECRET_KEY = 'xxxxxx'\n\n # You must write this code below SECRET_KEY.\n from variantmpl import monkey\n monkey.patch_all()\n\n.. code-block:: python\n\n # views.py --\n\n # You don't need to replace to 'variantmpl'.\n from django.shortcuts import render\n\n def sample(request):\n request.variant = 'v2'\n\n # Actually \"index+v2.html\" is rendered\n return render(request, 'index.html')\n\nAll targets for monkey patching.\n\n.. code-block::\n\n django.shortcuts.render\n django.shortcuts.render_to_response\n django.template.loader.render_to_string\n django.template.response.TemplateResponse.resolve_template\n\n They are replaced by the functions/methods of the same name in `variantmpl`.\n\n\nConfiguration\n===============\n\nVARIANTMPL_VARIANT_FORMAT\n-----------------------------------\n\nYou can change ``variant`` format. default: ``+variant``.\n\n.. code-block:: python\n\n # settings.py --\n VARIANTMPL_VARIANT_FORMAT = '@{variant}'\n\n::\n\n # The lookup target template name changes as follows.\n\n \"index+variant.html\" -> \"index@variant.html\"\n\n\nVARIANTMPL_PROPERTY_NAME\n-----------------------------------\n\nYou can rename ``request.variant`` property.\n\n.. code-block:: python\n\n # settings.py --\n VARIANTMPL_PROPERTY_NAME = 'mutation'\n\n.. code-block:: python\n\n # You can set 'mutation' instead of 'varaiant'\n request.mutation = 'v2'\n\n\nVARIANTMPL_TEMPLATE_FORMAT\n-----------------------------------\n\nYou can change the position of the variant inserted into template path.\n\n.. code-block:: python\n\n # For example, you have this path.\n render('sample1/sample2/index.html')\n\n # variantmpl inserts the variant(v2) as follows.\n 'sample1/sample2/index+v2.html'\n\n # At this time, VARIANTMPL_TEMPLATE_FORMAT is like this. (default)\n VARIANTMPL_TEMPLATE_FORMAT = '{dirpath}{filename}{variant}.{ext}'\n dirpath # => 'sample1/sample2/'\n filename # => 'index'\n variant # => '+v2'\n ext # => 'html'\n\nChange this format like this.\n\n.. code-block:: python\n\n VARIANTMPL_TEMPLATE_FORMAT = '{variant}/{dirpath}{filename}.{ext}'\n\n # variantmpl inserts the variant(v2) as follows.\n '+v2/sample1/sample2/index.html'\n\nIn this case templates layout will change as follows\n\n::\n\n templates\n \u251c\u2500\u2500 +v2\n \u2502\u00a0\u00a0 \u2514\u2500\u2500 sample1\n \u2502\u00a0\u00a0 \u2514\u2500\u2500 sample2\n \u2502\u00a0\u00a0 \u2514\u2500\u2500 index.html\n \u2514\u2500\u2500 sample1\n \u2514\u2500\u2500 sample2\n \u2514\u2500\u2500 index.html\n\n\nPython and Django Support\n=========================\n\n* Python 3.4 later\n* Django 1.10 later\n* Support only the latest 3 versions.\n\nLicense\n=======\n\nMIT Licence. See the LICENSE file for specific terms.\n\nHistory\n=======\n\n0.1.0(12 26, 2017)\n---------------------\n* First release\n\n.. |travis| image:: https://travis-ci.org/tell-k/django-variantmpl.svg?branch=master\n :target: https://travis-ci.org/tell-k/django-variantmpl\n\n.. |coveralls| image:: https://coveralls.io/repos/tell-k/django-variantmpl/badge.png\n :target: https://coveralls.io/r/tell-k/django-variantmpl\n :alt: coveralls.io\n\n.. |version| image:: https://img.shields.io/pypi/v/django-variantmpl.svg\n :target: http://pypi.python.org/pypi/django-variantmpl/\n :alt: latest version\n\n.. |license| image:: https://img.shields.io/pypi/l/django-variantmpl.svg\n :target: http://pypi.python.org/pypi/django-variantmpl/\n :alt: license\n\n\n", "description_content_type": null, "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/tell-k/django-variantmpl", "keywords": "django templates switch useragent", "license": "MIT", "maintainer": "", "maintainer_email": "", "name": "django-variantmpl", "package_url": "https://pypi.org/project/django-variantmpl/", "platform": "", "project_url": "https://pypi.org/project/django-variantmpl/", "project_urls": { "Homepage": "https://github.com/tell-k/django-variantmpl" }, "release_url": "https://pypi.org/project/django-variantmpl/0.1.0/", "requires_dist": [ "Django" ], "requires_python": "", "summary": "Provide a mechanism for Django that switching template based on request context.", "version": "0.1.0" }, "last_serial": 3442855, "releases": { "0.1.0": [ { "comment_text": "", "digests": { "md5": "e5af78534e32ec0e2de743db082b70cc", "sha256": "28cbb33ef9d7db1ec3b1e7ac342d1ef5ec4340c79b07198df296136b32836d12" }, "downloads": -1, "filename": "django_variantmpl-0.1.0-py3-none-any.whl", "has_sig": false, "md5_digest": "e5af78534e32ec0e2de743db082b70cc", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 9472, "upload_time": "2017-12-26T07:53:31", "url": "https://files.pythonhosted.org/packages/4d/58/02ea6c085df5993b573c505636a72e8589b8d02b9245f1085583e21aa229/django_variantmpl-0.1.0-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "1f1e03b2ceffaf1560996298dc5cc85a", "sha256": "18474b9d5a0770835e335cd651bce7b6f4ae8799be4a019a40680b03fc7825d4" }, "downloads": -1, "filename": "django-variantmpl-0.1.0.tar.gz", "has_sig": false, "md5_digest": "1f1e03b2ceffaf1560996298dc5cc85a", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 9000, "upload_time": "2017-12-26T07:53:32", "url": "https://files.pythonhosted.org/packages/f6/4a/e6b0654bc6b13b8ee4d96c91d6d10025b7eb594e4ad8d9cc3bc30168b211/django-variantmpl-0.1.0.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "e5af78534e32ec0e2de743db082b70cc", "sha256": "28cbb33ef9d7db1ec3b1e7ac342d1ef5ec4340c79b07198df296136b32836d12" }, "downloads": -1, "filename": "django_variantmpl-0.1.0-py3-none-any.whl", "has_sig": false, "md5_digest": "e5af78534e32ec0e2de743db082b70cc", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 9472, "upload_time": "2017-12-26T07:53:31", "url": "https://files.pythonhosted.org/packages/4d/58/02ea6c085df5993b573c505636a72e8589b8d02b9245f1085583e21aa229/django_variantmpl-0.1.0-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "1f1e03b2ceffaf1560996298dc5cc85a", "sha256": "18474b9d5a0770835e335cd651bce7b6f4ae8799be4a019a40680b03fc7825d4" }, "downloads": -1, "filename": "django-variantmpl-0.1.0.tar.gz", "has_sig": false, "md5_digest": "1f1e03b2ceffaf1560996298dc5cc85a", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 9000, "upload_time": "2017-12-26T07:53:32", "url": "https://files.pythonhosted.org/packages/f6/4a/e6b0654bc6b13b8ee4d96c91d6d10025b7eb594e4ad8d9cc3bc30168b211/django-variantmpl-0.1.0.tar.gz" } ] }