I want to do the following:
- Name template fields in the format
"My Field Name"
- Avoid filling out
Display Name
fields for each template field - Automatically serialize the field names in camelCase when returning the data from the Layout Service
Where
"fields": {
"Page Size": {
"value": 3
}
}
Becomes
"fields": {
"pageSize": {
"value": 3
}
}
By default, Sitecore does not do this -- it will serialize the field names literally, spaces included. This previous question about template field name conventions in JSS was helpful. The accepted recommendation was to use camelCase for the actual field names in conjunction with display names to make it readable for content authors. This works, but creates what could be considered unnecessary overhead. Further, this approach can be cumbersome when working with legacy codebases.
I have a temporary solution, but I am concerned about the maintainability because I have overridden the Layout Service Field Serializers with a single line of additional code (and had to do so for each field type):
namespace Website.Foundation.LayoutService.Serialization.FieldSerializers
{
public class NumberFieldSerializer : Sitecore.LayoutService.Serialization.FieldSerializers.NumberFieldSerializer
{
public NumberFieldSerializer(IFieldRenderer fieldRenderer) : base(fieldRenderer) { }
public override void Serialize(Field field, JsonTextWriter writer)
{
...
writer.WritePropertyName(field.Name.ToJsonPropertyName());
...
}
}
}
I'm looking for a maintainable approach to do this automatically.
To clarify, it's not that I am completely opposed to using Display Name
, or rather the Title
field of template fields to make them readable to content authors; it's that retrofitting it for hundreds of items becomes rather tedious. Automatic model generation is also a factor. A modern JSS solution written in TypeScript ostensibly calls for automatic model generation both in .cs
and .ts
files. Variable name format conventions are not the same across languages / frameworks.
The core of this question touches on best practices for C#, JS, Helix, and content authoring in terms of variable / label names.
For example, given a field item named My Field Name
, we have these requirements depending on where it's used:
- JSS:
myFieldName
- C#:
MyFieldName
(whereSomeModel.MyFieldName = {GUID}
) - Content Author:
My Field Name
There is no best solution. Each approach will involve code or configuration. The question stands: given a field named My Field Name
, what is the most maintainable way to automatically serialize the field name in the format myFieldName
in the Layout Service?
Display Name
? It is meant for this exact purpose - a nice display name and you can name the item in a programmatic way.Display Name
; the issue is more around automating and balancing C#, JS, helix, and content authoring conventions in terms of variable / label names. I added more information to the question.edge
GraphQL api? or the older layout service calls? In my experience, the Edge graphql end point is already camel casing the field names.