# haxe.Template question

**URL:** https://community.haxe.org/t/haxe-template-question/2972
**Category:** Haxe
**Created:** [April 9, 2021, 5:24pm UTC](https://community.haxe.org/t/haxe-template-question/2972 "2021-04-09T17:24:25Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![filt3rek](https://community.haxe.org/user_avatar/community.haxe.org/filt3rek/32/2613_2.png) [@filt3rek](https://community.haxe.org/u/filt3rek)
#### Post date: [April 9, 2021, 5:24pm UTC](https://community.haxe.org/t/haxe-template-question/2972/1 "2021-04-09T17:24:25Z")

</div>

Hej !

I try to do something like that :

```haxe
class Test {
	static function main() {
    var s = "Hello ::recipient.name:: from company $$instance(::recipient.companies::,1,\"name\") ### ::foreach recipient.companies::::name::::end::";
    var ctx : Dynamic = {
			recipient: {
				name : "Mrs. Annie Cordy",
        companies : [{ name : "Company 1" }, { name : "Company 2" }]
      }
    }
    var tpl	= new haxe.Template( s );
    var out	= tpl.execute( ctx, Test );
    trace( out );
	}
  @:keep
  static function instance( resolve:String->Dynamic, a : Array<Dynamic>, n : Int, field : String ){
		return Reflect.field( a[n], field );
  }
}

```

Here’s the try example : [Try Haxe !](https://try.haxe.org/#E5f4Aa0c)

Does anyone know how can I achieve that please ? I want to have a macro function to get an array index object instead of iterating with `forech`

Thanks all !

---

<div class="post-metadata">

### Author: ![filt3rek](https://community.haxe.org/user_avatar/community.haxe.org/filt3rek/32/2613_2.png) [@filt3rek](https://community.haxe.org/u/filt3rek)
#### Post date: [April 9, 2021, 5:51pm UTC](https://community.haxe.org/t/haxe-template-question/2972/2 "2021-04-09T17:51:36Z")

</div>

I think I can get it with hscript to parse the array but maybe there is another way ?

---

<div class="post-metadata">

### Author: ![filt3rek](https://community.haxe.org/user_avatar/community.haxe.org/filt3rek/32/2613_2.png) [@filt3rek](https://community.haxe.org/u/filt3rek)
#### Post date: [April 9, 2021, 6:21pm UTC](https://community.haxe.org/t/haxe-template-question/2972/3 "2021-04-09T18:21:02Z")

</div>

I succeed with that : [Try Haxe !](https://try.haxe.org/#cAFa3E18) , but if anyone has a better solution I take it 🙂

```haxe
class Test {
	static function main() {
		var s = "Hello ::recipient.name:: from company $$instance(recipient.companies,1,name)";
		var ctx = {
			recipient: {
				name: "Mrs. Annie Cordy",
				companies: [{name: "Company 1"}, {name: "Company 2"}]
			}
		}
		var tpl = new haxe.Template(s);
		var out = tpl.execute(ctx, Test);
		trace(out);
	}

	@:keep
	static function instance(resolve:String->Dynamic, v:String, n:Int, field:String) {
		var a = v.split( "." );
    var o	= resolve( a.shift() );
    do {
			o = Reflect.field( o, a.shift() );
    }while( a.length > 0 );
    var it : Array<Dynamic> = o;
    return Reflect.field( it[n], field );
	}
}

```

Maybe @RealyUniqueName ?

---

<div class="post-metadata">

### Author: ![filt3rek](https://community.haxe.org/user_avatar/community.haxe.org/filt3rek/32/2613_2.png) [@filt3rek](https://community.haxe.org/u/filt3rek)
#### Post date: [April 9, 2021, 6:39pm UTC](https://community.haxe.org/t/haxe-template-question/2972/4 "2021-04-09T18:39:17Z")

</div>

All fields are evaluated as strings, maybe it should be good to have a way to get an object as is forachieve some maipulations ?

---

<div class="post-metadata">

### Author: ![ibilon](https://community.haxe.org/user_avatar/community.haxe.org/ibilon/32/13_2.png) [@ibilon](https://community.haxe.org/u/ibilon)
#### Post date: [April 9, 2021, 6:48pm UTC](https://community.haxe.org/t/haxe-template-question/2972/5 "2021-04-09T18:48:54Z")

</div>

If your templates are available at compile time then [tink\_template](https://github.com/haxetink/tink_template) is a good alternative to `haxe.Template`.  
With it you should be able to do `(: recipient.companies[1].name :)`, or could always call a function from the template anyway.

---

<div class="post-metadata">

### Author: ![filt3rek](https://community.haxe.org/user_avatar/community.haxe.org/filt3rek/32/2613_2.png) [@filt3rek](https://community.haxe.org/u/filt3rek)
#### Post date: [April 9, 2021, 6:54pm UTC](https://community.haxe.org/t/haxe-template-question/2972/6 "2021-04-09T18:54:33Z")

</div>

Thanks Valentin for your reply.  
I yet use tink\_template for my web templates that I found awesome. But here I need it on run-time. I found haxe.Template quite good but it misses some little things like that, that’s pitty.

---

<div class="post-metadata">

### Author: ![filt3rek](https://community.haxe.org/user_avatar/community.haxe.org/filt3rek/32/2613_2.png) [@filt3rek](https://community.haxe.org/u/filt3rek)
#### Post date: [April 9, 2021, 8:24pm UTC](https://community.haxe.org/t/haxe-template-question/2972/7 "2021-04-09T20:24:41Z")

</div>

Take a look at this awesome new run-time Template system based on hscript !!! 🤣 :  
The synthax used is `::+` and `+::`, called “super stars” 🙃

```haxe
var ctx	= {
			recipient: {
				name	: "Mrs. Annie Cordy",
				male : false, 
				companies: [{name: "Company 1"}, {name: "Company 2"}]
			}
		}

		var s	= "Hello ::+ctx.recipient.name+:: from ::+ctx.recipient.companies[0].name+::
		::+( if( !ctx.recipient.male ):: Bonjour Madame ! ::else:: Bonjour Monsieur ! ::)+::
		You work in these companies : ::+ctx.recipient.companies.map( function( c ) return c.name ).join( ', ' )+::";

		var ps	= s.split( "::" ).join( '"' );
		var pss	= 'var str	= "$ps";return str;';

		try{
			var parser	= new hscript.Parser();
			var ast = parser.parseString( pss );
			var interp = new hscript.Interp();
			interp.variables.set( "ctx", ctx ); 
			var ret = interp.execute( ast );
			trace( ret );
		}catch( e ){
			trace( "HScript interpreter : " + e.message );
		}

```

Gives :

```haxe
Hello Mrs. Annie Cordy from Company 1
Bonjour Madame !
You work in these companies : Company 1, Company 2

```

In order to unify a bit the synthax we could do this mapping :parrot::

```haxe
::if+ => ::+(if
+if:: => ::
::+else+::	=> ::else::
::+endif+::	=> ::)::

```

So the source would look like :

```haxe
Hello ::+ctx.recipient.name+:: from ::+ctx.recipient.companies[0].name+::
::+if( !ctx.recipient.male )+if:: Bonjour Madame ! ::+else+:: Bonjour Monsieur ! ::+endif+::
You work in these companies : ::+ctx.recipient.companies.map( function( c ) return c.name ).join( ', ' )+::

```
