Make = <SELECT name="where_make">
% for make in makes:
<OPTION value="{{make}}"
% if make == defaults['make']:
selected="selected"
% end
>{{make}}</option>
%end
如果单行上的语句怎么办呢?
答案 0 :(得分:6)
Bottle的内置模板引擎支持inline if statements:
<option value="{{make}}" {{!'selected="selected"' if make == defaults['make'] else ""}}>{{make}}</option>
注意selected="selected"
之前的感叹号 - 它告诉模板引擎不要转义引号。
演示:
from bottle import SimpleTemplate
tpl = SimpleTemplate("""Make = <SELECT name="where_make">
% for make in makes:
<option value="{{make}}" {{!'selected="selected"' if make == defaults['make'] else ""}}>{{make}}</option>
%end""")
print tpl.render(make='test', defaults={'make': 'test'}, makes=['test'])
打印:
Make = <SELECT name="where_make">
<option value="test" selected="selected">test</option>
希望有所帮助。