I'm writing a simple web app using Flask, which is hosted on OpenShift. I want to control whitespace in my template files, with the Jinja command lines essentially removed as in:
<div>
{% if True %}
yay
{% endif %}
</div>
...producing:
<div>
yay
</div>
The Jinja docs at http://jinja2.readthedocs.io/en/latest/templates.html#whitespace-control (and other sources) imply I should do
flask_app_obj.jinja_env.lstrip_blocks = True
flask_app_obj.jinja_env.trim_blocks = True
...which I have done, in my init.py file for my app package. However, this yields inconsistent behaviour when I test locally and then on OpenShift; locally, I get the expected result (i.e. the Jinja commands essentially removed, as if they were never there), and on OpenShift I get:
<div>
YAY
</div>
Consulting the docs and researching similar whitespace issues, it seemed there could be two causes of the inconsistency. Either there is something strange going on with newlines, with my local machine using a different scheme compared to the hosted OpenShift environment (along the lines of https://superuser.com/questions/374028/how-are-n-and-r-handled-differently-on-linux-and-windows) and this produces different whitespace-culling behaviour when the template is generated at the OpenShift end, or, I am changing the Jinja2 environment object when it is shared or after a template has been loaded, which violates the docs which quote:
Instances of this class may be modified if they are not shared and if no template was loaded so far. Modifications on environments after the first template was loaded will lead to surprising effects and undefined behaviour.
... along the lines of Stripping whitespace in jinja2 & flask...why do I still need the minus sign? and Trimming blocks using whitespace control from jinja2 template.
Plausible causes, but I am in doubt they are the reason for the strange OpenShift-side template behaviour. If I had operated on the Jinja environment too early, surely I would see incorrect template output locally as well (which I don't, I get the expected result locally). Indeed, it's in my init.py file that I set lstrip_blocks and trim_blocks to True. I doubt it's a newline issue too since I am developing on OS X, and the OpenShift environment is *nix, so both environments should be using the same newline scheme.
Given the above, I have no more theories as to why I could be getting the inconsistent results between OpenShift testing and local testing. So my questions are these: Have I set the Jinja options in the wrong place and they should be somewhere other than init.py? Am I modifying the Jinja2 Environment inappropriately? Is my method of setting Jinja2 options just plain wrong? Perhaps I should be using some other avenue to set such options other than
flask_app_obj.jinja_env.lstrip_blocks = True
flask_app_obj.jinja_env.trim_blocks = True
Thanks for answering.