Marko Fundamentals
- Marko builds on HTML with JavaScript expressions and component tags
- Files in
tags/become reusable components automatically - Core tags like
<if>and<for>handle control flow directly in markup
Marko feels familiar to anyone who writes HTML, while adding dynamic expressions, components, and control flow. Let's walk through the fundamentals.
Templates are HTML
Nearly any valid HTML is valid Marko. We can start with plain HTML and layer in dynamic features only where needed:
<h1>My Blog</h1>
<nav><a href="/about">About</a><a href="/contact">Contact</a></nav>h1 -- My Blog
nav
a href="/about" -- About
a href="/contact" -- ContactAttributes are JS
Attribute values accept JavaScript expressions directly:
<a href=`/user/${input.user.id}`>Profile</a>
<a href=getRandomPage()>Discover Something New</a>a href=`/user/${input.user.id}` -- Profile
a href=getRandomPage() -- Discover Something NewSee Attributes for ...spreads, methods(), and other shorthands.
Dynamic Content
Tag content behaves like a JavaScript template literal, so dynamic values interpolate with ${expression}:
<p>Today is ${new Date().toDateString()}</p>
<p>Random number: ${Math.floor(Math.random() * 100)}</p>p -- Today is ${new Date().toDateString()}
p -- Random number: ${Math.floor(Math.random() * 100)}Components
Each .marko file is a component that can be used like an HTML tag. Placing a file in a tags/ directory makes it available by filename:
<div>
<h3>Alice</h3>
<p>Designer</p>
<p>Joined 2023</p>
</div>div
h3 -- Alice
p -- Designer
p -- Joined 2023<h1>Our Team</h1>
<card/>
<card/>
<card/>h1 -- Our Team
card
card
cardComponents in tags/ are discovered automatically, so no import is required.
Reusable markup snippets can also live in the same file with <define>.
Passing Data
Attributes passed to a component are available on input:
<div>
<h3>${input.name}</h3>
<p>${input.role}</p>
<p>Joined ${input.year}</p>
</div>export interface Input {
name: string;
role: string;
year: number;
}
<div>
<h3>${input.name}</h3>
<p>${input.role}</p>
<p>Joined ${input.year}</p>
</div>div
h3 -- ${input.name}
p -- ${input.role}
p -- Joined ${input.year}export interface Input {
name: string;
role: string;
year: number;
}
div
h3 -- ${input.name}
p -- ${input.role}
p -- Joined ${input.year}<h1>Our Team</h1>
<card name="Alice" role="Designer" year=2023/>
<card name="Bob" role="Developer" year=2024/>
<card name="Charlie" role="Product Manager" year=2022/>h1 -- Our Team
card name="Alice" role="Designer" year=2023
card name="Bob" role="Developer" year=2024
card name="Charlie" role="Product Manager" year=2022Components can also accept named sections of markup with attribute tags.
Next Steps
Contributors
Helpful? You can thank these awesome people! You can also edit this doc if you see any issues or want to improve it.