A structured category replaces the rich text body of its articles with typed fields that you define in the dashboard. This guide reads those fields and renders them with one generic component.
Reference - This guide is a tutorial. For exhaustive, always-current signatures and field lists, see the auto-generated reference:
Types: SDK Types (
SchemaField,SchemaFieldType)API: Storefront: Content
Prerequisites
A Lynkow client in
@/lib/lynkow(Add Lynkow to an existing project)A category set to structured mode in the dashboard, with a few fields
Standard and structured categories
Every category has a contentMode:
'standard': articles carry rendered HTML inbody.'structured': articles carry their values incustomData, keyed by the fieldkeyof the category'sschema.
customData comes only with a single article read such as contents.getBySlug(). List rows (contents.list(), the contents of categories.getBySlug()) do not include it.
Read the values and the schema
The values live on the article and the field definitions live on the category:
const article = await lynkow.contents.getBySlug('retail-store-redesign')
article.customData?.budget // 45000
article.customData?.client // { name: 'Acme Stores', location: 'Lyon' }
const { category } = await lynkow.categories.getBySlug('case-studies', { limit: 1 })
category.contentMode // 'structured'
category.schema
// {
// fields: [
// { key: 'budget', type: 'number', label: 'Budget', required: true },
// { key: 'summary', type: 'richtext', label: 'Summary' },
// { key: 'gallery', type: 'array', label: 'Gallery', itemLabel: 'Photo', fields: [
// { key: 'image', type: 'image', label: 'Image' },
// { key: 'caption', type: 'string', label: 'Caption' }
// ]},
// { key: 'client', type: 'object', label: 'Client', fields: [
// { key: 'name', type: 'string', label: 'Name' },
// { key: 'location', type: 'string', label: 'Location' }
// ]}
// ]
// }categories.getBySlug() also returns a page of the category's articles. limit: 1 keeps that page small when you only need the schema.
Field types and their values
The renderer below reads key, type, label, options (for select and multiselect), fields (for object and array) and itemLabel from each field.
Field type | Value in | Render as |
|---|---|---|
|
| Text |
|
|
|
|
| HTML, like |
|
| Formatted number |
|
| Badge or condition |
|
| Label from |
|
| Labels from |
|
|
|
|
|
|
|
| External link |
|
| |
|
| Formatted date |
|
| Formatted date and time |
|
| Swatch |
|
| Sub-fields, recursively |
|
| Sub-fields of each item |
An image or file value is an object with a required url and an optional alt, never a bare URL string. A richtext value arrives as HTML, ready to render.
A generic renderer
Create components/structured-content-renderer.tsx:
import type { SchemaField } from 'lynkow'
const isEmpty = (value: unknown) => value === undefined || value === null || value === ''
export function StructuredContentRenderer({
data,
fields,
}: {
data: Record<string, any>
fields: SchemaField[]
}) {
return (
<dl className="space-y-6">
{fields.map((field) =>
isEmpty(data[field.key]) ? null : (
<div key={field.key}>
<dt className="text-sm font-medium text-gray-500">{field.label}</dt>
<dd>
<FieldValue field={field} value={data[field.key]} />
</dd>
</div>
)
)}
</dl>
)
}
export function FieldValue({ field, value }: { field: SchemaField; value: any }) {
const optionLabel = (v: string) => field.options?.find((o) => o.value === v)?.label ?? v
switch (field.type) {
case 'text':
return <p className="whitespace-pre-wrap">{value}</p>
case 'richtext':
return <div className="prose max-w-none" dangerouslySetInnerHTML={{ __html: value }} />
case 'boolean':
return <span>{value ? 'Yes' : 'No'}</span>
case 'select':
return <span>{optionLabel(value)}</span>
case 'multiselect':
return <span>{(value as string[]).map(optionLabel).join(', ')}</span>
case 'image':
return <img src={value.url} alt={value.alt ?? ''} loading="lazy" />
case 'file':
return <a href={value.url} download>{value.alt || value.url.split('/').pop()}</a>
case 'url':
return <a href={value} target="_blank" rel="noopener noreferrer">{value}</a>
case 'email':
return <a href={`mailto:${value}`}>{value}</a>
case 'date':
return <time dateTime={value}>{new Date(value).toLocaleDateString('en-US')}</time>
case 'datetime':
return <time dateTime={value}>{new Date(value).toLocaleString('en-US')}</time>
case 'color':
return <span className="inline-block h-6 w-6 rounded" style={{ backgroundColor: value }} />
case 'object':
return <StructuredContentRenderer data={value} fields={field.fields ?? []} />
case 'array':
return (
<ol className="space-y-4">
{(value as Record<string, any>[]).map((item, i) => (
<li key={i}>
{field.itemLabel && <p className="text-sm text-gray-500">{field.itemLabel} {i + 1}</p>}
<StructuredContentRenderer data={item} fields={field.fields ?? []} />
</li>
))}
</ol>
)
default:
return <span>{String(value)}</span>
}
}string and number fall through to the default branch. Format numbers there, or add a case 'number' with Intl.NumberFormat.
A structured article page
Create app/case-studies/[slug]/page.tsx:
import { notFound } from 'next/navigation'
import { isLynkowError } from 'lynkow'
import { lynkow } from '@/lib/lynkow'
import { StructuredContentRenderer } from '@/components/structured-content-renderer'
const CATEGORY_SLUG = 'case-studies'
export default async function CaseStudyPage({ params }: { params: Promise<{ slug: string }> }) {
const { slug } = await params
let article
try {
article = await lynkow.contents.getBySlug(slug)
} catch (error) {
if (isLynkowError(error) && error.status === 404) notFound()
throw error
}
// getBySlug() finds any article by slug: serve only those whose first category is this one
if (article.categories[0]?.slug !== CATEGORY_SLUG) notFound()
const { category } = await lynkow.categories.getBySlug(CATEGORY_SLUG, { limit: 1 })
return (
<main className="mx-auto max-w-3xl px-4 py-12">
<h1 className="text-4xl font-bold">{article.title}</h1>
{article.customData && category.schema ? (
<StructuredContentRenderer data={article.customData} fields={category.schema.fields} />
) : (
<div className="prose" dangerouslySetInnerHTML={{ __html: article.body }} />
)}
</main>
)
}This folder serves an article only when it matches the path the API gives it, and that path depends on the site's URL settings and languages. To serve any article wherever its path points, use the route of Catch-all Routes & Path Resolution.
For a custom layout, read fields by key and reuse FieldValue for the ones you do not style yourself:
import type { SchemaField } from 'lynkow'
import { FieldValue } from '@/components/structured-content-renderer'
function CaseStudyHeader({ data, fields }: { data: Record<string, any>; fields: SchemaField[] }) {
const client = fields.find((f) => f.key === 'client')
return (
<header>
<p className="text-3xl font-bold">
{new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' }).format(data.budget)}
</p>
{client && data.client && <FieldValue field={client} value={data.client} />}
</header>
)
}Next Steps
Pages & Site Configuration: schema-driven pages and site blocks
Media & Image Optimization: resize
imagefield URLsCatch-all Routes & Path Resolution: resolve any article
path