Language Guide // MarkoImprove this page
Table of Contents
- Template Directives Overview
- Text Replacement
- Attributes
- Expressions
- Includes
- Variables
- Conditionals
- Looping
- Macros
- Structure Manipulation
- Comments
- Whitespace
- Helpers
- Miscellaneous
- Global Properties
- Custom Tags and Custom Attributes
- Async Taglib
- Layout Taglib
Template Directives Overview
Almost all of the Marko templating directives can be used as either an attribute or as an element. For example:
Applying directives using attributes:
<!-- Colors available --><ul ifcolors.length><li forcolor in colors></li></ul><!-- No colors available--><div else>No colors!</div>
Applying directives using elements:
<!-- Colors available --><ifcolors.length><ul><forcolor in colors><li></li></for></ul></if><!-- No colors available --><else><div>No colors!</div></else>
The disadvantage of using elements to control structural logic is that they change the nesting of the elements which can impact readability. For this reason it is often more suitable to apply directives as attributes.
Text Replacement
Dynamic text is supported using ${<javascript-expression>}
.
Examples:
Hello !Hello !
By default, all special HTML characters will be escaped in dynamic text to prevent Cross-site Scripting (XSS) Attacks. To disable HTML escaping, you can use $!
as shown in the following sample code:
Hello ! <!-- Do not escape -->
If necessary, you can escape $
using a forward slash to have it be treated as text instead of a placeholder token:
Test: \<!-- Rendered Output:Test: ${hello}-->
Attributes
All attribute values are parsed as JavaScript expressions. In addition, placeholders (${<javascript-expression>}
) are allowed in single and double quoted strings.
<div class=data.myClassName><input type="checkbox" checked=data.isChecked/><my-component string="Hello"/><my-component number=1/><my-component template-string="Hello "/><my-component boolean=true/><my-component array=1, 2, 3/><my-component object={hello: 'world'}/><my-component variable=name/><my-component function-call=data.foo/><my-component complex-expression=1+2/><my-component super-complex-expression=data.foo + data.bar'a', 'b', 'c'/>
In addition, Marko has special support for the class
and style
attributes as shown below:
Style attribute
The value of the style attribute can resolve to an object expression (in addition to a string value) as shown below:
<div style={color: 'red', 'font-weight': 'bold'}>
Output:
Class attribute
The value of the class attribute can resolve to an object expression or an array expression (in addition to a string value) as shown below:
<!-- array: --><div class='a', null, 'c'><!-- object: --><div class={a: true, b: false, c: true}>
In both cases, the output will be the same:
Expressions
Wherever expressions are allowed, they are treated as JavaScript expressions and copied out to the compiled template verbatim. For example:
<div ifsearchResults.length > 100>Show More</div>
Includes
Marko supports includes/partials. Other Marko files can be included using the <include>
tag and a relative path. For example:
<include"./greeting.marko" name="Frank" count=30/>
Alternatively, you can pass the template data by providing a JavaScript expression as a second argument to the include tag:
<include"./greeting.marko", {name: "Frank", count: 30}/>
The template expression provided as the first argument can also be a dynamic JavaScript expression that resolves to a loaded template as shown below:
In your JavaScript controller:
var myIncludeTarget = ;var anotherIncludeTarget = ;template;
And then in your template:
<includedata.myIncludeTarget name="Frank" count=30/><includedata.anotherIncludeTarget name="Frank" count=30/>
You can also choose to load the include target within the calling template as shown below:
<script marko-init>var myIncludeTarget = ;</script>...<includedata.myIncludeTarget name="Frank" count=30/>
Including static text
<include-text'./foo.txt'/>
NOTE: Special HTML characters will be escaped. If you do not want escaping then use the <include-html>
tag (see below)
Including static HTML
<include-html'./foo.html'/>
NOTE: Special HTML characters will not be escaped since the file is expected to be an HTML file.
Variables
Input data passed to a template is made available using a special data
variable. It's possible to declare your own variables as shown in the following sample code:
<var name=data.name.toUpperCase/>
To assign a new value to an existing variable the <assign>
tag can be used as shown in the following sample code:
<assign name=data.name.toLowerCase/>
The <var>
directive can also be used to create scoped variables as shown in the following sample code:
<var nameUpper=data.name.toUpperCase nameLower=data.name.toLowerCase>Hello !Hello !</var><!--nameUpper and nameLower will be undefined here-->
Conditionals
if...else-if...else
Any element or fragment of HTML can be made conditional using the following directives:
if
else-if
else
Applied as attributes:
<!-- Simple if --><div ifsomeCondition>Hello World</div><!-- Simple unless --><div unlesssomeCondition>Hello World</div><!-- Complex if --><div iftest === "a">A</div><div else-iftest === "b">B</div><div else-iftest === "c">C</div><div else>Something else</div><!-- Complex unless --><div unlesstest === "a">A</div><div else-iftest === "b">B</div><div else>Something else</div>
Applied as elements:
<!-- Colors available --><!-- Simple if --><ifsomeCondition><div>Hello World</div></if><!-- Complex if --><iftest === "a"><div>A</div></if><else-iftest === "b"><div>B</div></else-if><else-iftest === "c"><div>C</div></else-if><else><div>Something else</div></else>
unless...else-if...else
The unless
directive is also supported as an alternative to if
in cases where the condition should be negated.
<!-- Simple unless --><div unlesssomeCondition>Hello World</div><!-- Complex unless --><div unlesstest === "a">A</div><div else-iftest === "b">B</div><div else>Something else</div>
Applied as elements:
<!-- Simple unless --><unlesssomeCondition><div>Hello World</div></unless><!-- Complex unless --><unlesstest === "a"><div>A</div></unless><else-iftest === "b"><div>B</div></else-if><else-iftest === "c"><div>C</div></else-if><else><div>Something else</div></else>
Shorthand Conditionals
Regular JavaScript can be used to achieve shorthand conditional values inside attributes or wherever expressions are allowed:
<div class=active && 'tab-active'>Hello</div>
With a value of true
for active
, the output would be the following:
Hello
With a value of false
for active
, the output would be the following:
Hello
NOTE: If an attribute value expression evaluates to null
or false
then the attribute is not included in the output.
A ternary condition can also be used:
<div class=active ? 'tab-active' : 'tab-inactive'>Hello</div>
With a value of false
for active
, the output would be the following:
Hello
Conditional Attributes
Marko supports conditional attributes when the value of an attribute is an expression. Marko also supports HTML boolean
attributes (e.g., <input type="checkbox" checked>
) If an attribute value resolves to null
, undefined
, or false
then the attribute will not be rendered. If an attribute value resolves to true
then only the attribute name will rendered.
For example, given the following data:
active: truechecked: falsedisabled: true
And the following template:
<div class=data.active && "tab-active"/><input type="checkbox" checked=data.checked disabled=data.disabled/>
The output HTML will be the following:
Looping
for
Any element can be repeated for every item in an array using the for
directive. The directive can be applied as an element or as an attribute.
Applied as an attribute:
<ul><li foritem in items></li></ul>
Applied as an element:
<ul><foritem in items><li></li></for></ul>
Given the following value for items:
"red" "green" "blue"
The output would be the following:
redgreenblue
Loop Status Variable
The for
directive also supports a loop status variable in case you need to know the current loop index. For example:
<ul><li forcolor in colors | status-var=loop>) of<ifloop.isFirst> - FIRST</if><ifloop.isLast> - LAST</if></li></ul>
Loop Separator
<forcolor in colors | separator=", "></for><div><span forcolor in colors | separator=", " style="color: "></span></div>
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.
<ul><li fori from 0 to 10></li></ul>
<ul><li fori from 0 to 10 step 2></li></ul>
<ul><li fori from 0 to myArray.length-1></li></ul>
Property Looping
<ul><li forname,value in settings><b></b>:</li></ul>
Native JavaScript for-loop
<forvar i=1; i<=3; i++></for>
Custom Iterator
A custom iterator function can be passed as part of the view model to the template to control looping over data.
A sample custom iterator function that loops over an array in reverse is shown below:
{forvar i=arrayListlength-1; i>=0; i--;}
The custom iterator can then be used in a template as shown below:
Applied as part of a for
attribute:
<div foritem in 'a', 'b', 'c' | iterator=data.reverseIterator></div>
Output:
cba
Applied as part of a <for>
element:
<foritem in 'a', 'b', 'c' | iterator=data.reverseIterator></for>
cba
Custom iterators also support providing a custom status object for each loop iteration:
{var statusVar = first: 0 last: arrayListlength-1;forvar i=arrayListlength-1; i>=0; i--statusVarindex = i;;}
Applied as part of a for
attribute:
<div foritem in 'a', 'b', 'c' | iterator=data.reverseIterator status-var=status></div>
Output:
2c1b0a
Applied as part of a <for>
element:
<foritem in 'a', 'b', 'c' | iterator=data.reverseIterator status-var=status></for>
Output:
2c1b0a
while
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/><ul><li whilen < 4></li></ul>
Applied as an element:
<var n=0/><ul><whilen < 4><li></li></while></ul>
In both cases the output would be the following:
012
Macros
Parameterized macros allow for reusable fragments within an HTML template. A macro can be defined using the <macro>
directive.
macro
The <macro>
directive can be used to define a reusable function within a template.
<macro greetingname, count>Hello ! You have new messages.</macro>
The above macro can then be invoked as part of any expression. Alternatively, the <invoke>
directive can be used invoke a macro function using named attributes. The following sample template shows how to use macro functions inside expressions:
<macro greetingname, count>Hello ! You have new messages.</macro><p><greeting"John", 10/></p><p><!-- Or, using named attributes: --><greeting name="Frank" count=20/></p>
Structure Manipulation
Dynamic attributes
The attrs
attribute allows attributes to be dynamically added to an element at runtime. The value of the attrs attribute should be an expression that resolves to an object with properties that correspond to the dynamic attributes. For example:
<var myAttrs={style: "background-color: #FF0000;", "class": "my-div"} /><div >Hello World!</div>
Output:
Hello World!
body-only-if
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=data.linkUrl body-only-if!data.linkUrl>Some body content</a>
Given a value of "http://localhost/"
for the data.linkUrl
variable: , the output would be the following:
<a href="http://localhost/">Some body content</a>
Given a value of undefined
for the data.linkUrl
variable: , the output would be the following:
Some body content
Comments
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 --><h1>Hello</h1>
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><h1>Hello</h1>
Output:
<!--This is a comment that *will* be rendered--><h1>Hello</h1>
Whitespace
The Marko compiler will remove unnecessary whitespace based on some builtin rules, by default. These rules are partially based on the rules that browser's use to normalize whitespace and partially based on the goal of allowing nicely indented markup with minified output. These rules are as follows:
- For text before the first child element:
text.replace(/^\n\s*/g, '')
- For text after the last child element:
text.replace(/\n\s*$/g, '')
- For text between child elements:
text.replace(/^\n\s*$/g, '')
- Any contiguous sequence of whitespace characters is collapsed into a single space character
In addition, whitespace within the following tags is preserved by default:
<pre>
<textarea>
<script>
Example template:
<div><a href="/home">Home</a><a href="/Profile">My Profile</a><textarea>HelloWorld</textarea></div>
Example output:
<div><a href="/home">Home</a><a href="/Profile">My Profile</a><textarea>HelloWorld</textarea</div>
The following options are available to control whitespace removal:
Option 1) Disable whitespace removal using the marko-compiler-options
tag:
<marko-compiler-options preserve-whitespace/><div><img src="foo.jpg"/><img src="foo.jpg"/></div>
Option 2) Disable whitespace removal using the marko-preserve-whitespace
attribute:
<div marko-preserve-whitespace><img src="foo.jpg"/><img src="foo.jpg"/></div>
NOTE: When whitespace preservation is enabled, whitespace will be preserved for text at any level below the tag that the marko-preserve-whitespace
attribute is attached to.
Option 3) Disable all whitespace removal by changing a compiler option
defaultOptionspreserveWhitespace = true;
Option 4) Control whitespace removal for specific tags (in marko.json
/marko-tag.json
)
Adding the "preserve-whitespace": true
property to a tag definition will result in the Marko compiler preserving whitespace wherever that tag is encountered in a template.
"<my-custom-tag>":"preserve-whitespace": true
NOTE: When whitespace preservation is enabled, whitespace will be preserved for text at any level below the tag.
Helpers
Since Marko template files compile into CommonJS modules, any Node.js module can be "imported" into a template for use as a helper module. For example, given the following helper module:
src/util.js:
exports {var out = "";for var i=strlength-1; i>=0; i--out += str;return out;};
The above module can then be imported into a template as shown in the following sample template:
src/template.marko:
<script marko-init>var util = ;</script><div></div>
It's also possible to pass helper functions to a template as part of the view model:
var template = ;template;
Usage inside template:
<div></div>
Miscellaneous
invoke
The <invoke>
directive can be used to invoke a standard JavaScript function during rendering:
<invoke console.log'Hello World'/>
Global Properties
The $global
property is used to add data that is available to all templates encountered during rendering by having the data hang off the wrapped writer.
template;
Given the following template:
<div>Hello !</div>
The output would be the following:
<div>Hello Frank</div>
Custom Tags and Custom Attributes
Marko supports extending the language with custom tags and attributes. A custom tag or a custom attribute must have at least one dash to indicate that is not part of the standard HTML grammar.
Below illustrates how to use a simple custom tag:
<div><my-hello name="Frank" message-count="30"/></div>
The output of the above template might be the following:
Hello Frank! You have 30 new messages.
With Marko, attribute values can be JavaScript expressions:
<div><my-hello name=data.name.toUpperCase message-count=data.messageCount/></div>
You can also pass a data object by providing a JavaScript expression as an argument to the custom tag:
<div><my-hello{name: "Frank", messageCount: "30"}/></div>
For information on how to use and create custom taglibs, please see the documentation for Custom Taglibs.
Async Taglib
The async taglib provides an <await>
tag that allows portions of your template to be rendered asynchronously. An asynchronous fragment can be bound to a function that accepts an "args" objects and callback argument. When the data provider function completes and invokes the callback with the resulting data, the body of the async fragment is then rendered with the asynchronous data assigned to the specified variable. As an additional feature, asynchronous fragments allow parts of your page to render out-of-order while still providing the final HTML in the correct order allowing to have very reactive websites with almost instant visual feedback. Features like out-of-order rendering, that are based on client-reordering, require the use of JavaScript. Websites that have to render completely without JavaScript should avoid using this additional feature (they can still use asynchronous fragments though).
Example:
template;
<awaituserProfile from data.userProfilePromise><ul><li>First name:</li><li>Last name:</li><li>Email address:</li></ul></await>
For more details, please see Marko Async Taglib.
Layout Taglib
Marko provides a layout
taglib to support separating out layout from content. The usage of the layout
taglib is shown in the sample code below:
default-layout.marko:
<html lang="en"><head><meta charset="UTF-8"><title><layout-placeholder name="title"/></title></head><body><h1 ifdata.showHeader !== false><layout-placeholder name="title"/></h1><p><layout-placeholder name="body"/></p><div><layout-placeholder name="footer">Default Footer</layout-placeholder></div></body></html>
Usage of default-layout.marko
:
<layout-use"./default-layout.marko" show-header=true><layout-put into="title">My Page</layout-put><layout-put into="body">BODY CONTENT</layout-put></layout-use>
For more details, please see Marko Layout Taglib.