Skip to content

Commit

Permalink
[2474] Add form.prepend() macro and added it to add group form
Browse files Browse the repository at this point in the history
Also added documentation to form macros.
  • Loading branch information
aron committed May 30, 2012
1 parent 78c12fe commit 50f95d1
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 1 deletion.
2 changes: 1 addition & 1 deletion ckan/templates/group/partials/group_form.html
Expand Up @@ -14,7 +14,7 @@ <h2>{{ _('Errors in form') }}</h2>
{% endif %}

{{ form.input('title', label=_('Title'), id='field-title', placeholder=_('My Group'), data=data, errors=errors, classes=['control-full']) }}
{{ form.input('name', label=_('URL'), id='field-url', placeholder=_('my-group'), data=data, errors=errors, classes=['control-full']) }}
{{ form.prepend('name', label=_('URL'), prepend=h.url(controller='group', action='index') ~ '/', id='field-url', placeholder=_('my-group'), data=data, errors=errors) }}
{{ form.markdown('description', label=_('Description'), id='field-description', placeholder=_('A little information about my group...'), data=data, errors=errors) }}
{{ form.input('image_url', label=_('Image URL'), id='field-url', type='url', placeholder=_('http://example.com/my-image.jpg'), data=data, errors=errors, classes=['control-full']) }}
{% if c.is_sysadmin or c.auth_for_change_state %}
Expand Down
94 changes: 94 additions & 0 deletions ckan/templates/macros/form.html
@@ -1,3 +1,24 @@
{#
Creates all the markup required for an input element. Handles matching labels to
inputs, error messages and other useful elements.

name - The name of the form parameter.
id - The id to use on the input and label. Convention is to prefix with 'field-'.
label - The human readable label.
value - The value of the input. The alternative is to pass in the data dict
which will use the name key as the value.
placeholder - Some placeholder text.
type - The type of input eg. email, url, date (default: text).
data - The data dict, used to look up the value using the name.
errors - The errors dict, used to look up the error using the name.
classes - An array of classes to apply to the control-group.

Examples:

{% import form from 'macros/form.html' %}
{{ form.input('title', label=_('Title'), data=data, errors=errors) }}

#}
{% macro input(name, id='', label='', value='', placeholder='', type='text', data={}, errors={}, classes=[]) %}
<div class="control-group{% if classes %} {{ classes | join(' ') }}{% endif %}" py:with="error = errors.get('title', '')">
<label class="control-label" for="{{ id or name }}">{{ label or name }}</label>
Expand All @@ -8,6 +29,24 @@
</div>
{% endmacro %}

{#
Creates all the markup required for an select element. Handles matching labels to
inputs and error messages.

name - The name of the form parameter.
id - The id to use on the input and label. Convention is to prefix with 'field-'.
label - The human readable label.
options - A dict of value/label pairs to be used as <options>.
selected - The value of the selected <option>.
errors - The errors dict, used to look up the error using the name.
classes - An array of classes to apply to the control-group.

Examples:

{% import form from 'macros/form.html' %}
{{ form.select('year', label=_('Year'), options={2010: 2010, 2011: 2011}, selected=2011, errors=errors) }}

#}
{% macro select(name, id='', label='', options='', selected='', errors={}, classes=[]) %}
<div class="control-group{% if classes %} {{ classes | join(' ') }}{% endif %}" py:with="error = errors.get('title', '')">
<label class="control-label" for="{{ id or name }}">{{ label or name }}</label>
Expand All @@ -22,6 +61,26 @@
</div>
{% endmacro %}

{#
Creates all the markup required for a Markdown textarea element. Handles
matching labels to inputs, selected item and error messages.

name - The name of the form parameter.
id - The id to use on the input and label. Convention is to prefix with 'field-'.
label - The human readable label.
value - The value of the input. The alternative is to pass in the data dict
which will use the name key as the value.
placeholder - Some placeholder text.
data - The data dict, used to look up the value using the name.
errors - The errors dict, used to look up the error using the name.
classes - An array of classes to apply to the control-group.

Examples:

{% import form from 'macros/form.html' %}
{{ form.markdown('desc', id='field-description', label=_('Description'), data=data, errors=errors) }}

#}
{% macro markdown(name, id='', label='', value='', placeholder='', data={}, errors={}, classes=[]) %}
<div class="control-group control-full{% if classes %} {{ classes | join(' ') }}{% endif %}" py:with="error = errors.get('title', '')">
<label class="control-label" for="{{ id or name }}">{{ label or name }}</label>
Expand All @@ -32,3 +91,38 @@
</div>
</div>
{% endmacro %}

{#
Creates all the markup required for an input element with a prefixed segment.
These are useful for showing url slugs and other fields where the input
information forms only part of the saved data.

name - The name of the form parameter.
id - The id to use on the input and label. Convention is to prefix with 'field-'.
label - The human readable label.
prepend - The text that will be prepended before the input.
value - The value of the input. The alternative is to pass in the data dict
which will use the name key as the value.
placeholder - Some placeholder text.
data - The data dict, used to look up the value using the name.
errors - The errors dict, used to look up the error using the name.
classes - An array of classes to apply to the control-group.

Examples:

{% import form from 'macros/form.html' %}
{{ form.prepend('slug', id='field-slug', prepend='/dataset/', label=_('Slug'), data=data, errors=errors) }}

#}
{% macro prepend(name, id='', label='', prepend='', value='', placeholder='', type='text', data={}, errors={}, classes=[]) %}
<div class="control-group{% if classes %} {{ classes | join(' ') }}{% endif %}" py:with="error = errors.get('title', '')">
<label class="control-label" for="{{ id or name }}">{{ label or name }}</label>
<div class="controls">
<div class="input-prepend">
{% if prepend %}<span class="add-on">{{ prepend }}</span>{%- endif -%}
<input id="{{ id or name }}" name="{{ name }}" value="{{ data[name] or value | e }}" placeholder="{{ placeholder }}" />
</div>
{% if errors[name] %}<span class="error-block">{{ errors[name] }}</span>{% endif %}
</div>
</div>
{% endmacro %}

0 comments on commit 50f95d1

Please sign in to comment.