How AI is powering better personalization in e-commerce [with Vercel]. Sign up now

Sanity initial value template not working in structure - "Template not defined" warning

4 repliesLast updated: Dec 1, 2025

Hi! I'm trying to use a initial value template in a structure like this example . But the value is not set, and in the browser console there is a warning "Template 'article-by-product' not defined, using empty initial value".What am I doing wrong ?
AI Update

I can see the confusion here! The error "Template 'article-by-product' not defined, using empty initial value" means your template isn't properly registered in your Studio configuration. Let me show you the correct way to set this up.

The Solution

Initial Value Templates need to be defined in your sanity.config.ts file using the schema.templates property. Here's the proper setup:

// sanity.config.ts
import {defineConfig} from 'sanity'

export default defineConfig({
  // ... other config
  schema: {
    types: [
      // your schema types here
    ],
    templates: (prev) => [
      ...prev,
      {
        id: 'article-by-product',
        title: 'Article by Product',
        schemaType: 'article',
        parameters: [{name: 'productId', type: 'string'}],
        value: (params) => ({
          product: {
            _type: 'reference',
            _ref: params.productId
          }
        })
      }
    ]
  }
})

Then in your Structure Builder, you can reference this template:

S.listItem()
  .title('New Article')
  .child(
    S.documentTypeList('article')
      .initialValueTemplates([
        S.initialValueTemplateItem('article-by-product', {
          productId: 'some-product-id'
        })
      ])
  )

Key Points

Common Mistakes

The most common issue is not defining the template in schema.templates at all, or defining it in the wrong place (like in document.newDocumentOptions, which is for customizing the UI options shown to users, not for defining templates themselves). The Structure Builder is just for using templates - the actual template definitions must live in your schema configuration.

Another common mistake is a typo in the template ID - make sure the string you pass to initialValueTemplateItem() exactly matches the id in your template definition.

Restart Required

After adding templates to sanity.config.ts, make sure to restart your dev server. Changes to the config file require a restart to take effect.

Dynamic Values Example

If you need to fetch data or use async logic, your value function can be async:

{
  id: 'article-by-product',
  title: 'Article by Product',
  schemaType: 'article',
  parameters: [{name: 'productId', type: 'string'}],
  value: async (params) => {
    // You can even fetch data here if needed
    return {
      product: {
        _type: 'reference',
        _ref: params.productId
      },
      publishedAt: new Date().toISOString()
    }
  }
}

For more examples of using templates with parent-child relationships, check out the parent-child taxonomy guide which shows practical implementations.

Show original thread
4 replies

Was this answer helpful?

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Related contributions