Little run-time AND COMPILE-TIME template system based on hscript

EDIT :If someone is interested, changes can be found here : GitHub - filt3rek/hscript-template: Little run-time template system based on hscript

Hej !

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;
2 Likes

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 :rofl: ) :

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;
1 Like

Please don’t do that x)

Hej Rudy !

So you doin’t like WINDEV ??? :rofl:

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…

Personally I prefer something like that :

Template.DO		= "obey_sheep";
Template.IF		= "to_be_or_not_to_be";
Template.ELSE	= "so_f**k_off";
Template.FOR	= "cheese_cake";
Template.END	= "O000OO0O11lll01l00l11l0";

Or even

Template.DO		= "▶️";
Template.IF		= "❓";
Template.ELSE	= "❗";
// Template.ELSEIF	= "⁉️";
Template.FOR	= "🔁";
Template.END	= "🔚";

Oh that makes me think that I haven’t done “elseif”… I’ll add that later even if we can nest an if inside the else…

Here with elseif added : Try Haxe !
The Template class is of course over there

I’ll stop annoying people with this post, so if someone is interested in this, changes can be found here : GitHub - filt3rek/hscript-template: Little run-time template system based on hscript

Cheers

hej,

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…

New version added with some improvements like run-time template in template inclusions and more.

Also available on https://lib.haxe.org/p/hscript_template/