Marko Compiler // MarkoImprove this page
The Marko compiler produces a Node.js-compatible, CommonJS module as output. This output format has the advantage that compiled template modules can benefit from a context-aware module loader and templates can easily be transported to work in the browser using Lasso.js or Browserify.
The marko
module will automatically compile templates loaded by your application on the server, but you can also choose to precompile all templates. This can be helpful as a build or test step to catch errors early.
You can either use the command line interface or the JavaScript API to compile a Marko template file. To use the CLI you must first install the marko
module globally using the following command:
npm install marko --global
You can then compile single templates using the following command:
markoc hello.marko
This will produce a file named hello.marko.js
next to the original file.
You can also recursively compile all templates in the current directory (the node_modules
and .*
directories will be ignored by default)
markoc .
You can also specify multiple directories or files
markoc foo/ bar/ template.marko
To delete all of the generated *.marko.js
files you can add the --clean
argument. For example:
markoc . --clean
Alternatively, you can use the JavaScript API to compile a module as shown in the following sample code:
;
Sample Compiled Template
{var str = __helperssescapeXml = __helpersxforEach = __helpersfescapeXmlAttr = __helpersxa;return {out;if datacolorslengthout;;out;elseout;};}moduleexports = ;
The compiled output is designed to be both extremely readable and minifiable. The minified code is shown below:
{var d=anec=axe=aff=axa;return {b;?bb:b}}moduleexports=;
File size: 223 bytes gzipped (300 bytes uncompressed)
Compiler API
See: Marko Compiler - API
Compile-time Tags
See: Marko Compiler - Compile-time Tags