Reputation: 18289
Make = <SELECT name="where_make">
% for make in makes:
<OPTION value="{{make}}"
% if make == defaults['make']:
selected="selected"
% end
>{{make}}</option>
%end
How can I do this if statement on a single line?
Upvotes: 2
Views: 4818
Reputation: 473853
Bottle's built-in template engine supports inline if statements:
<option value="{{make}}" {{!'selected="selected"' if make == defaults['make'] else ""}}>{{make}}</option>
Note the exclamation mark before the selected="selected"
- it tells the template engine not to escape quotes.
Demo:
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'])
prints:
Make = <SELECT name="where_make">
<option value="test" selected="selected">test</option>
Hope that helps.
Upvotes: 7