{"action":"created","issue":{"url":"https://api.github.com/repos/gregmuellegger/django-floppyforms/issues/163","labels_url":"https://api.github.com/repos/gregmuellegger/django-floppyforms/issues/163/labels{/name}","comments_url":"https://api.github.com/repos/gregmuellegger/django-floppyforms/issues/163/comments","events_url":"https://api.github.com/repos/gregmuellegger/django-floppyforms/issues/163/events","html_url":"https://github.com/gregmuellegger/django-floppyforms/issues/163","id":110009160,"number":163,"title":"In rendering context variables that were not provided should be initialized","user":{"login":"patrys","id":81205,"avatar_url":"https://avatars.githubusercontent.com/u/81205?v=3","gravatar_id":"","url":"https://api.github.com/users/patrys","html_url":"https://github.com/patrys","followers_url":"https://api.github.com/users/patrys/followers","following_url":"https://api.github.com/users/patrys/following{/other_user}","gists_url":"https://api.github.com/users/patrys/gists{/gist_id}","starred_url":"https://api.github.com/users/patrys/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/patrys/subscriptions","organizations_url":"https://api.github.com/users/patrys/orgs","repos_url":"https://api.github.com/users/patrys/repos","events_url":"https://api.github.com/users/patrys/events{/privacy}","received_events_url":"https://api.github.com/users/patrys/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-10-06T13:36:50Z","updated_at":"2015-11-30T09:57:53Z","closed_at":null,"body":"Currently only variables that were provided using `with` are passed to the templates. This breaks in two cases:\r\n\r\n1. When an identically named variable exists in the outer scope, it will leak into the inner scope:\r\n\r\n    ```html+django\r\n    {% with label=\"foo\" %}\r\n        {% formrow bar %}\r\n    {% endwith %}\r\n    ```\r\n\r\n    This will output a field labeled “foo” which may be unexpected and very hard to track as the variables could come from a different template file.\r\n\r\n2. When using Django's [`string_if_invalid`](https://docs.djangoproject.com/en/1.8/ref/templates/api/#invalid-template-variables) to track errors in templates, uninitialized variables are no longer considered empty by the `default` filter:\r\n\r\n    ```python\r\n    # settings.py\r\n    TEMPLATES = [{\r\n        'BACKEND': 'django.template.backends.django.DjangoTemplates',\r\n        'DIRS': ...,\r\n        'OPTIONS': {\r\n            'debug': True,\r\n            'context_processors': ...,\r\n            'loaders': ...,\r\n            'string_if_invalid': '<< MISSING VARIABLE \"%s\" >>',\r\n        }\r\n    }]\r\n    ```\r\n\r\n    ```html+django\r\n    {% formrow bar %}\r\n    ```\r\n\r\n    This will output a field labelled “<< MISSING VARIABLE \"label\" >>” and with a help text of “<< MISSING VARIABLE \"help_text\" >>”.\r\n\r\nIf variables like `label`, `help_text` and `classes` were always initialized to `field.label`, `field.help_text` and so on then the row template itself would become simpler:\r\n\r\n```html+django\r\n{% with classes=field.css_classes label=label|default:field.label help_text=help_text|default:field.help_text %}\r\n...\r\n{% endwith %}\r\n```\r\n\r\nThis `with` would be unnecessary."},"comment":{"url":"https://api.github.com/repos/gregmuellegger/django-floppyforms/issues/comments/160581303","html_url":"https://github.com/gregmuellegger/django-floppyforms/issues/163#issuecomment-160581303","issue_url":"https://api.github.com/repos/gregmuellegger/django-floppyforms/issues/163","id":160581303,"user":{"login":"gregmuellegger","id":88278,"avatar_url":"https://avatars.githubusercontent.com/u/88278?v=3","gravatar_id":"","url":"https://api.github.com/users/gregmuellegger","html_url":"https://github.com/gregmuellegger","followers_url":"https://api.github.com/users/gregmuellegger/followers","following_url":"https://api.github.com/users/gregmuellegger/following{/other_user}","gists_url":"https://api.github.com/users/gregmuellegger/gists{/gist_id}","starred_url":"https://api.github.com/users/gregmuellegger/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gregmuellegger/subscriptions","organizations_url":"https://api.github.com/users/gregmuellegger/orgs","repos_url":"https://api.github.com/users/gregmuellegger/repos","events_url":"https://api.github.com/users/gregmuellegger/events{/privacy}","received_events_url":"https://api.github.com/users/gregmuellegger/received_events","type":"User","site_admin":false},"created_at":"2015-11-30T09:57:53Z","updated_at":"2015-11-30T09:57:53Z","body":"Hi, sorry for my late response, I've been on a longer trip with no coding on the plan :)\r\n\r\nI know the trouble you are having with the variables leaking into inner scopes. That's not ideal in some circumstances, but the idea is to mirror the semantics of Django's `{% include %}` template tag. You can isolate the scope by using the `only` keyword just as with the `include` tag, there is an example on this page: http://django-floppyforms.readthedocs.org/en/latest/templatetags.html \r\n\r\nThe issue with the `string_if_invalid` has been discussed quite a while back: https://github.com/gregmuellegger/django-floppyforms/issues/37#issuecomment-6605001 it's a little tricky, since Django's template language behaves odd in a few places if you set the setting. However I only tried this back before the different template engines were introduced. But I'm afraid we are out of luck here to reliably support this setting in all circumstances. Maybe the link from above helps a little.\r\n\r\nRegarding initializing the variables `label`, `help_text` etc: It's true, it would make the template code easier to read. However the goal of the template rendering of floppyforms is to put as much logic into the template in order to keep the rendering out of the python code. And if we pull the initialization into the template tag logic, then we miss out on the explicitness that we have right now in the templates, so that you know where the values come from etc.\r\nAm I a little to puristic here, what do you think?"}}