HomeDocumentation

Compiler

The compiler takes an X string and compiles it into a vanilla javascript string that may be evaluated or stored in a javascript file. The following aspects are important when dealing with the compiler:

Here is an example that covers all of these aspects:

var xforjs_string_to_compile =
   "{namespace testing}"+
   "{template test123}hello{/template}"+
   "{template test456}there{/template}";
//Optionally override default options
var compiler_options = {
   "global":false
};
var compiler = XforJS.getCompiler(compiler_options);
var vanilla_js = compiler.compile(xforjs_string_to_compile);
var templates = eval(vanilla_js);
//outputs "hello"
templates.testing.test123();

Configuration

There are several configuration options that control what is output by the compiler.

Here are the accepted options and their default values:

{
   "escapexss": true,
   "global": true,
   "minifyhtml": true,
   "normalizespace": true,
   "removelogs": true,
   "useexternal": false
}

Here is a detailed view of the options:

escapexss

When true, any value from a context selector will be safely escaped for HTML.

global

When true, the resulting string will assign all templates to the global namespace when evaluated. A value of false returns an object that contains the templates to avoid touching the global namespace.

minifyhtml

When true, all space between angle brackets is removed.

normalizespace

When true, all space is converted to a single space.

removelogs

When true, all log statements are removed.

useexternal

When true, the resulting javascript will reference an external library. This has the benefit of lowering the overall size of a compiled template file, but introduces the added dependency into your environment.