#Templating
#Anatomy
Everything is a component, Riot refers to them as tags Tags have to be mounted
<example>
<b>Markup</b>
<script>
// Script
</script>
</example>
#Expressions
#Pure JavaScript
Can contain any javascript except curly brackets
Random number: {Math.random() * 10}
© <time datetime={new Date().getFullYear()}></time>
How long is a day in seconds? {60*60*24}
#Accessing tag properties
My name is {author.name}
and I'm {author.age} {unit}s old
<script>
this.author = {
name: 'Martin',
age: 25
}
this.unit = 'year'
</script>
#Foreach - loop data
#Array
<nav>
<a href="#{doc}" each={doc in docs}>
{doc}
</a>
</nav>
<script>
this.docs = [
'templating',
'tag',
'mixin',
'observable',
'router'
]
</script>
You can access both index and value by providing a second argument
<nav>
<a href="#{doc}" each={doc, index in docs}>
{index + 1} - {doc}
</a>
</nav>
#Object
Used for more complex structures, where each item has a distinct key
Objects use different order of key, value
in the each statement
<card size={card.size} name={title}
each={title, card in cards}>
</card>
<script>
this.cards = {
analytics : {
size: 1,
toolbar: ['reset']
},
posts: {
size: 2,
toolbar: ['add', 'list']
}
}
</script>
#Virtual
The virtual tag is used for loops that should generate no wrapper markup
<dl>
<virtual each={item in items}>
<dt>{item.key}</dt>
<dd>{item.value}</dd>
</virtual>
</dl>
#Conditionals
#Shorthand ternary
<div class={active: item.active}></div>
#Ternary
<div class={item.active ? 'active' : ''}></div>
#Blocklevel
Does not write HTML if condition is false
<div if={shop.items.length}></div>
#Hide
Writes HTML, just sets display
style to none
if condition is true
<nav hide={mobile}></nav>
#Show
Opposite of Hide display
<nav show={mobile}></nav>
#Access elements and tags
#HTML Elements
You can also use id
if you are not comfortable with name
<input name="todo">
<script>
this.todo.value = 'write todolist'
</script>
#Child tags
Access via name
or id
<todo-item name="first"></todo-item>
<script>
this.tags.first
</script>
If there are more instances, you get an array of tags
<todo-item></todo-item>
<todo-item></todo-item>
<todo-item name="last"></todo-item>
<script>
this.tags['todo-item'] // Array<Tag> - 2
this.tags.last // <Tag> - 1
</script>
#Options
Options can be passed via html params or on mount
Options only accept boolean
, number
, string
or simple array
, when passing values directly
#Passing values directly per HTML
<todo-item name="Finish Cheatsheet" done={false}>
</todo-item>
<script>
// Script of todo-item
this.opts.name // 'Finish Cheatsheet'
this.opts.done // false
</script>
#Passing variables per HTML
<todo-item item={item}></todo-item>
<script>
this.item = {
name: 'Study riot',
done: true
}
</script>
#Passing values on Mount
On mount, we are more flexible, since we are in js See mount
var items = [
{name: 'Share', done: true},
{name: 'Star', done: true},
{name: 'Work', done: false},
]
riot.mount('todo-list', items)
#Yield
Yielding is like options, just that it accepts HTML and other riot tags
Definition
<popup-body>
<yield>
</popup-body>
Usage
<popup-body>
Hi! I'm supporting
<abbr title="Hypertext Markup Language">HTML</abbr>
</popup-body>
#Multiple Yieldpoints
Notice: This feature is supported in Riot 2.3.12 or later
Usage
<card>
<yield to="toolbar">
<a>Add post</a>
<a>Recently published</a>
</yield>
<yield to="header">
<i class="fa fa-text"></i> Posts
</yield>
</card>
Definition
<card>
<h2>
<yield from="header" />
</h2>
<div class="toolbar">
<yield from="toolbar" />
</div>
</card>