We're used to passing body content to HTML tags. When you do this, the tag has control over where and when this content is rendered. A good example of this is the HTML <details>
element:
Hello World This is somecontent that can be toggled.
This is what it renders (try clicking it):
Custom tags can also receive content in the same way. This allows a component to give its user full control over how some section of the content is rendered, but control where, when, and with what data it is rendered. This feature is necessary to build composable components like overlays, layouts, dropdowns, etc. Imagine a <table>
that didn't give you control over how its cells were rendered. That would be pretty limited!
When a custom tag is passed body content, it is received as a special renderBody
property on the component's input
. You can include this content anywhere in your component by using the <${dynamic}>
syntax.
<div class="container fancy"></></div>
div class="container fancy"</>
If we were to use this tag like this:
<fancy-container><p>Content goes here...</p></fancy-container>
fancy-containerp -- Content goes here...
The rendered output would be:
Content goes here...
This is a pretty basic example, but you can imagine how this could be incorporated into a more advanced component to render passed content where/when needed.
ProTip: Body content can be rendered multiple times. Or not at all.
When rendering body content with <${dynamic}>
, attributes may also be passed:
<!-- heh, it's not actually random -->< number=1337 />
// heh, it's not actually random< number=1337/>
These attribute values can be received as a tag parameter:
<random-value{ number }>The number is</random-value>
random-value{ number } -- The number is
ProTip: Some tags (like the above tag) may not render anything except their body content with some data. This can be quite useful, just look at the
<for>
and<await>
tags!
You can also pass named content sections to a tag using attribute tags which are denoted by the @
prefix.
<layout><@heading><h1>Hello Marko</h1></@heading><@content><p>...</p></@content></layout>
layout@headingh1 -- Hello Marko@contentp -- ...
Like attributes, these attribute tags are received as input.heading
and input.content
, but they each have a renderBody
property which we can now use:
<html><body></><hr/></></body></html>
htmlbody</>hr</>
ProTip: The
renderBody
property can be omitted. You could use<${input.heading}/>
, for example.
It is sometimes useful to allow multiple of the same attribute tag to be passed. This would allow us to, for example, build a custom table component which would allow its user to specify any number of columns, while still giving ther user control over how each column is rendered:
<fancy-table data=people><@columnperson>Name:</@column><@columnperson>Age:</@column></fancy-table>
fancy-table data=people@columnperson -- Name:@columnperson -- Age:
In order to receive multiple of the same attribute tag, you need to specify that the attribute tag can be repeated in a marko-tag.json
file.
"@data": "array""<column>":"is-repeated": true
We can then use the <for>
tag to render the body content into table, passing the row data to each column's body.
<table class="fancy"><forrow of=input.data><tr><forcolumn of=input.column><td>< ...row/></td></for></tr></for></table>
table class="fancy"forrow of=input.datatrforcolumn of=input.columntd< ...row/>
We now have a working <fancy-table>
. Let's see what it renders:
name: "Patrick"age: 63name: "Austin"age: 12;
Name: PatrickAge: 63Name: AustinAge: 12
If you look at our previous example, we had to prefix each cell with the column label. It would be better if we could give a name to each column instead and only render that once.
<fancy-table><@columnperson heading="Name"></@column><@columnperson heading="Age"></@column></fancy-table>
fancy-table@columnperson heading="Name" --@columnperson heading="Age" --
Now, each object in the input.column
array will contain a heading
property in addition to its renderBody
. We can use another <for>
and render the headings in <th>
tags:
<table class="fancy"><tr><forcolumn of=input.column><th></th></for></tr><forrow of=input.data><tr><forcolumn of=input.column><td>< ...row/></td></for></tr></for></table>
table class="fancy"trforcolumn of=input.columnth --forrow of=input.datatrforcolumn of=input.columntd< ...row/>
We'll now get a row of headings when we render our <fancy-table>
NameAgePatrick63Austin12
Continuing to build on our example, what if we want to add some custom content or even components into the column headings? In this case, we can extend our <fancy-table>
to use nested attribute tags. We'll now have <@heading>
and <@cell>
tags nested under <@column>
. This gives users of our tag full control over how to render both column headings and the cells within the column!
<fancy-table><@column><@heading><app-icon type="profile"/> Name</@heading><@cellperson></@cell></@column><@column><@heading><app-icon type="calendar"/> Age</@heading><@cellperson></@cell></@column></fancy-table>
fancy-table@column@headingapp-icon type="profile"-- Name@cellperson --@column@headingapp-icon type="calendar"-- Age@cellperson --
Now instead of rendering the heading as text, we'll render the heading's body content.
<table class="fancy"><tr><forcolumn of=input.column><th></></th></for></tr><forrow of=input.data><tr><forcolumn of=input.column><td>< ...row/></td></for></tr></for></table>
table class="fancy"trforcolumn of=input.columnth</>forrow of=input.datatrforcolumn of=input.columntd< ...row/>
Our headings can now include icons (and anything else)!
NameAgePatrick63Austin12
The flexibility of the <fancy-table>
is great if you want to render columns differently or have columns that display the data in a special way (such as displaying an age derived from a date of birth). However, if all columns are basically the same, the user might feel they're repeating themselves. As you might expect, you can use <for>
(and <if>
) to dynamically render attribute tags.
<fancy-table><for{ property, title, icon }><@column><@heading><app-icon type=icon/></@heading><@cellperson></@cell></@column></for></fancy-table>
fancy-tablefor{ property, title, icon }@column@heading@cellperson --
Helpful? You can thank these awesome people! You can also edit this doc if you see any issues or want to improve it.