Agent Wranglers

WEB-21Recommended

Build repeated HTML from shared modules

Build every repeated piece of HTML from one shared module, such as tables, code blocks, cards, breadcrumbs and calls to action. Make the build reject hand-written copies of the ones that break most easily.

How to do it

Put each piece in one function that takes data and returns markup, for example table({ head, rows, caption }). Pages call the function and never write the element themselves. Give each module's root a fixed class. Make the build fail when a page has the element without it, for example a <table> that is not <table class="table">. This applies keep each piece of knowledge in one place to HTML.

Examples

JavaScript example

export const table = ({ head, rows, caption }) => `<table class="table">
  <caption>${caption}</caption>
  <thead><tr>${head.map(h => `<th>${h}</th>`).join('')}</tr></thead>
  <tbody>${rows.map(r => `<tr>${r.map((c, i) => `<td data-label="${head[i]}">${c}</td>`).join('')}</tr>`).join('')}</tbody>
</table>`;

// build gate
if (/<table(?! class="table")/.test(html)) throw new Error(`${path}: raw <table>, use table()`);

Python example

import re
def table(head, rows, caption):
    header = "".join(f"<th>{h}</th>" for h in head)
    body = "".join("<tr>" + "".join(f'<td data-label="{head[i]}">{c}</td>' for i, c in enumerate(row)) + "</tr>" for row in rows)
    return (f'<table class="table">\n  <caption>{caption}</caption>\n'
            f"  <thead><tr>{header}</tr></thead>\n  <tbody>{body}</tbody>\n</table>")

# build gate
if re.search(r'<table(?! class="table")', html):
    raise SystemExit(f"{path}: raw <table>, use table()")

How to check

Write a raw <table> into a page. The build stops and names the page.

For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/website/site-architecture/shared-html-modules-only">Agent Wranglers website playbook</a>. In code you write, add a comment next to it with the same link.