Form Templates
Describes the Form.io Form Template System
The Form.io template platform allows you to completely change the rendered HTML for any component within the Form.io platform. You can either create an entire template for all components, or simply override existing templates if you wish to just alter a few of the components.
There are several examples of templates that have been created which can be seen at the following URL's.

Setting a CSS Framework

When setting up an application, you can select which CSS Framework you want to render the output to. Form.io currently supports the following frameworks:
  • Bootstrap 3 (bootstrap3)
  • Bootstrap 4 (bootstrap)
  • Semantic UI (semantic)
  • Other - Contact us for help implementing another framework!
The default template is currently Bootstrap 4 so if you want to use that, you don't have to do anything else. In order to switch to a different framework, you will need to set it globally so that the renderer will use the framework's templates.
1
import { Templates } from 'formiojs';
2
3
Templates.framework = 'semantic';
Copied!
If you are using a framework wrapper, you should import Templates from that wrapper instead of formio.js so that it will apply to the Formio instance within the wrapper as well. For example, if you are using react-formio you should do:
1
import { Templates } from 'react-formio';
2
3
Templates.framework = 'semantic';
Copied!

Plugin registration

Templates can also be registered through the Plugin system, where an external template is provided to the renderer. For an example of how this is done, please see https://github.com/formio/semantic
While these examples represent complete templates, you can also create partial overrides as well.

Overriding templates

In addition to setting the global CSS Framework, you can also override specific templates within that framework. All templates within the renderer are pre-compiled templates. This means that instead of using HTML for each template, you will provide a template function, which simply takes a context variable and returns a string that is the HTML you would like to render. An example of a template function looks like the following.
1
const myInput = function(ctx) {
2
return '<div class="' + ctx.component.type + '">' + ctx.value + '</div>';
3
};
Copied!
Or if you are using ES6, it can be written easier as follows.
1
const myInput = (ctx) => `<div class="${ctx.component.type}">${ctx.value}</div>`;
Copied!
You can override a bunch of template functions by setting the current template with the templates you wish to override. This allows you to change the html output of the component even as the component continues to function the same. In order to do this, simply set the template on the Templates object and the renderer will start using it.
You can do this one of two ways. First, by setting multiple templates:
1
Templates.current = {
2
input: {
3
form: (ctx) => '<div>My custom template</div>'
4
},
5
html: {
6
form: (ctx) => '<div>My other custom template</div>'
7
}
8
};
Copied!
Or individually:
1
Templates.current.input.form = (ctx) => `<div>My custom template</div>`;
Copied!
You can extend an existing template by first getting the current template function and then call that function in addition to your own. This allows you to provide wrappers around certain elements within the renderer. Here is an example of how to do this.
1
var inputTemplate = Templates.current.input.form;
2
Templates.current = {
3
input: {
4
form: (ctx) => `<div class="input-wrapper">${inputTemplate(ctx)}</div>`
5
}
6
};
Copied!
IMPORTANT NOTE: These examples are in ES6. Depending on where you are making these changes, you may need to have them written in ES5 so that they will work with all browsers. Example.
1
var inputTemplate = Templates.current.input.form;
2
Templates.current = {
3
input: {
4
form: function (ctx) {
5
return '<div class="input-wrapper">' + inputTemplate(ctx) + '</div>';
6
}
7
}
8
};
Copied!

Specific templates

Templates can be shared by multiple components. For example, the input template is used by many component to render the html input element. Sometimes you may want to override it for only certain components but not all components. You can do this by setting the template name to include the specific information. This can either be the component type or the component property name (api key). You can even do both. The renderer will search in this order for the first match to determine which component to select:
1
`${templateName}-${component.type}-${component.key}`,
2
`${templateName}-${component.type}`,
3
`${templateName}-${component.key}`,
4
`${templateName}`,
Copied!
If you set a template for 'input-number' it will only override the input template for number components. If you set a template for 'input-number-total' it would only override the input template for a number component with the property name of 'total';

Render modes

In addition to templates, there are a few render modes that allow different representations of the same component. The default mode is form which will render all the components as form components. Another common mode is ```html`` which will render an html only representation of the component without any form elements like inputs. This is useful for displaying data.
The built in render modes are:
  • form - The default render to render as a form.
  • html - Render the data as generic html, instead of a form.
  • flat - Similar to form but will flatten out components like tabs and wizards that tend to hide data.
  • builder - A builder view of a component (not frequently used outside of the form builder).
To set the render mode, set it on the options when instantiating a form.
1
import { Form } from 'formiojs';
2
3
const form = new Form(document.getElementById('formio'), 'https://examples.form.io/example', {
4
renderMode: 'html'
5
});
Copied!
You can even create your own render modes and use them with your forms.
1
import { Form, Templates } from 'formiojs';
2
3
Templates.current = {
4
input: {
5
foo: (ctx) => '<div>bar</div>'
6
}
7
};
8
9
const form = new Form(document.getElementById('formio'), 'https://examples.form.io/example', {
10
renderMode: 'foo'
11
});
Copied!

Custom Component Templates

You can create custom components using the same lifecycle methods.

Template References (refs)

With the form.io templating functionality, the underlying DOM structure can be very different, even entirely custom. Because of this, adding events to the DOM necessitates being able to find the right part of the DOM to add the events. In order to do this, the formio.js library uses refs to refer to parts of the DOM. These can then be selected regardless of where they are in the DOM.
When rendering a template, you can use the ref attribute to set a reference string. Then in the attach phase, you can get any DOM elements that have that ref, regardless of where they are. In order to facilitate this, formio.js has a "loadRefs" function that can find all the refs and adds them to this.refs.
1
import { Templates, Components, Component } from 'formiojs';
2
3
Templates.addTemplate('mytemplate', {
4
form: (ctx) => `
5
<div>
6
<div ref="myref">
7
${ctx.foo}
8
<div ref="mychild">1</div>
9
<div ref="mychild">2</div>
10
<div ref="mychild">3</div>
11
</div>
12
</div>
13
`
14
});
15
16
class MyComponent extends Component {
17
init() {
18
// Init tasks here.
19
}
20
21
render() {
22
// By calling super.render, it wraps in component wrappers.
23
return super.render(this.renderTemplate('mytemplate', {
24
foo: 'bar',
25
data: 'these are available in the template'
26
}));
27
}
28
29
attach(element) {
30
this.loadRefs(element, {
31
myref: 'single',
32
mychild: 'multiple',
33
});
34
35
this.refs.myref; // This will be either null or the div with "myref" set on it.
36
this.refs.mychild; // This will be either null or an array of divs with "mychild" set on them.
37
}
38
39
detach() {
40
// Called on redraw or rebuild. The opposite of attach.
41
}
42
43
destroy() {
44
// Called on rebuild. The opposite of init.
45
}
46
}
47
48
Components.addComponent('mycomponent', MyComponent);
Copied!

Layout component references

Layout components can contain other components within them including other components of the same type. For this reason be careful with refs in layout components and be sure to append the component id to each key. This is so that loadRefs only selects the refs for that component and not any nested components.

Server Side Rendering

One of the goals of the 4.x branch was to make the formio.js renderer compatible with server side rendering. While this should be compatible with it due to the changes that were made, we haven't had a chance to fully test it out yet. We would greatly welcome some help in testing this out.
Here is how it is designed to work:
On the server side:
1
import { Form } from 'formiojs';
2
3
// Instead of a URL, you can also use a form definition JSON instead.
4
const form = new Form('https://examples.form.io/example');
5
form.ready.then(function(instance) {
6
const html = instance.render();
7
// At this point you can put the html in the page.
8
});
Copied!
On the client side:
1
import { Form } from 'formiojs';
2
3
const form = new Form('https://examples.form.io/example');
4
// Find the DOM element wherever it is.
5
const element = document.getElementById('formio');
6
form.ready.then(function(instance) {
7
instance.attach(element);
8
});
Copied!
Last modified 4mo ago