Switch branches/tags
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
521 lines (384 sloc) 11.2 KB

Core tags and attributes

Marko provides a number of tags

Control flow

<if>, <else-if>, <else>

The <if>, <else-if>, and <else> tags provide conditional control-flow for templates.

    <div>Hey there</div>
    <div>Bye now</div>
    <div>What's up?</div>

Conditionals may also be applied as attributes:

<div if(arriving)>Hey there</div>
<div else-if(leaving)>Bye now</div>
<div else>What's up?</div>

And support complex expressions:

<if(Math.random() > 0.5)>
    <div>50-50 chance of seeing this</div>


The <for> tag allows iterating over an array of items:

    <for(color in colors)>

It may also be applied as an attribute:

    <li for(color in colors)>${color}</li>

With either of the above templates, and the following value for colors:

var colors = ["red", "green", "blue"];

The output HTML would be the following:


Loop Status Variable

The for directive also supports a loop status variable in case you need to know the current loop index. For example:

    <li for(color in colors | status-var=loop)>
        ${loop.getIndex()+1}) of ${loop.getLength()}
        <if(loop.isFirst())> - FIRST</if>
        <if(loop.isLast())> - LAST</if>
Loop Status Methods

Returns the length of the array


Returns the current loop index


Returns true if the current index is the first index, otherwise false


Returns true if the current index is the last index, otherwise false

Loop Separator

Used for separating values in a loop by characters. The first element will not be prefixed and the last element will not be suffixed with the separator:

    <!-- Output: red, green, blue -->
    <span for(color in colors | separator=", ") style="color: ${color}">

Range Looping

A range can be provided in the following format; <var-name> from <from> to <to>[ step <step>].

The from, to and step values must be numerical expressions. If not specified, step defaults to 1.

    <li for(i from 0 to 10)>
    <li for(i from 0 to 10 step 2)>
    <li for(i from 0 to myArray.length-1)>

Property Looping

    <li for(name, value in settings)>
        <b>${name}</b>: ${value}

Native JavaScript for-loop

<for(var i=1; i<=3; i++)>

Custom Iterator

static function reverseIterator(arrayList, callback) {
    for(var i=arrayList.length-1; i>=0; i--){

<div for(item in ['a', 'b', 'c'] | iterator=reverseIterator)>




Any element can be repeated until a condition is met by using the while directive. The directive can be applied as an element or as an attribute.

Applied as an attribute:

$ var n = 0;

    <li while(n < 4)>

Applied as an element:

$ var n = 0;

    <while(n < 4)>


If you find that you have a wrapper element that is conditional, but whose body should always be rendered then you can use the body-only-if attribute to handle this use case. For example, to only render a wrapping <a> tag if there is a valid URL then you could do the following:

<a href=input.linkUrl body-only-if(!input.linkUrl)>
    Some body content

Given a value of "http://localhost/" for the input.linkUrl variable: , the output would be the following:

<a href="http://localhost/">
    Some body content

Given a value of undefined for the input.linkUrl variable: , the output would be the following:

Some body content


The following tags are always written using the concise syntax, even when using HTML syntax for tags that generate HTML output.


Static: The code generated by import will run once when the template is loaded and be shared by all calls to render. It must be declared as a top level tag and does not have access to data, state, or other values passed in at render.

The import tag is used to access data and functions from external files. It follows the same syntax as the JavaScript import statement.

import sum from './utils/sum';
<div>The sum of 2 + 3 is ${sum(2, 3)}</div>


<${useDiv ? 'div' : 'span'}>
    Hello there!

The <${dynamic}> tag is used to render a tag, component, or transcluded content that isn't determined until runtime.

Dynamic components

The <${dynamic}> tag can be used to embed another template in the current template:

import componentA from "./path/to/component-a.marko";
import componentB from "./path/to/component-b.marko";

<${useA ? componentA : componentB} name="Frank"/>

You can also switch between a normal HTML tag and a Marko component:

import FancyButton from "./path/to/fancy-button.marko";

<${isFancy ? FancyButton : 'button'}>
    Button text

Layouts and transcluded content

You can pass transcluded content to a tag using nested attribute tags which are denoted by the @ symbol:


        <h1>Hello Marko</h1>

Then in your layout template you can render the transcluded content using the <${dynamic}> tag:


<!doctype html>
        <!-- this comes from <@body> -->



Special HTML characters will be escaped. If you do not want escaping then use the <include-html> tag (see below).



Special HTML characters will not be escaped since the file is expected to be an HTML file.


Parameterized macros allow for reusable fragments within an HTML template. A macro can be defined using the <macro> directive.

<macro greeting(name, count)>
    Hello ${name}! You have ${count} new messages.

The above macro can then be invoked as part of any expression. The following sample template shows how to use macro functions inside expressions:

<macro greeting(name, count)>
    Hello ${name}! You have ${count} new messages.
    <greeting("John", 10)/>
    <!-- Or, using named attributes: -->
    <greeting name="Frank" count=20/>

Async content


The <await> tag is used to dynamically load in content from a data provider. The data provider can be a Promise or a callback. Once the provider returns it's results the children are rendered.


$ var personPromise = new Promise((resolve, reject) => {
    setTimeout(function() {
            name: 'Frank'
    }, 1000);

<await(person from personPromise)>
    <div>Hello ${}!</div>

Advanced implementation:

  • <await> tag signature
    • Basic usage: <await(results from dataProvider)>...</await>
    • Optional attributes
      • client-reorder boolean
      • arg expression
      • arg-* string
      • method string
      • timeout integer
      • timeout-message string
      • error-message string
      • placeholder string
      • renderTimeout function
      • renderError function
      • renderPlaceholder function
      • name string
      • scope expression
      • show-after string
    • Optional child tags
      • <await-placeholder>Loading...</await-placeholder>
      • <await-timeout>Request timed out</await-timeout>
      • <await-error>Request errored</await-error>


Standard HTML comments can be used to add comments to your template. The HTML comments will not show up in the rendered HTML.

Example comments:

<!-- This is a comment that will not be rendered -->
// You can also use standard JavaScript-style comments
 Block comments are also supported

If you would like for your HTML comment to show up in the final output then you can use the custom html-comment tag.



<html-comment>This is a comment that *will* be rendered</html-comment>


<!--This is a comment that *will* be rendered-->

Alternatively, the <marko-compiler-options> tag may be used to configure comments for the entire template:

<marko-compiler-options preserve-comments/>

Compiler options


Whitespace can be preserved using the preserve-whitespace attribute:

<div marko-preserve-whitespace>
    All of this
    whitespace   will
    be preserved.

Alternatively, the <marko-compiler-options> tag may be used to configure whitespace for the entire template:

<marko-compiler-options preserve-whitespace/>


The marko-body attribute can be used to control how body content is parsed. The following values are supported:

  • html - Body content will be parsed HTML (the default)
  • static-text - Body content will be parsed as static text (HTML tags will be ignored). Placeholders will be ignored.
  • parsed-text - Body content will be parsed as text (HTML tags will be ignored). Placeholders will not be ignored.


<div marko-body="static-text">
    This is just one
    <span if(foo)>
            Hello ${THIS IS NOT VALID}!
    big text block


  This is just one &lt;span if(foo)&gt; Hello ${THIS IS NOT VALID}!
  &lt;/span&gt; big text block