aicoded.dev

Template syntax: tags and attributes

A page's index.html is HTML with ssr: tags, ssr: attributes and {{ }} expressions. aicoded generate turns it into Go code, checks it, and refuses anything that could run code in the browser or write a value where escaping cannot make it safe.

Tags

Every tag is written <ssr:name …/>. An unknown ssr: tag is an error (E-GEN-004), and ssr: attributes do not work on ssr: tags (E-GEN-005).

Tag What it does
<ssr:access role="a,b" guard="true"/> who may open the page; see access
<ssr:access role="a,b" shared="true"/> who may open a page with an id in its URL that shows every record to everyone it admits; see access
<ssr:var name="…" type="…"/> declares a value the page's data provider fills
<ssr:content default="…"/> where the page below a layout is shown; see routing
<ssr:assets/> the script and style tags of the page and the pages inside it; see assets
<ssr:form name="…"> a form that posts to the page; see forms
<ssr:input>, <ssr:select>, <ssr:textarea> the fields of a form
<ssr:json name="…" value="…"/> passes a value to the page's script as JSON
<ssr:call name="…" in="…" out="…"/> a function the page's script may call; see page calls

<ssr:var>

<ssr:var name="users" type="[]User"/>
<ssr:var name="intro" type="web.SafeHTML"/>
<ssr:var name="tabClass" type="func(string) string"/>
<ssr:var name="userCount" type="int" reactive="true"/>
<ssr:var name="displayName" type="string" reactive="true" client-writable="true"/>

Declarations may stand anywhere in the template; where they stand changes nothing.

<ssr:json>

<ssr:var name="langs" type="[]string"/>
<ssr:json name="langs-data" value="langs"/>

It renders <script type="application/json" id="langs-data">["Go","TypeScript","Rust"]</script>. A script reads it with JSON.parse(document.getElementById("langs-data")?.textContent ?? "[]"). The name is lower-case letters, digits and -, used once per template, and the value is an expression written without {{ }} (E-GEN-033). Browsers never run it, so it is the way to hand data to a script.

Attributes on HTML elements

Attribute What it does
ssr:if="expr" renders the element only when expr is true
ssr:else-if="expr" follows an ssr:if or ssr:else-if element
ssr:else follows an ssr:if or ssr:else-if element
ssr:for="item in list", ssr:for="i, item in list" repeats the element for each item
ssr:bind="name" keeps a native input in step with a client-writable live value
<span ssr:if="user.Age <= 18">0-18</span>
<span ssr:else-if="user.Age <= 30">19-30</span>
<!-- the older groups -->
<span ssr:else>31+</span>

<li ssr:for="u in users"><a href="/users/{{ u.Login }}">{{ u.Name }}</a></li>
<li ssr:for="i, lang in langs">#{{ i }}: {{ lang }}</li>

Expressions

{{ expr }} writes a value into text or into a quoted attribute value, escaped for where it lands. {{$ expr }} writes markup as it is, and takes only web.SafeHTML. An attribute value may mix fixed text and several expressions:

<p>Total: {{ price * quantity }}</p>
<a class="{{ u.Login == current ? 'active' : '' }}" href="/users/{{ u.Login }}">{{ u.Name }}</a>

See expressions for the language and the escaping.

What the generator refuses

Pages run under a strict Content-Security-Policy, which blocks inline code, and the generator refuses it before the browser would:

It also refuses a value where no escaping makes it safe:

Attributes starting with data-ssr- are the generated code's own (E-GEN-042).

Whitespace, comments and the document

Images

An <img src> that names a file next to the template, such as src="logo.svg", or src="/assets/<file>" in the app's assets/ folder, is copied into the app with a hash in its name and served from there. See assets.

See also

Markdown · Source on GitHub · framework f46741f