On Mar 12, 2011, at 11:05 AM, pierreth wrote:
>
> Hello,
>
> I have small problem when the characters "< and ">" are used in web2py
> html template views. Using them for Python breaks the html:
>
> {{="OK" if x < 0 else "bad"}}
>
> Because these characters are not escaped in the code, the html file is
> no longer well formed.
>
> Using the html entities "<" and "&gr;" does not solve the problem
> because web2py gives an error when theses characters are used as
> Python code in templates.
>
> Is it possible to fix this to have well formed html for web2py
> templates?
This is a consequence of the way the '=' (request.write) operator behaves at
the beginning of a code block: it treats the entire string as its argument
string, something like: request.write('"OK" if x < 0 else "bad"')
You can rewrite it to use request.write explicitly, or to put each = operator
on its own line.
{{request.write("OK" if x < 0 else "bad")}}
or
{{if x < 0:
="OK"
else:
="bad"
pass
}}
(I think)
When the '=' operator is embedded in a code block, it consumes everything until
the end of the physical line or code block, whichever comes first. When it
begins a code block, it consumes the entire block.