Templates and layouts

Templates are reusable pieces of Twig markup, layouts wrap your pages in a shared header and footer, and the editor tells you where each page's CSS and JavaScript actually come from.

What they are

Templates are posts of the Templates type (wpcanai_template). Each one has a template type that decides when CanAI uses it:

A layout is a template of type layout. It holds the page shell (head, header, footer) and prints the page's own markup where it says {{ page_content }}. Pages and templates choose a layout in the CanAI Layout box; if they choose none, the Default Layout applies (CanAI → Settings, section "Default Layout"). A page with no layout and no default layout renders on its own.

A delegate page is for structural templates (shop, product-category, cart, checkout, my-account, order-received, category, tag, author, search, archive). The template can name a page whose layout and content it should use. The Delegate Page dropdown only appears for those types.

Use a layout whenever two or more pages share chrome. Put shared CSS and JavaScript on the layout so it is loaded once for every page that uses it.

Where you edit

Templates and Twig pages are edited in the CanAI Editor box right on the normal WordPress edit screen (post.php). The standalone Tools → Editor page is still available. The editor has HTML, CSS, JavaScript and History tabs, plus Structure, Format and Codes Guide helpers. Side boxes cover Structure, CanAI Layout, Template Variables, Context and Compiled Tailwind.

Two special cases:

History is CanAI's own snapshot list, not WordPress revisions.

Where the CSS and JS come from

An empty CSS tab on a header or component is normal: the styles for pages using it usually live on the layout. CanAI shows this so you do not have to hunt.

Order of resolution for a page or template: its own code, then its layout (explicit choice, otherwise the Default Layout), then that layout's own parent layout.

Example

Create a layout, a page template, and ask an agent which styles reach the page.

{# layout (type: layout) #}
<!doctype html>
<html {{ language_attributes() }}>
<head>{{ wp_head() }}</head>
<body class="{{ body_class() }}">
  <header>{% for item in get_menu('primary') %}<a href="{{ item.url }}">{{ item.title }}</a>{% endfor %}</header>
  <main>{{ page_content }}</main>
  {{ wp_footer() }}
</body>
</html>

Over MCP:

{ "ability": "wpcanai/create-template",
  "input": { "title": "Site layout", "type": "layout", "html": "...", "css": "main{max-width:72rem}" } }

{ "ability": "wpcanai/read-meta",
  "input": { "post_id": 42, "fields": ["asset_sources"] } }

create-template also accepts layout (a layout post ID) for non-layout templates. read-meta with asset_sources returns, in output order, each post that contributes CSS or JS (post_id, title, role, kind, lines, delivery). Roles are self, layout, parent_layout and default_layout.

In wp-admin: CanAI → All Templates → Add New, pick the type, open the CanAI Editor box, then choose a layout in CanAI Layout.

If something looks wrong

Run CanAI → Tools → Diagnostics, or the wpcanai/scan ability, which reports:

Other common causes:

Related