Marko Fundamentals

TL;DR
  • 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" -- Contact

Attributes 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 New
Note

See 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:

tags/card.marko
<div>
  <h3>Alice</h3>
  <p>Designer</p>
  <p>Joined 2023</p>
</div>
div
  h3 -- Alice
  p -- Designer
  p -- Joined 2023
index.marko
<h1>Our Team</h1>
<card/>
<card/>
<card/>
h1 -- Our Team
card
card
card
Note

Components in tags/ are discovered automatically, so no import is required.

Tip

Reusable markup snippets can also live in the same file with <define>.

Passing Data

Attributes passed to a component are available on input:

tags/card.marko
<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}
index.marko
<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=2022
Note

Components can also accept named sections of markup with attribute tags.

Core Tags

Marko ships built-in core tags for control flow and state. Use <if> and <else> for conditional markup:

tags/product.marko
<div>
  <h3>${input.name}</h3>
  <p>$${input.price}</p>
  <if=input.stock>
    <button>Add to Cart</button>
  </if>
  <else>
    <button disabled>Out of Stock</button>
  </else>
</div>
export interface Input {
  name: string;
  price: number;
  stock: number;
}

<div>
  <h3>${input.name}</h3>
  <p>$${input.price}</p>
  <if=input.stock>
    <button>Add to Cart</button>
  </if>
  <else>
    <button disabled>Out of Stock</button>
  </else>
</div>
div
  h3 -- ${input.name}
  p -- $${input.price}
  if=input.stock
    button -- Add to Cart
  else
    button disabled -- Out of Stock
export interface Input {
  name: string;
  price: number;
  stock: number;
}

div
  h3 -- ${input.name}
  p -- $${input.price}
  if=input.stock
    button -- Add to Cart
  else
    button disabled -- Out of Stock
index.marko
<product name="T-Shirt" price=25 stock=10/>
<product name="Hoodie" price=45 stock=0/>
<product name="Hat" price=15 stock=5/>
product name="T-Shirt" price=25 stock=10
product name="Hoodie" price=45 stock=0
product name="Hat" price=15 stock=5

Use <for> to render a list:

static const products = [
  { name: "T-Shirt", price: 25, stock: 10 },
  { name: "Hoodie", price: 45, stock: 0 },
  { name: "Hat", price: 15, stock: 5 },
];

<for|product| of=products>
  <product name=product.name price=product.price stock=product.stock/>
</for>
static const products = [
  { name: "T-Shirt", price: 25, stock: 10 },
  { name: "Hoodie", price: 45, stock: 0 },
  { name: "Hat", price: 15, stock: 5 },
];

for|product| of=products
  product name=product.name price=product.price stock=product.stock

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.