There are several ways you can do it.
If you want to change the output for all fields of a particular schema field type, you can override the appropriate template in lib/modules/apostrophe-schemas/views
, such as string.html
.
The standard version of that template imports macros.html
and invokes schemas.string
, et cetera, but you don't have to do that.
What you DO have to do is:
Make sure your field's entire content is wrapped in an outer element with a data-name
attribute, which is set to the schema field name. Including any label or other content that should be shown and hidden together with the input field.
Make sure the field itself has a name
attribute which is also set to the schema field name. This applies to most field types, see macros.html for unusual cases like the tag and join editors.
In general: the markup is yours to change but data-
attributes and the name
attribute must stay the same.
"What if I want the conventional formatting in Apostrophe's forms, and special formatting in my forms?" One way to do that is to set a style
property on the field in your schema, like so:
{
name: 'flavor',
type: 'string',
style: 'custom'
}
Then your string.html
can look like this:
{%- import "macros.html" as schemas -%}
{% if (data.style == 'custom') %}
{# Custom way #}
<div data-name="{{ data.name }}">
<h4>{{ data.label }}</h4>
<input name="{{ data.name }}" />
</div>
{% else %}
{# Normal way #}
{{ schemas.string(data) }}
{% endif %}
Notice that this makes the custom
style available for any form on your site where you choose to use it when adding fields. Also note that you can use addFields
to restate the definition of a standard field if you want to change, let's say, title
to use a custom style.
"Where do I output the current value of the field?" You don't. The schema module javascript will set that on the fly, and read it as well, which is one reason why you must follow the data-name
and name
attribute conventions.