I use a variable called x,the x is not defined, and use x to compare with a number in mako template:
%if x>5:
<h1>helloworld</h1>
%endif
And why this sentence not cause an exception or an error? But when I want to print this out:
%if x>5:
<h1>${x}</h1>
%endif
it caused an exception. Why?
This is in mako. Why can’t I use this sentence in IPython? Because if I use an undefined variable in IPython, it will tell me variable is not defined suddenly.
That’s because
makouses by default anUndefinedobject that only fails when rendered, but can be used in boolean expressions because implements the__nonzero__method:To use an undefined value that fails even in boolean expressions, you can use
strict_undefinedargument as follows:Note that
strict_undefinedis available in bothmako.template.Templateandmako.lookup.TemplateLookup.The description from the documentation is: