<%def name="input(label, name, autofocus=None, type='text', cls='', placeholder='', help=None, **kw)">
<%self:group name="${name}">
    <label for="${name}">${label}</label>
    %if help:
    <div class="help-block">${help}</div>
    %endif
    %if hasattr(caller, 'body'):
    ${caller.body()}
    %endif
    <input type="${type}" class="${cls}"
           name="${name}" ${'autofocus="autofocus"' if autofocus else '' | n}
           ${'placeholder="%s"' % placeholder if placeholder else '' | n}
           ${' '.join(['%s="%s"' % (k, v) for k, v in kw.iteritems()]) | n} />
</%self:group>
</%def>

<%def name="radio(label, name, value, autofocus=None, cls='', help=None, checked=False)">
<%self:group name="${name}">
<label class="radio ${cls}">
    <form:error name="field_name" />
    <input name="${name}" value="${value}" type="radio"
           ${'autofocus="autofocus"' if autofocus else '' | n}
           ${'checked="checked"' if checked else '' | n} />
    ${label}
</label>
</%self:group>
</%def>

<%def name="checkbox(label, name, autofocus=None, cls='', help=None, checked=False)">
<%self:group name="${name}">
<label class="checkbox ${cls}">
    <form:error name="field_name" />
    <input name="${name}"  type="checkbox"
           ${'autofocus="autofocus"' if autofocus else '' | n}
           ${'checked="checked"' if checked else '' | n} /> ${label}
    %if help:
    <div class="help-block">${help}</div>
    %endif
</label>
</%self:group>

</%def>

<%def name="checkboxes(label, name, checkboxes, autofocus=None, cls='', placeholder='', help=None)">
<label for="checkbox">
    <input value="${value}" name="${name}" type="checkbox" ${'autofocus="autofocus"' if autofocus else '' | n}/> ${label}
</label>
</%def>

<%def name="textarea(label, name, value, autofocus=None, rows=3, cls='', placeholder='', help=None)">
## This renders a textarea like this:
##
## <label />
## [help] (wrapped into a div.help-block)
## [def:before]
## <textarea>
## [def:after]
##
<%self:group name="${name}">
<label for="${name}">${label}</label>
%if help:
<div class="help-block">${help}</div>
%endif
%if hasattr(caller, 'before'):
${caller.before()}
%endif
<textarea class="${cls}" name="${name}" rows="${rows}"
          ${'autofocus="autofocus"' if autofocus else '' | n}
          ${'placeholder="%s"' % placeholder if placeholder else '' | n}>${value}</textarea>
%if hasattr(caller, 'after'):
${caller.after()}
%endif
${caller.body()}
</%self:group>
</%def>

<%def name="select(label, name, values, autofocus=None, cls='', help=None)">
<%self:group name="${name}">
<label for="${name}">${label}</label>
%if help:
<div class="help-block">${help}</div>
%endif
<select name="${name}" ${'autofocus="autofocus"' if autofocus else '' | n}>
    %for option in values:
    <option value="${option['value']}"
            ${h.literal('selected="selected"') if option.get('selected') else '' | n}>
        ${option['label']}
    </option>
    %endfor
</select>
</%self:group>
</%def>

<%def name="group(name)">
<form:iferror name="${name}"><div class="control-group error"></form:iferror>
<form:iferror name="not ${name}"><div class="control-group"></form:iferror>
${caller.body()}
</div>
</%def>
