# 6. Templates, parts and patterns

Block themes only. A template, a template part or a synced pattern becomes a Jikji document the same way a page does; the difference is where it renders and how it is published.

## 6.1 Concepts

| | |
|---|---|
| Template selection | **WordPress's template hierarchy** decides which template a request uses (`single` → `singular` → `index`, …). If the chosen template is published with Jikji, Jikji renders the whole page |
| Parts | A template part (header, footer…) is replaced by its Jikji version **wherever it renders** — inside a block template or inside a Jikji template (`j:part`) |
| Patterns | A **synced pattern** (`wp_block` post) is replaced by its Jikji version wherever it is inserted by reference (`core/block`). Unsynced patterns are copies, so they have no Jikji version to show ([6.4](#64-synced-patterns)) |
| Jikji publish state | Template posts are always `publish` in WordPress, so Jikji keeps its own state, `draft` or `publish`. While the Jikji version is a draft the site keeps using the blocks — save and check as much as you like, then publish |
| Fallback | If a published Jikji template fails at runtime, visitors get the block template (`post_content`) and the error is recorded (`wp jikji status` → `runtimeError`, shown in the builder as "Runtime error — block template shown") |
| Site-wide | Hierarchy templates (`index`, `single`, `page`, `archive`…) and theme parts (`header`, `footer`) affect every matching page, so scripts must confirm their publish/unpublish/release with `--yes` |
| Theme switch | Jikji templates belong to their theme: switching themes stops using them, switching back resumes. A changed theme file never merges into a customized template; `themeFileChanged` in the list only reports it |
| The first document | Adopting a theme-file template creates the customized post (as the Site Editor does) with the blocks as the fallback. From the Site Editor the Jikji document starts from the **saved blocks** (paragraphs, headings, lists, images become elements; the rest stays block islands); the CLI/REST default is a starting skeleton, `--import` / `import: true` converts the blocks |

## 6.2 The Site Editor is the entry point

Open any template, part or pattern in the Site Editor (Appearance → Editor). Builders see **Edit with Jikji** — as a notice action and in the ⋮ options menu:

- A document Jikji already manages opens in the builder at once. The notice also says whether the **Jikji version or the blocks** are live, and that blocks edited here are not shown while a Jikji version is published (everyone sees that notice, not only builders).
- An unmanaged document is **adopted** first — its saved blocks become the starting Jikji document — and then opened. If the Site Editor has **unsaved changes**, nothing is adopted: the notice offers *Save and edit with Jikji*, and adoption follows a successful save (a failed save or a save in progress adopts nothing).
- The builder says once what the document started from: how many blocks were converted, how many stayed WordPress blocks, or that it started from an empty skeleton because the blocks could not be converted.
- An unsynced pattern gets an explanation and no action ([6.4](#64-synced-patterns)).
- A document made after the page loaded (a new pattern, a duplicated part) is looked up when you open it; no reload is needed.

There is no separate Jikji screen for templates. Publishing, unpublishing and previewing happen in the builder's template bar; releasing a document from Jikji (keeping the post and its blocks) and the overall list are `wp jikji template release`, `wp jikji templates list` / `pattern list`, or the REST routes. "Back to WordPress admin" in the builder returns to the document in the Site Editor.

## 6.3 Template documents in the builder

A template document shows a **template bar** above the canvas: the kind (Template, Template part, Pattern) and name, the state badge (Draft / Published / Runtime error), **Preview on site…**, **Publish** / **Unpublish**, and for templates the **Sample** selector ([6.6](#66-the-sample-query)). The save button reads *Save draft* while the Jikji version is a draft and *Save and publish* once it is published. Template documents can use the **Template Part** block, the data picker's **Site** and **Archive** tabs, the loop source **Main query** and the **Pagination** helper ([Dynamic data 5.7](05-dynamic-data.md#57-the-main-query-and-pagination-templates)).

Publishing compiles; the state changes only when the compile succeeds (otherwise the error is shown and the site keeps the previous state). Unpublishing returns the site to the blocks and keeps the Jikji draft.

## 6.4 Synced patterns

Site patterns created in the Site Editor (Patterns) are `wp_block` posts and work like parts: publish, and every place that inserts the pattern **by reference** shows the Jikji version; a second copy on one page gets `--p2` suffixes on its IDs. A runtime error falls back to the pattern's blocks and is recorded. A pattern post that is not published, or has a password, shows nothing — exactly as WordPress treats it — even when a Jikji version is published.

- The builder labels the document **Pattern** with its title; there is no sample selector (a pattern takes the context of the page it appears on), and *Preview on site…* asks on which page to preview, like a part.
- Theme-file patterns are not posts and cannot be adopted; duplicate one in the Site Editor first.
- **Unsynced** patterns are copied into pages as plain blocks, so a Jikji version would never be shown: they can be managed (the builder says "Not synced") but are not rendered until the pattern is made synced.

```bash
$WP jikji pattern list --format=json                 # id, title, synced, jikji status, editUrl
$WP jikji pattern adopt "$PAT" --format=json         # the pattern's blocks become the first Jikji document (a draft)
$WP jikji template publish "$PAT" --yes              # every reference now renders the Jikji version
$WP jikji template unpublish "$PAT" --yes            # back to the pattern blocks (the draft stays)
$WP jikji template release "$PAT" --yes              # Jikji data removed; the pattern and its blocks stay
```

`pattern adopt` reports what it started from — `import.converted` (blocks that became elements), `island` (kept as WordPress blocks), `skipped`, `warning`, or `error` (e.g. `jikji_nothing_to_import`) when it fell back to the skeleton.

## 6.5 Which template to use

| Goal | How | Scope |
|---|---|---|
| A full layout for particular pages | A **custom template** (`wp jikji template create <slug> --title=… --assign=<ids>`, or core `POST /wp/v2/templates` then adopt) assigned to those pages | Only the assigned pages — the safest choice |
| All posts, archives, search, 404… | A **hierarchy template** of the theme (`single`, `archive`, `index`, `search`, `404`, `single-{type}`, `page-{slug}`…) — adopt it; if the theme does not have it, `wp jikji template prepare <slug> --yes` creates it from the blocks of the template those requests use now, then adopts it | **Every** matching request. Build as a Jikji draft, check, then publish |
| Header, footer, banners | A **new part** (`wp jikji part create`, included with `j:part` only where you want it) or the **theme's part** (`template adopt header --part`) | A new part renders only where it is included; publishing the theme `header` changes every page that uses it |
| A fragment inserted into many pages | A **synced pattern** ([6.4](#64-synced-patterns)) | Wherever it is inserted by reference |

Rules the commands enforce:

- `template create` refuses hierarchy names (`index`, `single-post`, `page-about`, `image-jpeg`… — names WordPress applies by itself) with `jikji_hierarchy_slug` and points to adopt or prepare. A custom template for five hub pages gets a non-hierarchy name (`situation-layout`, assigned to the pages), while `page-situation` would be the hierarchy template of the one page whose slug is `situation`.
- `template prepare` refuses non-hierarchy names (`jikji_not_hierarchy`) and points to create. Names a plugin adds through `*_template_hierarchy` filters can be declared with the `jikji_cli_hierarchy_slug` filter. The copy source is the template the hierarchy falls through to (`single-case` → `single` → `singular` → `index`); `--from=<slug>` chooses another; later changes to the source do not propagate. Creating the template itself changes which template requests use, hence `--yes`.
- `template create` checks everything it can (slug, duplicates, the posts to assign) before the first write; if something fails afterwards it reports `jikji_partial_failure` with `data.resume`, the command to run next (adopt and assign are idempotent).

Default samples by template: `index`/`home` the posts list, `front-page` the static front page or the list, `single*` a recent post of that type, `page*` the front or a recent page, archives a term with posts, `search` `{"s":"a"}`, `404` an empty 404.

## 6.6 The sample query

A template has no post of its own, so the canvas and `wp jikji eval` evaluate it inside a stored **sample query** (`WP_Query` variables). The template bar's Sample selector offers candidates (the default for the template, Home, posts of each type, categories…) and accepts custom variables as JSON.

```bash
$WP jikji template sample "$SINGLE"                              # current sample and candidates
$WP jikji template sample "$SINGLE" --vars='{"p": 12, "post_type": "post"}'
$WP jikji template sample "$ARCHIVE" --vars='{"cat": 3}'
$WP jikji template sample "$SEARCH" --vars='{"s": "shoes"}'
$WP jikji template sample "$NOTFOUND" --vars='{"error": "404"}'
```

`found` is the number of posts the sample's main query returns; with 0, `$post` is `null` and post bindings look empty.

## 6.7 Headers, footers and hierarchy templates from a shell

Replacing the theme header (site-wide):

```bash
HEADER=$($WP jikji template adopt header --part --format=json | jq -r .wpId)

cat > header.html <<'HTML'
<header id="{site_header}" class="site-header">
  <a id="{brand}" class="brand" href="#" j:attr:href="jikji_site('url')">
    <img id="{brand_logo}" src="" alt="" j:if="jikji_site('logo_url')" j:attr:src="jikji_site('logo_url')" j:attr:alt="jikji_site('name')">
    <span id="{brand_name}" j:text="jikji_site('name')">Site</span>
  </a>
  <nav id="{site_nav}">
    <a id="{nav_blog}" href="/blog/">Blog</a>
    <a id="{nav_about}" href="/about/">About</a>
  </nav>
</header>
HTML
cat > header.json <<'JSON'
[
  {"name": "{site_header}", "type": "I", "css": "display:flex;justify-content:space-between;align-items:center;padding:16px 24px;border-bottom:1px solid #e5e7eb;"},
  {"name": "{site_nav}", "type": "I", "css": "display:flex;gap:20px;"}
]
JSON

$WP jikji content save "$HEADER" --html=header.html --styles=header.json --placeholders
$WP jikji template publish "$HEADER" --yes          # every page that uses the theme header changes
```

The footer is the same (`template adopt footer --part`, `j:text="'© ' . date('Y') . ' ' . jikji_site('name')"`). Undo with `template unpublish <id> --yes` (back to the block header, draft kept) or `template release <id> --yes` (Jikji data removed); to return to the theme file entirely, release and then `wp post delete <id> --force` (the Site Editor's "Reset").

A new header for some templates only: `wp jikji part create --title='Promo Header' --area=header`, save, publish, and include it with `<div j:part="promo-header"></div>` — no confirmation, since it renders only where included.

**Menus.** Jikji has no menu helper. For a menu operators edit under Appearance → Menus (Navigation), make a part in the Site Editor that holds only a Navigation block (e.g. `site-nav`) and include it with `<div j:part="site-nav"></div>`; WordPress renders it with `current-menu-item`/`aria-current` and its mobile script. For a developer-only menu, write the links in a Jikji part with `j:attr:aria-current="is_page('about') ? 'page' : null"` and style with `@state current`.

A single-post template:

```bash
SINGLE=$($WP jikji template adopt single --format=json | jq -r .wpId)     # theme has it
SINGLE=$($WP jikji template prepare single --yes --format=json | jq -r .wpId)   # theme does not
```

```html
<div j:part="header"></div>
<main id="{single}" class="single">
  <p id="{cats}" class="cats" j:text="jikji_term_list('category', $post)">Category</p>
  <h1 id="{title}" j:text="jikji_post('title', $post)">Title</h1>
  <p id="{meta}" class="meta" j:text="jikji_post('date', $post, 'Y.m.d') . ' · ' . jikji_post('author_name', $post)">date · author</p>
  <img id="{cover}" src="" alt="" j:if="jikji_post('thumbnail_url', $post)" j:attr:src="jikji_post('thumbnail_url', $post, 'large')" j:attr:alt="jikji_post('title', $post)">
  <div id="{content}" class="content" j:html="jikji_post('content', $post)"></div>
  <ul id="{tags}" class="tags" j:each="jikji_terms('post_tag', $post) as $term">
    <li id="{tag}"><a id="{tag_link}" href="#" j:attr:href="jikji_term('link', $term)" j:text="jikji_term('name', $term)">tag</a></li>
  </ul>
  <section id="{related}" j:each="jikji_query('{&quot;post_type&quot;:&quot;post&quot;,&quot;post__not_in&quot;:[&quot;{post:ID}&quot;],&quot;posts_per_page&quot;:3}', $post) as $item">
    <a id="{rel_link}" href="#" j:attr:href="jikji_post('permalink', $item)" j:text="jikji_post('title', $item)">Related</a>
  </section>
</main>
<div j:part="footer"></div>
```

```bash
$WP jikji content save "$SINGLE" --html=single.html --styles=single.json --placeholders
$WP jikji eval "$SINGLE" "jikji_post('title', \$post)"            # evaluated under the template's sample
$WP jikji template publish "$SINGLE" --yes
```

The posts list goes in `home` (or `index`) with the markup of [Dynamic data 5.7](05-dynamic-data.md#57-the-main-query-and-pagination-templates); search in `search` with `jikji_archive('search_query')`; a 404 in `404` with a message and a `jikji_site('url')` link. Publishing a list layout to `index` makes every request that falls through to `index` look like a list (pages and posts too if the theme has no `page`/`single`) — prefer narrower names (`archive`, `category`, `home`) or prepare `page` and `single`.

A custom page template:

```bash
TPL=$($WP jikji template create landing --title='Landing' --assign="$PAGE" --format=json | jq -r .wpId)
$WP jikji content save "$TPL" --html=landing.html --styles=landing.json --placeholders
$WP jikji template publish "$TPL"
$WP jikji template assign landing 42 57          # more pages later (idempotent)
```

Inside it the page's own content (its Jikji document or block body) is `j:html="jikji_post('content', $post)"`; output the title in the template or in the page document, not both.

## 6.8 Publish, verify, undo

```bash
$WP jikji template publish "$SINGLE" --yes       # compiles; the state changes only on success
$WP jikji status "$SINGLE" --format=json          # status, compileError, runtimeError, siteWide, previewUrl
$WP jikji template unpublish "$SINGLE" --yes     # back to the blocks, Jikji draft kept
$WP jikji template release "$SINGLE" --yes       # Jikji data removed, post and blocks kept
$WP jikji revisions list "$SINGLE"               # every save and publish
```

- A failed publish (`jikji_compile_failed`) leaves the state unchanged; the site keeps what it had.
- A `runtimeError` means visitors see the block template; fix and publish again to clear it, or unpublish to return immediately.
- Draft previews (`previewUrl`, the builder's Preview) are visible to logged-in builders only.

## 6.9 Operators and developers

Expressions run as PHP, so the builder is for `jikji_compile` holders (administrators). Give editors fields instead of the builder:

| Who | What | Where |
|---|---|---|
| Operator (editor role) | Copy, photos, links, the items of lists | ACF/SCF fields on the post edit screen, the block body, menus, form settings |
| Developer | Structure, bindings, styles, publishing templates | The Jikji builder, `wp jikji` |

Make everything an operator changes a field and let the document read it ([Dynamic data 5.8](05-dynamic-data.md#58-acf-and-secure-custom-fields)); a post whose body is the block editor's is included with `j:html="jikji_post('content', $post)"`. A dedicated content-only editing mode does not exist yet.
