{define}

Writes a custom tag in the template itself - what a _tags/name.pad file is, without the file.

Layouts and components Tag pairs

Syntax

{define 'name'} ... {#parm} ... @content@ ... {/define}
{define 'name'} {parms title, tone='info'} ... {slot 'footer'}@content@{/slot} {/define}
{name parm='value'}
{name parm='value'} content {slot 'footer'}fill{/slot} {/name}
{define:name ...}

Parameters

name
The name of the new tag. Under the strict check it may not be a built-in tag, an application tag or a {macro} already.

Description

The body between {define 'card'} and {/define} becomes the template of a new tag, {card}, exactly as if it stood in _tags/card.pad. Defining prints nothing; every use of the tag renders the body, where the use stands and with the fields there.

Everything a file-based custom tag can do works in the body: {#name} reads a parameter of the use, {parms} declares them (a bare name required, name=default filling in), @content@ takes the content of a pair use in, and {slot}s take named fills. A define can use itself, for rows below the current one.

The body is kept as written and rendered at each use, so a field in it is read at the use, not at the definition. A define exists from the point it is walked - like a {content} or {data} store - so a use above the {define} is an unknown tag. {define:card} asserts that the name is a define.

Under the strict check a name that is a built-in or application tag, or already a {macro}, is an error, and so is a define without a name or without a body. Use a define for markup with a frame - content and slots; use a macro when the parameters should read as plain fields.

Examples

A card with content

The body is the tag's template: {#title} reads a parameter, @content@ takes the content in.

Templateexamples/define/1-card.pad
{define 'defineCard'}
  <div style="border:1px solid #ccc; border-radius:8px; padding:8px 12px; margin:6px 0">
    <strong>{#title}</strong>
    <div>@content@</div>
  </div>
{/define}

{defineCard title='Revenue'}€ 12,400 this month{/defineCard}
{defineCard title='Orders'}86 new, 4 open{/defineCard}
Resultopen alone
Revenue
€ 12,400 this month
Orders
86 new, 4 open

Parameters and a slot

{parms} gives a default, and a {slot} takes a fill from the use - or shows its own default.

Templateexamples/define/2-parms-slot.pad
{define 'defineNote'}
  {parms text, tone='info'}
  <p style="margin:6px 0">[{#tone}] {#text}
    {slot 'more'}<em>no details</em>{/slot}</p>
{/define}

{defineNote text='Backup finished'}{/defineNote}
{defineNote text='Disk almost full', tone='warning'}{slot 'more'}<b>92% used</b>{/slot}{/defineNote}
Resultopen alone

[info] Backup finished no details

[warning] Disk almost full 92% used

See also

  • {macro} A template function: declared parameters, given in order or by name, read as fields inside its body.
  • {parms} Declares the parameters of a custom tag, with defaults, at the top of its template.
  • {slot} A named place in a custom tag's template - and the fill a caller gives it.
  • {content} Keeps a piece of template under a name instead of printing it, for a later {name} to render.