Fire TS
Introduction
Fire TS is a template engine for generating code.
- Designed to generate C, C++, Java, JavaScript, Ruby, Perl, and HTML
- Tries to generate well formatted code, you can properly indent your templates and fire-ts will remove your extra indention
- Can update existing generated files with out overwriting everything
- Designed to be compatible with the combustion template system
If you want to generate HTML templates you should consider other template engines
The basics
The syntax for Fire TS looks a lot like JSP, except the base language is JavaScript
<% var colors=[ "red","blue","green"]; var numbers=[1,2,3,5];%> #include<stdio.h> /*%{header} The text in this 'header' block can be overridden by what is found in the file that wil be overwritten when this file is generated. Allowing the user to edit this bit of comment or code safely without having to worry about loosing his/her changes. }%*/ //%{prefix} //This code will be preserved, even when the template is re-generated!!! const char *prefix=">"; //}% char *colors[]=; int *numbers[]=; void
And will generate this:
#include<stdio.h> /*%{header} This text was pulled from the file that was gonna be over written. }%*/ //%{prefix} const char *prefix="<";//}% char *colors[]=; int *numbers[]=; void
Writing templates!
Well formated templates!
One of the neat things that Fire TS does is it tries to allow you to both have well formated templates and well formatted output. This means that Fire-TS will count the number of opening and closing braces and then remove the appropriates spacing. By default Fire-TS will look for tabs, although you can set the 'indent' property in the options to change it to spaces, etc.
Here are some examples:
<% for(var i in items) %> //tabDepth--; // Would generate int foo;
<% for(var i in items) %>//Would generate int foo;
//Tell Fire-TS to remove two spaces for each level of indention found Fire;`` ## Evaluating code Anything in: ```jsp<% CODE %>
will be evaluated as JavaScript, and it can even be multi-line
Let's do some looping<% for(var i=0;i<3;i++){ if(i==2){%> i is 2<% } }%>
Which would even track the number of opening and closing braces to properly add and remove tab characters from the output - So your templates and your generated code will look good!
Let's do some looping
i is 2
If you want to actually indent "i is 2" simply add more tabs to it.
Expressions
There are multiple types of expressions
Raw
This will just output the value of the variable or expression with no formatting
<!-- Output the raw variable or expression --> <%=variable%>
JSON Encoded
This will output the JSON encoded variable or expression
<!-- Output a JSON encoded value - which works well for 'c' escaped strings --> <%#variable%>
URL Encoded
This will output a URL escaped string
<!-- Output a URL encoded value --> <%%variable%>
Capturing and modifying the template itself
//Will replace Their with There<%(%>Hello Their <%=name%><%).replace(/Their/,"There")%> //Will convert this to lower case <%(%>MiXeDcAsE<%).toLowerCase()%>
Using '<%(%>' and '<%)%>' allows you to capture and modify result of that part of the template as a String and modify it. Here are a few examples:
Including sub-templates
If you want to nest templates you an do this:
<%@ header.fts %>
All the inputs and options pasted to the top template will be passed to the nested templates. If you need to capture variables from one template to the nested templates:
<%@ header.fts (variableA,variableB) %>
will capture local variableA and variableB and pass them into the nested template. Fire-TS will call opts.render to resolve the template, so you can back it with something that returns a file or a named snippet
Blocks
One of the more advanced features of Fire-TS is that it can preserve the contents of the file it is going to over-write. Let's say for example you want to have a SQL file:
<% var colors=[ "red","blue","green"]; var numbers=[1,2,3,5];%> -- %{schema} ( id bigint auto_increment, color varchar(20) not null, number bigint not null,); -- }% <% colors.map(function(color){ %> <% numbers.map(function(number){ %> insert into colors (name) values(<%#color%>,<%=number%>); <% }); %><% }); %>
Fire-TS will see if the file it is about to over-write exists, and look for blocks ( starting with '%{[A-Za-z0-9]+}' and ending with '}%' ) and read them from the old file, and then insert them into the newly written output. Allowing you to preserve certain parts of older files. In the example above it would let you safely modify the schema and have the inserts regenerated each time!
Embedding
Take a look at bin/fire-ts to get an idea how to use the templating engine, you can install it globally using
npm install fire-ts -g
You can see embedding examples here: https://github.com/celer/fire-ts/tree/master/examples/embed
Command line
./fire-tsOptions: --output File to write --template The template to use --compile The template to compile --input The input to use to run the template --blocks The blocks to use --compare Compare the output file to the specified file --uglify Uglify the compiled template --debug Debug
Let's run a template
./bin/fire-ts --template test/1.fts
Let's compile a template
./bin/fire-ts --compile test/1.fts
Results in
{_$_o=_$_o||{}_$_ob=_$_oblocks||{};var _$_s=""_$_e=_$_oe||{_$_s+=_};with_$_oj||JSONstringify_$_orender_$_i;return _$_s}
License
MIT
API
/** Fire Template System @module FireTS @class Fire*/ /** Read blocks from a given string Blocks are primarly used as a way to keep modifications from various files. So typically FireTS will read the blocks from the file that is about to be overwritten and make sure they aren't modified when the template is rewritten. @param @returns Example of a block @example //% This is a block //}% Reading the above example would return: @example { header:"\n This is a block\n//"} @method Fire.readBlocks */ /** Read blocks from a file asynchronously @param @param @param @param @method Fire.readFileBlocks @see Fire.readBlocks */ /** Read blocks from a file synchronously @param @return @method Fire.readFileBlocksSync @see Fire.readBlocks */ /** Compile a string into a template @param @param @param @param @param @returns {Function or String} String or compiled template @method Fire.compile */ /** Synchronous template function @param @param @param @param @param @param @param @returns @example var result = template({ name:"George" },{ render: function(template,input,opts){ return Fire.compile("<%=xi%>")(input) } }); @method template (synchronous) */ /** Asynchronous template function @param @param @param @param @param @param @param @param @param @param @param @example template({name:"hello"},{ fetch: function(template,onComplete){ Fire.parse(template,{async:true},onComplete); } },function(err,template){ console.log("Result",template); }); @method template (asynchronous) */ /** Simulate the combustion template interface The returned functions has the following parameters @param @param @returns @example var template = Fire.combustion().compile("<%=x%>"); template({x:5}); @method Fire.combustion */ /** Generate a file from a template This function will: * Read the blocks from outFile if it exists * Generate a new output file, reusing the blocks from the prior outFile @param @param @param @param @returns @method Fire.generateSync */ /** Parse a template file synchronously and return a compiled template @param @param @return @method Fire.parseSync */ /** Parse a template file and return a compiled template @param @param @param @param @param @method Fire.parse */