2.9 KiB
title: Form Templates summary: Customize the generated HTML for a FormField or an entire Form.
Form Templates
Most markup generated in SilverStripe can be replaced by custom templates. Both Form and FormField instances
can be rendered out using custom templates using setTemplate
.
$form = new Form(..);
$form->setTemplate('MyCustomFormTemplate');
// or, just a field
$field = new TextField(..);
$field->setTemplate('MyCustomTextField');
Both MyCustomTemplate.ss
and MyCustomTextField.ss
should be located in mysite/templates/forms/ or the same directory as the core.
By default, Form and Fields follow the SilverStripe Template convention and are rendered into templates of the same
class name (i.e EmailField will attempt to render into EmailField.ss
and if that isn't found, TextField.ss
or
finally FormField.ss
).
For FormField instances, there are several other templates that are used on top of the main setTemplate
.
$field = new TextField();
$field->setTemplate('CustomTextField');
// Sets the template for the <input> tag. i.e '<input $AttributesHTML />'
$field->setFieldHolderTemplate('CustomTextField_Holder');
// Sets the template for the wrapper around the text field. i.e
// '<div class="text">'
//
// The actual FormField is rendered into the holder via the `$Field`
// variable.
//
// setFieldHolder() is used in most `Form` instances and needs to output
// labels, error messages and the like.
$field->setSmallFieldHolderTemplate('CustomTextField_Holder_Small');
// Sets the template for the wrapper around the text field.
//
// The difference here is the small field holder template is used when the
// field is embedded within another field. For example, if the field is
// part of a `FieldGroup` or `CompositeField` alongside other fields.
All templates are rendered within the scope of the FormField. To understand more about Scope within Templates as well as the available syntax, see the Templates documentation.