comparison sat_templates/default/input/field.html @ 153:b84d20af0ed3

macros (field, form, textbox): general improvments: - use a generic "field" macro for most fields - added "required" class on input labels if suitable - extra kw args are used as input field attributes. Same thing for forms - better handling of id if label is missing - added blog_text macro in textbox for basic text blog input - textbox is more customisable (class and textarea class can be specified)
author Goffi <goffi@goffi.org>
date Thu, 21 Jun 2018 01:09:00 +0200
parents 33c7ce833d3f
children
comparison
equal deleted inserted replaced
152:da2c0dc7c1ff 153:b84d20af0ed3
1 {# macros to create form fields #}
2
3 {% macro field(type, name, label="", required=false) %}
4 {# generic field
5 "class" keyword can be used to add classes
6 additional kwargs will be passed as attributes #}
7 <span class="form_input {{kwargs.pop('class', '')}}">
8 {% set cur_id = name|next_gidx %}
9 {% if label %}
10 <label for="{{cur_id}}" {{'class="required"'|safe if required}}>{{label}}</label>
11 {% endif %}
12 <input id="{{cur_id}}" type="{{type}}" name="{{name}}" {{"required" if required}} {{kwargs|xmlattr}}>
13 </span>
14 {% endmacro %}
15
1 {% macro choices(name, choices_list, checked=none) %} 16 {% macro choices(name, choices_list, checked=none) %}
2 {% for choice, label in choices_list %} 17 {% for choice, label in choices_list %}
3 <div class="form_input"> 18 <div class="form_input {{kwargs.pop('class', '')}}">
4 <input id="{{name|next_gidx}}" type="radio" name="{{name}}" value="{{choice}}"{{" checked" if checked==choice}}><label for="{{name|cur_gidx}}">{{label}}</label> 19 <input id="{{name|next_gidx}}" type="radio" name="{{name}}" value="{{choice}}"{{" checked" if checked==choice}}><label for="{{name|cur_gidx}}">{{label}}</label>
5 </div> 20 </div>
6 {% endfor %} 21 {% endfor %}
7 {% endmacro %} 22 {% endmacro %}
8 23
9 {% macro int(name, label="", init=0) %} 24 {% macro int(name, label="", init=0) %}
10 <span class="form_input"> 25 {{ field("number", name=name, label=label, value=init, step=1, min=0, **kwargs) }}
11 <label for="{{name|next_gidx}}">{{label}}</label><input id="{{name|cur_gidx}}" type="number" name="{{name}}" value="{{init}}" step="1" min="0"> 26 {% endmacro %}
12 </span> 27
28 {% macro checkbox(name, label="", checked=false) %}
29 {% if checked %}
30 {{ field("checkbox", name=name, label=label, checked="checked", **kwargs) }}
31 {% else %}
32 {{ field("checkbox", name=name, label=label, **kwargs) }}
33 {% endif %}
13 {% endmacro %} 34 {% endmacro %}
14 35
15 {% macro text(name, label="", placeholder="", required=false) %} 36 {% macro text(name, label="", placeholder="", required=false) %}
16 {# single line text field 37 {{ field("text", name=name, label=label, required=required, placeholder=placeholder, **kwargs) }}
17 additional kwargs will be passed as attributes #}
18 <span class="form_input">
19 <label for="{{name|next_gidx}}">{{label}}</label><input id="{{name|cur_gidx}}" type="text" name="{{name}}" placeholder="{{placeholder}}" {{"required" if required}} {{kwargs|xmlattr}}>
20 </span>
21 {% endmacro %} 38 {% endmacro %}
22 39
23 {% macro password(name, label="", required=false) %} 40 {% macro password(name, label="", required=false) %}
24 {# password field 41 {{ field("password", name=name, label=label, required=required, **kwargs) }}
25 additional kwargs will be passed as attributes #}
26 <span class="form_input">
27 <label for="{{name|next_gidx}}">{{label}}</label><input id="{{name|cur_gidx}}" type="password" name="{{name}}" {{"required" if required}} {{kwargs|xmlattr}}>
28 </span>
29 {% endmacro %} 42 {% endmacro %}
30 43
31 {% macro email(name, label="", required=false) %} 44 {% macro email(name, label="", required=false) %}
32 {# email field 45 {{ field("email", name=name, label=label, required=required, **kwargs) }}
33 additional kwargs will be passed as attributes #}
34 <span class="form_input">
35 <label for="{{name|next_gidx}}">{{label}}</label><input id="{{name|cur_gidx}}" type="email" name="{{name}}" {{"required" if required}} {{kwargs|xmlattr}}>
36 </span>
37 {% endmacro %} 46 {% endmacro %}
38 47
39 {% macro textarea(name, rows=10, cols=50, placeholder='', required=false) %} 48 {% macro date(name, label="", required=false) %}
40 <textarea name="{{name}}" rows="{{rows}}" cols="{{cols}}" placeholder="{{placeholder}}" {{"required" if required}}></textarea> 49 {{ field("date", name=name, label=label, required=required, **kwargs) }}
50 {% endmacro %}
51
52 {% macro url(name, label="", required=false) %}
53 {{ field("url", name=name, label=label, required=required, **kwargs) }}
54 {% endmacro %}
55
56 {% macro file(name, label="", required=false) %}
57 {{ field("file", name=name, label=label, required=required, **kwargs) }}
58 {% endmacro %}
59
60 {% macro textarea(name, label="", rows=10, cols=50, placeholder='', required=false) %}
61 <div class="form_input {{kwargs.pop('class', '')}}">
62 {% set cur_id = name|next_gidx %}
63 {% if label %}
64 <label for="{{cur_id}}" {{'class="required"'|safe if required}}>{{label}}</label>
65 {% endif %}
66 <textarea id="{{cur_id}}" name="{{name}}" rows="{{rows}}" cols="{{cols}}" placeholder="{{placeholder}}" {{"required" if required}} class="{{kwargs.pop('class', '')}}"></textarea>
67 </div>
41 {% endmacro %} 68 {% endmacro %}
42 69
43 {% macro meta(name, value) %} 70 {% macro meta(name, value) %}
44 <input type="hidden" name="{{name}}" value="{{value}}"> 71 <input type="hidden" name="{{name}}" value="{{value}}">
45 {% endmacro %} 72 {% endmacro %}
46 73
47 {% macro submit(text=_("Send")) %} 74 {% macro submit(text=_("Send")) %}
48 <input class="form_submit button" type="submit" value="{{text}}"> 75 <input class="form_submit button" type="submit" value="{{text}}" class="{{kwargs.pop('class', '')}}">
49 {% endmacro %} 76 {% endmacro %}