I’ve written this little class that “generates” a “haxe source” which you can use with hscript to get a run-time template system. It can be improved of course but I stop here for now I get all for my needs.
The synthax is almost the same as in tink_template and it supports expressions output, if, else, for statements and “do”. I didn’t wrote any error handling because hscript does that.
This class could directly generate hscript AST expressions, but I’ve done that quickly, I have no time to look deeper into hscript for now.
You can test it here : Try Haxe !
Here is an example of a working template :
Hello "::ctx.recipient.name::", your main company is : ::ctx.recipient.companies[ 0 ].name::
::if( !ctx.recipient.male )::Bonjour Madame !::else::Bonjour Monsieur !::end::
You work in these companies : ::ctx.recipient.companies.map( function( c ) return c.name ).join( ', ' )::
Here are your companies :
::do var rand = Math.rand()::
::for( company in ctx.recipient.companies )::
::if( rand < .5 )::
::company.name::
::else::
None
::end::
::end::
The output source that is “generated” and that you give to eat for hscript is that :
var s = "";
s += "Hello \"";
s += ctx.recipient.name;
s += "\", your main company is ";
s += ctx.recipient.companies[ 0 ].name;
s += "
";
if( !ctx.recipient.male ){
s += "Bonjour Madame !";
}else{
s += "Bonjour Monsieur !";
}
s += "
You work in these companies ";
s += ctx.recipient.companies.map( function( c ) return c.name ).join( ', ' );
s += "
";
var rand = Math.random();
s += "
Here are your companies ";
for( company in ctx.recipient.companies ){
s += "
";
if( rand < .5 ){
s += "
";
s += company.name;
s += "
";
}else{
s += "
None
";
}
s += "
";
}
return s;
A bit improved version where you can customize the sign used to delimitate expressions, nd the keywords as if, else, for, end and do.
You can also get a pretty output if you want debug maybe… Try Haxe !
So now you can wrtie templates like that (like in the awful WINDEV ) :
Hello "**ctx.recipient.name**", your main company is : **ctx.recipient.companies[ 0 ].name**
**si( !ctx.recipient.male )**Bonjour Madame !**sinon**Bonjour Monsieur !**fin**
You work in these companies : **ctx.recipient.companies.map( function( c ) return c.name ).join( ', ' )**
Here are your companies :
**pose var rand = Math.random()**
**boucle( company in ctx.recipient.companies )**
**si( rand < .5 )**
**company.name**
**sinon**
None
**fin**
**fin**
And get that to give to eat for hscript :
var s = "";
s += "Hello \"";
s += ctx.recipient.name;
s += "\", your main company is : ";
s += ctx.recipient.companies[ 0 ].name;
s += "
";
if( !ctx.recipient.male ){
s += "Bonjour Madame !";
}else{
s += "Bonjour Monsieur !";
}
s += "
You work in these companies : ";
s += ctx.recipient.companies.map( function( c ) return c.name ).join( ', ' );
s += "
Here are your companies :
";
var rand = Math.random();
s += "
";
for( company in ctx.recipient.companies ){
s += "
";
if( rand < .5 ){
s += "
";
s += company.name;
s += "
";
}else{
s += "
None
";
}
s += "
";
}
return s;
I’ve done that possibility because I work for a french company and since it’s for some document templates that they want to be able to manage, write and edit online, maybe it will be more “user-friendly” for them…
Just to say that today I’ve removed tink_template from my project because I’ve added support for compile-time templates so now it works the same way both on run-time and compile-time.
I’ve also added switch/case statement and comments.
I’m surprised because hscript + GitHub - filt3rek/hscript-template: Little run-time and compile-time template system based on hscript run faster that tink_template alone…