Python Bottle - Inline IF statements in Template
Question: 
    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?


Answer: Bottle's built-in template engine supports [inline if
statements](http://bottlepy.org/docs/dev/stpl.html#inline-expressions):

    
    
    <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.

