Fixed #18400 Unexpected {% if %} behavior in templates.

https://code.djangoproject.com/ticket/18400

Please review this pull request and leave feedback. If you test this PR, do all the tests succeed?