The Compiler API // MarkoImprove this page
require('marko/compiler')
Methods
buildTaglibLookup(dirname)
Returns a TaglibLookup for discovering custom tags available to a template in the given directory.
Example usage:
var taglibLookup =;taglibLookup;taglibLookup;
compile(src, filename, options, callback)
compileFile(filename, options, callback)
createBuilder(options)
createWalker(options)
parseRaw(templateSrc, filename)
Properties
taglibLookup
Returns a reference to the taglib-lookup module.
taglibLoader
taglibFinder
taglib-lookup
Methods
registerTaglib = registerTaglib;
buildLookup(dirname)
clearCache();
AST
Node
The Node
type is the base class for all AST nodes. All AST nodes added to the AST must extend Node
.
Methods
wrap(wrapperNode)
Makes the current node a child of the provided wrapperNode
. Similar to the following:
thiscontainer;wrapperNode;
makeContainer(array)
Converts the provided Array
into an ArrayContainer
. If the provided Array
is already an instance of a Container
then it is simply returned.
appendChild(node)
Appends a child node to the associated container for the node. The this.body
property is used as the default container, but this method can be overridden in derived nodes.
Properties
type
The node type as a String. Example node types: "TemplateRoot"
, "HtmlElement"
, "Text"
, "If"
, "Else"
, "ForEach"
, etc.
container
If a Node
is the child of another Node
then it will be associated with a Container
. For example:
if thiscontainervar parentNode = thiscontainernode;// NOTE: The following lines produce the same result:thiscontainerthiselse// Either the node is the root node or it is detached from the AST
Container
TemplateRoot
HtmlElement
Text
JavaScript node types
Builder
methods
arrayExpression(elements)
assignment(left, right)
Returns a node that generates the following code:
<left> = <right>;
For example:
builder;// Output code:foo = '123';
binaryExpression(left, operator, right)
Returns a node that generates the following code:
<left> <operator> <right>
For example:
builder;// Output code:foo < 99;
code(value)
Returns a node that writes out arbitrary JavaScript code with the given value. The indentation of the provided code is adjusted for proper formatting.
For example:
builder// Output code:var a = 1;var b = 2;b = 3;
conditionalExpression(test, consequent, alternate)
Returns a node that generates the following code:
<test> ? <consequent> : <alternate>
For example:
builder;// Output code:isHidden ? "hidden" : "visible"
elseStatement(body)
Returns a node that generates the following code:
else<body>
For example:
builder;// Output code:elseconsole;console;
elseIfStatement(test, body, elseStatement)
Returns a node that generates the following code:
else if <test><body><elseStatement>
For example:
builder;// Output code:else if trueconsole;console;
forEach(def)
Returns a node that generates code to loop over an array, object properties or a range.
array:
builder// Output code:var forEach = __helpersf; // Static variable// ...;
forEach(varName, target, body)
Returns a node that generates a simple forEach
. See forEach(def)
.
forRange(def)
Returns a node that generates code to loop over a number range
array:
builder// Output code:{for var i = 0; i<=myArraylength; i+=2console;};
forRange(varName, from, to, step, body)
Returns a node that generates a simple forRange
. See forRange(def)
.
forStatement(init, test, update, body)
Returns a node that generates the following code:
for <init>; <test>; <update><body>
For example:
builder// Output:for var i = 0; i < 0; i++console;
functionCall(callee, args)
Returns a node that generates the following code:
<callee><arg1 arg2 ... argN>
builder;// Output:console;
functionDeclaration(name, params, body)
Returns a node that generates the following code:
{<body>}
Named function declaration:
builder;// Output:{return num1 + num2;}
Anonymous function declaration:
builder;// Output:{return num1 + num2;}
html(argument)
Returns a node that renders a fragment of HTML (special HTML characters will not be escaped):
builder;// Output:out;
htmlComment(comment)
builder// Output:out;
htmlElement(tagName, attributes, body, argument, openTagOnly, selfClosed)
builder// Output:out;
identifier(name)
Returns a node that generates the code for a JavaScript identifier code (e.g., a variable name, parameter name, property name, etc.)
For example:
builder// Output code:foo = "abc"
ifStatement(test, body, elseStatement)
Returns a node that generates the following code:
if <test><body><elseStatement>
For example:
builder;// Output code:if trueconsole;console;
invokeMacro(name, args, body)
Returns a node to generate the code to invoke a macro with the given name, args and body
For example:
builder
invokeMacroFromEl(el)
Returns a node to generate the code to invoke a macro based on the provided HtmlElement
node.
For example:
var el = builder;builder
literal(value)
Returns a node to generate a JavaScript code for literal strings, numbers, booleans, objects and arrays.
For example:
builder;// Output code:"abc"
Or, for a more complex example:
builder// Output code:var aString = "abc"aNumber = 123aBoolean = falseanObject ="foo": "bar""dynamic": datanameanArray ="foo"dataname
logicalExpression(left, operator, right)
macro(name, params, body)
Returns a node that generates a macro function with the given name, params and body content. The InvokeMacro
node should be used to generate the code to invoke the macro.
negate(argument)
Returns a node that generates the following code:
!<argument>
For example:
builder// Output:!foo
newExpression(callee, args)
node([type, ]generatCode)
Returns a generic Node
instance with the given node type (optional) and a generateCode(node, generator)
function that should be used to generate the code for the node. If a generateCode(node, generator)
function is not provided the node bust be monkey-patched to add a generateCode(generator)
method.
For example:
builder// Output code:out;
objectExpression(properties)
program(body)
Returns a node to generate the code for the root statements of a JavaScript code.
For example:
builder// Output code:var name = "Frank";console;
property(key, value)
require(path)
Returns a node that generates the following code:
Short-hand for the following:
builder
returnStatement(argument)
Returns a node that generates the following code:
return <argument>
For example:
builder// Output code:{return str;}
selfInvokingFunction(params, args, body)
Returns a node that generates the following code:
{<body>}<args>
For example:
builder// Output code:{winfoo = "bar";}window
Or, without params and args:
builder// Output code:{var foo;foo = "bar";}
selfInvokingFunction(body)
Equivalent to selfInvokingFunction(null, null, body)
.
slot(onDone)
Returns a node that defers generating code until everything else is done. This can be helpful in situations where a fragment of code is not known until the rest of the code is generated.
As an example, the TemplateRoot node uses a slot to defer generating the static variables section of the compiled template. Not until all of the nodes have generated code is it known which static variables need to be added at the top of the compiled template.
builder// Output code:var foo = "abc"bar = 123;
strictEquality(left, right)
Returns a node that generates the following code:
<left> === <right>
For example:
builder// Output code:a === b