Skip to content

Instantly share code, notes, and snippets.

@jshirley
Created April 19, 2012 19:41
Show Gist options
  • Save jshirley/2423625 to your computer and use it in GitHub Desktop.
Save jshirley/2423625 to your computer and use it in GitHub Desktop.
[% MACRO form_block BLOCK %]
[% context.scope = 'guide' %]
[% text_field({ label => 'Your Name', name => 'name',
value => 'This is focused', placeholder => 'Placeholder Text',
required => 1 }) %]
[% text_field({ label => 'Prepended', name => 'prepended_name', prepend => '←',
value => 'This is focused', placeholder => 'Placeholder Text',
required => 1,
#support => 'Support text', warning => 'And a warning'
}) %]
[% text_field({ label => 'Appended', name => 'appended_name',
append => '→',
value => 'Trailing', placeholder => 'Placeholder Text',
required => 1,
support => 'Support text', warning => 'And a warning'
}) %]
[% text_field({ label => 'Going both ways', name => 'app_pre_name',
classes => [ 'span2' ],
prepend => '←', append => '→',
value => 'Neat', placeholder => 'Placeholder Text',
required => 1,
support => 'Support text', error => 'Pick a direction'
}) %]
[% select_field({ label => 'A Select', name => 'select',
value => 1,
array => [ [ 0, 'Zero' ], [ 1, 'One' ], [ 2, 'Two' ] ],
value_method => '0', label_method => '1',
required => 1 }) %]
[% readonly_field({ label => 'Readonly', name => 'readonly_name',
value => 'This is read-only' }) %]
[% password_field({ label => 'A Password', name => 'password' }) %]
[% textarea_field({ label => 'Block of Text', name => 'textarea',
value => 'This is a textbox. You should have noticed.',
success => 'With a message',
support => 'field_support' }) %]
[% checkboxes({ label => 'And checkboxes', name => 'checkboxes',
array => [ [ 0, 'Zero' ], [ 1, 'One' ], [ 2, 'Two' ] ],
value_method => '0', label_method => '1',
error => 'Oh no. Failed.'
}) %]
[% text_field({ label => 'Disabled', name => 'disabled', value => 'This is disabled', disabled => 1 }) %]
[% text_field({ label => 'Warning', name => 'has_warning',
value => 'This has a warning', warning => 'Something may be wrong', }) %]
[% text_field({ label => 'Error', name => 'has_error',
value => 'This has an error', error => 'Something did go wrong', }) %]
[% text_field({ label => 'Success', name => 'has_success',
value => 'This is successful', success => 'Celebrate good times', }) %]
[% form_actions([
{ label => 'Save changes' }, # Defaults to submit
{ label => 'Cancel', type => 'cancel' }
]) %]
[% END %]
<form method="post" class="form-vertical">
<fieldset>
<legend>Vertical Form</legend>
[% form_block %]
</fieldset>
</form>
<form class="form-horizontal" method="post">
<fieldset>
<legend>Horizontal Form</legend>
[% form_block %]
</fieldset>
</form>
[%~
USE time_dir = Time.Duration;
MACRO system_localize(str) BLOCK;
IF str.match('^\@'); c.loc(str); ELSE; str | html; END;
END;
MACRO pretty_date(dt) BLOCK;
SET now_secs = now.epoch;
SET then_secs = dt.epoch || now_secs;
IF(now_secs > then_secs);
time_dir.ago(now_secs - then_secs);
ELSE;
time_dir.from_now(then_secs - now_secs);
END;
END;
MACRO form_actions(actions) BLOCK;
IF !actions; actions = [ { } ]; END;
%]
<div class="form-actions">
[% FOREACH action IN actions;
IF action.type == 'cancel' %]
<button class="btn">[% c.loc(action.label || 'Cancel') %]</button>
[% ELSE %]
<button class="btn btn-primary">[% c.loc(action.label || 'Save') %]</button>
[% END %]
[% END %]
</div>[%
END;
MACRO label_field(info) BLOCK;
IF !info.error && stack && stack.count;
error_name = info.name;
IF context.scope;
error_name = error_name.replace("${context.scope}.", "");
END;
info.error = c.loc( stack.for_subject(error_name).for_level('error').first_message.id );
END %]
<label class="control-label" for="[% info.id %]"
[%~ IF info.tooltip %] data-tooltip="[% info.tooltip | html %]"[% END ~%]
>
[%~ c.loc(info.label, info.params) ~%]
</label>
[% END;
MACRO readonly_field(info) BLOCK;
IF info.filter && info.value;
f = info.filter;
IF f == "join";
info.value = info.value.join(', ');
ELSE;
TRY; info.value = FILTER $f; info.value; END; CATCH; "Error!"; END;
END;
END;
%]
<div class="control-group[% IF info.error %] error[% ELSIF info.warning %] warning[% ELSIF info.success %] success[% END %]">
[% label_field(info) %]
<div class="controls ro">
<input type="hidden" id="form_[% info.name %]" name="[% info.name %]" value="[% info.value | html %]"/><span>
<span class="uneditable-input">[% info.value %]</span>
[% IF info.link || info.required || info.error || info.success || info.warning %]
<span class="help-inline">
[% IF info.required %]<span class="req">[% c.loc('FIELD REQUIRED SPLAT') %]</span>[% END; info.error; info.warning; info.success %]
[%~ IF info.link %]<a href="[% info.link %]">[%~ END ~%]
</span>
[% END %]
[% IF info.support %]<p>[% c.loc(info.support) %]</p>[% END %]
[%~ IF info.link ~%]</a>[%~ END ~%]</span>
</div>
</div>
[% END;
MACRO select_field(info) BLOCK;
IF !info.value && results.${context.scope};
value_name = info.name;
IF context.scope; value_name = value_name.replace("${context.scope}.", ""); END;
info.value = results.${context.scope}.get_original_value(value_name);
END;
IF !info.error && messages.for_scope(context.scope);
value_name = info.name;
IF context.scope; value_name = value_name.replace("^${context.scope}.", ""); END;
info.error = messages.for_scope(context.scope).for_subject(value_name).first_message.id;
END;
%]
[%~ IF info.dynamic_array =%]<script type="text/javascript">if ( typeof dynamic_forms === 'undefined' ) dynamic_forms = {}; dynamic_forms['[% info.value_from %]'] = { 'source': '[% info.dynamic_array %]', 'update': '[% info.name %]', 'value': "[% info.value || html %]" };</script>[% IF info.array.size == 0; info.array.push(c.loc('Please select [_1]', [ info.value_from ] ) ); END; END %]
<div class="control-group[% IF info.error %] error[% ELSIF info.warning %] warning[% ELSIF info.success %] success[% END %]">
[% label_field(info) %]
<div class="select controls">
<select id="form_[% info.name %]" name="[% info.name %]"
[% IF info.classes %] class="[% info.classes.join(' ') %]"[%~ END ~%]>
[% IF info.disabled %] disabled="disabled"[%~ END ~%]>
[% IF info.default_option %]<option value="[% info.default_option.0 | html %]">[% info.default_option.1 %]</option>[% END %]
[% FOREACH item = info.array;
IF !c.tt_blessed(item) && item.keys %]
<optgroup label="[% item.keys.0 %]">
[% FOREACH item = item.values.0;
IF info.value_method;
SET this_value = item.${info.value_method};
ELSE;
SET this_value = item;
END;
IF info.label_method;
SET this_name = item.${info.label_method};
ELSE;
SET this_name = item;
END;
%]<option value="[% this_value | html %]" [% IF this_value == info.value %]selected="selected"[% END %] >[% c.loc(this_name); " " %]</option>
[% END %]
</optgroup>
[% ELSE;
IF info.defined('value_method');
SET this_value = item.${info.value_method};
ELSE;
SET this_value = item;
END;
IF info.defined('label_method');
SET this_name = item.${info.label_method};
ELSE;
SET this_name = item;
END;
%]<option value="[% this_value | html %]" [% IF this_value == info.value %]selected="selected"[% END %]>[% c.loc(this_name) %]</option>
[% END %]
[% END %]
</select>
[% IF info.required || info.error || info.success || info.warning %]
<span class="help-inline">[% IF info.required %]<span class="req">[% c.loc('FIELD REQUIRED SPLAT') %]</span>[% END; info.error; info.warning; info.success %]</span>
[% END %]
[% IF info.support %]<p>[% c.loc(info.support) %]</p>[% END %]
</div>
</div>
[% END;
MACRO password_field(info) BLOCK;
info.type = 'password';
info.value = ''; # Force values to not be set.
text_field(info);
END;
MACRO text_field(info) BLOCK;
IF !info.defined('value') && results.${context.scope};
value_name = info.name;
IF context.scope; value_name = value_name.replace("^${context.scope}.", ""); END;
info.value = results.${context.scope}.get_original_value(value_name);
END;
IF !info.error && messages.for_scope(context.scope);
value_name = info.name;
IF context.scope; value_name = value_name.replace("^${context.scope}.", ""); END;
message = messages.for_scope(context.scope).for_subject(value_name).first_message;
IF message; info.${message.level} = c.loc(message.id); END;
END;
IF info.filter && info.value;
f = info.filter;
IF f == "join";
info.value = info.value.join(', ');
ELSE;
TRY; info.value = FILTER $f; info.value; END; CATCH; "Error applying filter to ${info.name}!"; END;
END;
END;
DEFAULT info.id = "form_" _ info.name.replace('\.', '_');
%]
<div class="control-group[% IF info.error %] error[% ELSIF info.warning %] warning[% ELSIF info.success %] success[% END %]">
[% label_field(info) %]
<div class="[% info.type || 'text' %] controls">
[%~ IF info.prepend || info.append ~%]<div class="[% IF info.prepend %]input-prepend[% END; IF info.append %] input-append[% END %]">
[%~ IF info.prepend ~%]<span class="add-on">[% info.prepend %]</span>[%~ END ~%]
[%~ END ~%]
<input type="[% info.type || 'text' %]" id="[% info.id %]" name="[% info.name %]"
[% IF info.pattern %] pattern="[% info.pattern %]" [% END %]
value="[% info.value | html %]"
[%~ IF info.classes %] class="[% info.classes.join(" ") %]"[% END ~%]
[% # The unicode char below is purely to make the placeholder junk work. %]
[%~ IF info.hint %] placeholder="&#x25E6; [% c.loc(info.hint) %]"[% END ~%]
[%~ IF info.autocomplete == 0 %] autocomplete="false"[% END ~%]
[%~ IF info.disabled == 1 %] disabled="disabled"[% END ~%]
[%~ IF info.tabindex %] tabindex="[% info.tabindex %]"[% END ~%]
[%~ IF info.maxlength %] maxlength="[% info.maxlength %]"[% END ~%]>
[%~ IF info.append ~%]<span class="add-on">[% info.append %]</span>[%~ END ~%]
[%~ IF info.prepend || info.append ~%]</div>[%~ END ~%]
[% IF info.required || info.error || info.success || info.warning %]
<span class="help-inline">[% IF info.required %]<span class="req">[% c.loc('FIELD REQUIRED SPLAT') %]</span>[% END; info.error; info.warning; info.success %]</span>
[% END %]
[% IF info.support %]<p>[% c.loc(info.support) %]</p>[% END %]
</div>
</div>
[% END;
MACRO textarea_field(info) BLOCK;
IF !info.value && results.${context.scope};
value_name = info.name;
IF context.scope; value_name = value_name.replace("${context.scope}.", ""); END;
info.value = results.${context.scope}.get_original_value(value_name);
END;
IF !info.error && messages.for_scope(context.scope);
value_name = info.name;
IF context.scope; value_name = value_name.replace("^${context.scope}.", ""); END;
message = messages.for_scope(context.scope).for_subject(value_name).first_message;
IF message; info.${message.level} = c.loc(message.id); END;
END;
%]
<div class="control-group[% IF info.error %] error[% ELSIF info.warning %] warning[% ELSIF info.success %] success[% END %]">
[% label_field(info) %]
<div class="[% info.type || 'textarea' %] controls">
<textarea [%= ~%]
id="form_[% info.name %]" name="[% info.name %]"
[% IF info.hint %] placeholder="[% c.loc(info.hint) %]"[% END %]
[%~ IF info.classes %] class="[% info.classes.join(' ') %]"[% END ~%]
[%~ IF info.disabled == 1 %] disabled="disabled"[% END ~%]
[%~ IF info.autocomplete == 0 %] autocomplete="false"[% END ~%]
[%~ IF info.tabindex %] tabindex="[% info.tabindex %]"[% END ~%]
[%~ IF info.rows %] rows="[% info.rows %]"[% END ~%]
[%~ IF info.cols %] cols="[% info.cols %]"[% END ~%]
>
[%~ info.value | html ~%]
</textarea>
[% IF info.error || info.success || info.warning %]
<span class="help-block">[% IF info.required %]<span class="req">[% c.loc('FIELD REQUIRED SPLAT') %]</span>[% END; info.error; info.warning; info.success %]</span>
[% END %]
[% IF info.support %]<p>[% c.loc(info.support) %]</p>[% END %]
</div>
</div>
[% END;
MACRO checkboxes(info) BLOCK;
IF !info.defined('value') && results.${context.scope};
value_name = info.name;
IF context.scope; value_name = value_name.replace("${context.scope}.", ""); END;
info.value = results.${context.scope}.get_original_value(value_name);
END;
IF !info.error && messages.for_scope(context.scope);
value_name = info.name;
IF context.scope; value_name = value_name.replace("^${context.scope}.", ""); END;
IF message; info.${message.level} = c.loc(message.id); END;
END;
%]
<div class="control-group[% IF info.error %] error[% ELSIF info.warning %] warning[% ELSIF info.success %] success[% END %]">
[% label_field(info) %]
<div class="checkboxes controls">
[% FOREACH item = info.array;
IF !c.tt_blessed(item) && item.keys %]
[% FOREACH this_name = item.values.0;
SET this_value = item.keys.0;
%]<label class="checkbox"><input type="[% info.type || 'checkbox' %]" name="[% info.name %]" value="[% this_value | html %]"[% IF info.selected.${this_value}.defined %] checked="checked"[% END %]>[% this_name %]</label>
[% END %]
[% ELSE;
IF info.defined('value_method');
SET this_value = item.${info.value_method};
ELSE;
SET this_value = item;
END;
IF info.defined('value_method');
SET this_name = item.${info.label_method};
ELSE;
SET this_name = item;
END;
%]<label class="[% IF info.type == 'radio'; "radio"; ELSE; "checkbox"; END %][% IF info.inline %] inline[% END %]"><input type="[% info.type || 'checkbox' %]" name="[% info.name %]" value="[% this_value | html %]"[% IF info.value == this_value || info.selected.defined(this_value) %] checked="checked"[% END %]> [% this_name %]</label>
[% END %]
[% END %]
[% IF info.required || info.error || info.success || info.warning %]
<p class="help-block">
[% IF info.required %]<span class="req">[% c.loc('FIELD REQUIRED SPLAT') %]</span>[% END %]
[% info.error; info.warning; info.success %]</span>
</p>
[% END %]
</div>
</div>
[% END ~%]
[%~ MACRO checkbox(info) BLOCK;
IF !info.defined('value') && results.${context.scope};
value_name = info.name;
IF context.scope; value_name = value_name.replace("${context.scope}.", ""); END;
info.value = results.${context.scope}.get_original_value(value_name);
END;
IF !info.error && messages.for_scope(context.scope);
value_name = info.name;
IF context.scope; value_name = value_name.replace("^${context.scope}.", ""); END;
IF message; info.${message.level} = c.loc(message.id); END;
END;
%]
<div class="control-group[% IF info.error %] error[% ELSIF info.warning %] warning[% ELSIF info.success %] success[% END %]">
[% label_field(info) %]
<div class="[% info.type || 'text' %] controls">
<input type="[% info.type || 'checkbox' %]" id="[% info.id %]" name="[% info.name %]"
[% IF info.pattern %] pattern="[% info.pattern %]" [% END %]
value="[% info.value | html %]"
[%~ IF info.classes %] class="[% info.classes.join(" ") %]"[% END ~%]
[%~ IF info.disabled == 1 %] disabled="disabled"[% END ~%]
[%~ IF info.checked == 1 %] checked="checked"[% END ~%]
[%~ IF info.tabindex %] tabindex="[% info.tabindex %]"[% END ~%]
[%~ IF info.maxlength %] maxlength="[% info.maxlength %]"[% END ~%]>
[% IF info.required || info.error || info.success || info.warning %]
<span class="help-inline">[% IF info.required %]<span class="req">[% c.loc('FIELD REQUIRED SPLAT') %]</span>[% END; info.error; info.warning; info.success %]</span>
[% END %]
[% IF info.support %]<p>[% c.loc(info.support) %]</p>[% END %]
</div>
</div>
[% END ~%]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment