# 8. Syntax reference

## 8.1 The document

A Jikji document is **HTML plus style rows** ([Styles 4.6](04-styles.md#46-the-style-row-format)). The HTML is a fragment; `j:` attributes add conditions, loops and bindings and do not appear in the output. When the document is published it is compiled to a PHP template and a CSS file; the front end runs the result.

Write **one element per block** so the editor can open it later:

| Markup | Editor block |
|---|---|
| `section`; a `div`/`article`/`aside`/`header`/`footer`/`main`/`nav`/`figure` with element children; an empty `div` | Section · Div |
| `h1`–`h6` · `p`/`span`/`figcaption`/`time`/`summary` · `blockquote` | Heading · Text · Blockquote |
| `ul`/`ol` · `li` · `a` · `button` | List · List Item · Link (an `a`; a container when it holds blocks) · Button (a `button`; a Button added in the editor is written as `<a href="#">`) |
| `img` · `video` · `svg` · `i`/`span` with `ri-*` / `bi-*` classes | Image · Video · SVG · Icon |
| An element mixing text and elements (`<p>a <b>b</b></p>`); a `div` with only text | Rich Text — the inner markup is a string; `j:*` inside it compiles but has no canvas sample and no Data panel |
| `j:shortcode` · `j:code` · `j:part` elements · `wp:` elements | Shortcode · Code · Template Part · WordPress block |

- Whitespace between blocks is dropped. HTML comments follow the setting (removed by default).
- Do not write `<script>` in documents; a Code block exists for server code, but prefer helpers and bindings.
- `j:code` text, `<script>`, `<iframe>` and `on*` attributes in a document are output **as written** — documents are written by builders who can run PHP anyway, so only inline SVG is sanitized. The canvas never runs scripts.

## 8.2 IDs

- Elements that have styles or that the editor manipulates carry a **server ID**: `jk-` plus six alphanumeric characters, unique across the site (checked against the style table).
- A human-readable ID (`hero-title`) would share one style row with every other document using that name; IDs must be unique site-wide. Writing by hand, use placeholders — `id="{hero}"` with `wp jikji content save --placeholders` — or request IDs from `wp jikji ids` / `POST /elements/ids` / `Id_Generator::generate()`.
- The same ID twice in one document is refused. IDs inside a loop are suffixed per item on the page ([Dynamic data 5.5](05-dynamic-data.md#55-loops)).

## 8.3 The `j:` attributes

| Attribute | Value | Output |
|---|---|---|
| `j:if` | PHP condition | The whole element only when true |
| `j:each` | `<array expr> as $item` or `<array expr> as $key => $item` | The children once per item; nothing at all when empty |
| `j:text` | PHP expression | `esc_html()` of the value replaces the children |
| `j:html` | PHP expression | The value **unescaped** replaces the children — body, wysiwyg and other trusted HTML only |
| `j:attr:<name>` | PHP expression | The attribute's value; `href` `src` `action` `poster` through `esc_url()`, others `esc_attr()`; `null`/`false` drops the attribute, `''` keeps it empty |
| `j:shortcode` | A shortcode string | `do_shortcode()` output as the content |
| `j:code` | (the text content is the code) | The whole element replaced by the code, unescaped |
| `j:part` | A template part slug | The whole element replaced by the rendered part (template documents) |
| `wp:<block>` | Block attributes JSON | The whole element replaced by the rendered Gutenberg block; the text is the block markup (8.6) |

Examples and compiled PHP:

```html
<p id="{sale}" j:if="jikji_meta('on_sale', $post) === '1'">On sale</p>
```
```php
<?php if (jikji_meta('on_sale', $post) === '1'): ?><p id="jk-…">On sale</p><?php endif; ?>
```

```html
<ul id="{list}" j:each="jikji_query('{&quot;post_type&quot;:&quot;post&quot;,&quot;posts_per_page&quot;:3}', $post) as $item">
  <li id="{row}" j:text="jikji_post('title', $item)">Post title</li>
</ul>
```
```php
<?php if (jikji_query('{"post_type":"post","posts_per_page":3}', $post)): ?><ul id="jk-…"><?php foreach (jikji_query('{"post_type":"post","posts_per_page":3}', $post) as $item): ?><li id="jk-…"><?php echo esc_html(jikji_post('title', $item)); ?></li><?php endforeach; ?></ul><?php endif; ?>
```

```html
<div id="{form}" j:shortcode="[contact-form-7 id=&quot;12&quot;]"></div>
<div j:code>&lt;?php echo esc_html( date_i18n( 'Y' ) ); ?&gt;</div>
<div j:part="header"></div>
<div j:if="! is_front_page()" j:part="breadcrumbs"></div>
```

## 8.4 Rules

- **Conditions**: no `else`/`elseif` — write a second element with the opposite condition. No assignment or ternary (`?:`, `??`) in a condition; use `&&` and `||` (`and`/`or` bind too loosely). `<` and `&` in a value may be written `&lt;`/`&amp;`.
- **Loops**: `j:if` with `j:each` is evaluated outside the loop. Loop variables `$this`, `$GLOBALS`, `$__jk_…` are refused; `$post` is the document's post, so it is not a loop variable. The loop element's own `j:attr` is outside the loop; children are inside.
- **Content bindings**: `j:text`, `j:html` and `j:shortcode` — one per element. The element's existing children are the canvas placeholder and are not output; put meaningful placeholder text there.
- **Attributes**: `j:attr:` names are lowercase HTML attribute names; `id`, `class` and `on*` cannot be bound. A static attribute of the same name is the placeholder.
- **`j:code`**: no other `j:` attribute, no child elements.
- **`j:part`**: only `j:if` may accompany it; no children. The value is the WordPress part slug as stored (a part with a non-ASCII name has a percent-encoded slug such as `%ec%83%88-…`). A block part renders as WordPress renders it; a Jikji-published part renders its compiled output; a missing slug or a part that includes itself outputs nothing. A Jikji part that appears twice on one page gets `--p2`, `--p3`… suffixes on its IDs from the second copy (`--p2-1` inside a loop item) and references follow; its ID styles apply to every copy.
- **SVG**: no `j:*` inside an `<svg>` or on its root — put them on a wrapping element.
- **Expressions compile to PHP as written.** The **canonical form** — a helper, single-quoted string arguments and a context variable, e.g. `jikji_post('title', $post)` — is what the editor shows as a chip ("Post › Title") and reopens in the picker; any other PHP works but shows as Custom.

```
expr   := helper '(' ( str ( ',' ctx )? ( ',' str )? )? ')'
helper := jikji_post | jikji_meta | jikji_term_list | jikji_terms | jikji_term | jikji_term_query | jikji_field | jikji_row | jikji_query
        | jikji_site | jikji_archive | jikji_main_query | jikji_pagination | jikji_template_part
ctx    := $post | a loop variable
str    := a single-quoted PHP string
```

## 8.5 Quoting and JSON

Attribute values are double-quoted; inside a value `"` is `&quot;`, `&` is `&amp;`, `<` is `&lt;`. The compiler decodes entities when it reads the attribute, so `j:each="jikji_query('{&quot;post_type&quot;:&quot;post&quot;}', $post) as $item"` reaches PHP as `jikji_query('{"post_type":"post"}', $post)`. In a shell, keep `$post` out of double quotes (`'jikji_post("title", $post)'` or `\$post`), and write HTML in a quoted heredoc (`<<'HTML'`).

## 8.6 Block islands (`wp:`)

One Gutenberg block (with its inner blocks) is one `div`: the attribute **name** is the block name, the **value** its attributes JSON (`{}` allowed), the **text** the block markup as the block editor serializes it, entity-escaped like `j:code`. The element itself (ID, classes, styles) is not output; `render_block()`'s result takes its place.

```html
<div id="{intro}" wp:core--paragraph="{&quot;align&quot;:&quot;center&quot;}">&lt;!-- wp:paragraph {"align":"center"} --&gt;
&lt;p class="has-text-align-center"&gt;Hello&lt;/p&gt;
&lt;!-- /wp:paragraph --&gt;</div>

<div id="{latest}" wp:core--latest-posts="{&quot;postsToShow&quot;:3,&quot;displayPostDate&quot;:true}"></div>
```

| | Rule |
|---|---|
| Attribute name | `wp:` + namespace + `--` + block name: `core/post-title` → `wp:core--post-title`, `acme/card` → `wp:acme--card`. Adjacent or leading/trailing hyphens inside a name are separated with `_` (`review--vendor/widget` → `wp:review-_-vendor--widget`, `a/-b` → `wp:a--_-b`). Non-canonical names are refused |
| Attributes-only form | Empty text renders `<!-- wp:name {attrs} /-->`. Only blocks with **`attrsOnly: yes`** produce output this way (`wp jikji blocks list --attrs-only=yes` — `latest-posts`, `site-title`, `post-title`, `post-date`, `post-excerpt`, `post-featured-image`, `archives`, `categories`, `navigation`, `search`…). Blocks whose saved HTML *is* their content (`paragraph`, `heading`, `image`, `list`, `button`… — `attrsOnly: no`) output nothing from attributes alone and get a `jikji_block_needs_markup` warning; `unknown` (third-party) blocks are checked with `POST /render/block` first. `dynamic` is only the core's "has a server render callback" flag — `core/image` is dynamic yet `no` |
| Markup form | When text is present it is what renders; the attributes JSON is for display and tools. Exactly **one top-level block** whose name equals the attribute name. Do not hand-write markup of static blocks; take it from the editor's Block tab or from `content import` |
| With other attributes | Only `j:if`. Other `j:*` and child elements are refused. No Jikji elements inside an island — inside is Gutenberg, outside is Jikji |
| In a loop | An island inside `j:each` renders per item, and the nearest **post loop's item** is the block's post (a `core/post-title` inside a term loop inside a post loop shows that post's title). Outside a loop, the document's post (a template's sample post) |
| Unregistered blocks | Kept as they are (a plugin switched off); rendered as `render_block()` does (saved HTML or nothing); saves and checks warn `jikji_block_unregistered` |
| Assets | Block CSS (layout, block supports, block styles) goes into `head` as the core does; the canvas includes the same assets |

Moving block-editor content into a document: do not convert by hand — `wp jikji content import <id>` / `POST /content/<id>/import` ([Blocks 3.7](03-blocks.md#37-wordpress-blocks-block-islands)).

## 8.7 What a save refuses

| Rule | Error |
|---|---|
| The same ID twice in a document | `jikji_invalid_markup` |
| More than one of `j:text`, `j:html`, `j:shortcode` on an element | `jikji_invalid_markup` |
| `j:code` or `j:part` with disallowed `j:` attributes or children | `jikji_invalid_markup` |
| Two `wp:` attributes; a non-canonical block name; an attribute value that is not a JSON object; markup that is not one top-level block of the same name; `j:*` other than `j:if` or child elements on an island | `jikji_invalid_markup` |
| `j:*` inside or on an `<svg>` | `jikji_invalid_markup` |
| `j:attr:id`, `j:attr:class`, `j:attr:onclick`… | `jikji_invalid_markup` |
| A malformed `j:each`; loop variables `$this`, `$GLOBALS`, `$__jk_…` | `jikji_invalid_markup` |
| A PHP syntax error in the compiled template (brackets, quotes) | `jikji_syntax_error` — `data.id` and `data.attr` locate it |
| A style name that cannot be stored; a class named like an element ID; a type change of a name | `jikji_invalid_style_name`, `jikji_class_is_id`, `jikji_selector_conflict` |

`wp jikji content validate` / `POST /content/validate` run the same checks without saving.

## 8.8 Helper summary

```text
jikji_post( field, $post [, format|size] )     jikji_site( field )
jikji_meta( key, $post )                       jikji_archive( field )
jikji_term_list( taxonomy, $post [, sep] )     jikji_main_query()
jikji_terms( taxonomy, $post )                 jikji_pagination( [json] )
jikji_term_query( json [, $post] )             jikji_template_part( slug )
jikji_term( field, $term )
jikji_field( name, $post [, 'url'|'id'|'text'] )
jikji_row( sub, $row [, 'url'|'id'|'text'] )
jikji_query( json, $post )
```

Details and field lists are in [Dynamic data](05-dynamic-data.md).
