1

I'm unable to use space in field name like "Page Title" in sitecore with nextjs.

in Layout.tsx it works like below but not working when have sitecore filed like "Page Title":

const { route } = layoutData.sitecore;

  return (
    <>
      <Head>
        <title>{route?.fields?.PageTitle?.value || 'Page'}</title>
        <link rel="icon" href={`${publicUrl}/favicon.ico`} />
      </Head>
.
.
.
1
  • What response are you getting from the layout service? You should be able to get the output via a request such as /sitecore/api/layout/render/jss?lang=en&sc_apikey={YOUR_KEY}&item=%2F. Take a look at the field names in there. You certainly can use spaces in your field template names, but it all depends on how you have everything set up. Commented Nov 18, 2022 at 23:13

2 Answers 2

3

I think this is expected behavior as the field name will get directly served as a property on the javascript side which would not work with spaces in the middle of it.

What you can do as a workaround is to name your Sitecore field PageTitle and then set its Title field (under Data section) to be Page Title - this way your content editors will see a more readable version when they work with an item of this template and field reference will work too.

3

I think a good practice to reduce complications is to ensure field names have no spaces. For content author readability, you can add the spaces in the Title field of the field item (think of it like the display name for content authors). Utilizing the Title field also decreases the likelihood of having to rename fields down the line which can break your application if you haven't accounted for that possibility.

Because Layout service will not do anything with the field names when responding to any item request, you will be seeing field name in JSON response. It's basically our design to define an appropriate name(s) for fields. As per your ex- if you created a field with name Page Title then it will be returned as "Page Title".

Layout Service will only serialize field values and return appropriately. Each and every field type is having it's own serialization logic to return value in Layout service JSON.

Hope this helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.