Skip to content

Commit

Permalink
Examples and init macro addStd() added
Browse files Browse the repository at this point in the history
  • Loading branch information
filt3rek committed Feb 15, 2022
1 parent b41a397 commit 55bd5b8
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 15 deletions.
17 changes: 13 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ public function myFunction( arg1, arg2... ){
ftk.format.template.Template.buildFromString( "::x:: is not bar" ); // foo is not bar
}
```
#### A Helper macro init function to add and keep haxe std classes at compilation (used with the `addStd` run-time *Template* [constructor](#constructor-))
- `addStd()`

Add `--macro ftk.format.Template.addStd()` into the *.hxml* build file.

This function will add and keep all the std classes to be available at run-time (when addStd is set at true in the *Template* constructor)

#### Compilation directives
- `-D hscriptPos` to report error line related to hscript macro exprs generator.
- `-D hscript_template_macro_pos` to report error line related to generated expressions.
Expand Down Expand Up @@ -130,9 +137,12 @@ This function takes this argument :

This function takes these arguments :
- `?runtimePos` : If set to true, it will manage source code if errors occurs, especially when using inclusions. true by dafault
- `?addStd` : If set to true, adds some standard haxe classes (Std, Math, Date, StringTools...)
- `?addStd` : If set to true, adds some standard haxe classes (Std, Math, Date, StringTools, DateTools, Lambda, haxe.ds.StringMap, haxe.ds.IntMap, haxe.ds.ObjectMap). *The package is removed so you'll access `IntMap` and not `haxe.ds.IntMap`*

**Note** : Don't forget to add `-D hscriptPos` if you set `runtimePos` at true to get the line position in error case !
**Notes** :
- Don't forget to add `-D hscriptPos` if you set `runtimePos` at true to get the line position in error case !
- Be sure that the std haxe classes are included (and all the wanted fields). You can add it in compilation with this init macro :
- `--macro ftk.format.template.Template.addStd()`

#### Main function that generates a template
- `execute( hscriptSource : String, ?ctx : {}, isInclusion = false )`
Expand Down Expand Up @@ -368,5 +378,4 @@ This function just returns this string with the basic text and all the evaluated

This little lib is simplier to use than the explanation with my wonderful english in this Readme file to read and understand :rofl:

You can take a look at [tink_template](https://github.com/haxetink/tink_template) Readme file if you haven't understood something here because the approach is very similar.

You can take a look at [tink_template](https://github.com/haxetink/tink_template) Readme file if you haven't understood something here because the approach is very similar.
2 changes: 1 addition & 1 deletion ftk/format/template/Parser.hx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package ftk.format.template;
using StringTools;

/**
* @version 1.2.3
* @version 1.2.4
* @author filt3rek
*/

Expand Down
29 changes: 19 additions & 10 deletions ftk/format/template/Template.hx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ using hscript.Tools;
#end

/**
* @version 1.2.3
* @version 1.2.4
* @author filt3rek
*/

Expand All @@ -47,6 +47,8 @@ class TemplateError {

class Template {

static var stdClasses = [ "Std", "Math", "Date", "StringTools", "DateTools", "Lambda", "haxe.ds.StringMap", "haxe.ds.IntMap", "haxe.ds.ObjectMap" ];

#if macro
static var templateMeta = ":template";
#else
Expand Down Expand Up @@ -98,15 +100,10 @@ class Template {
} );

if( addStd ){
hinterp.variables.set( "Std", Std );
hinterp.variables.set( "Math", Math );
hinterp.variables.set( "Date", Date );
hinterp.variables.set( "StringTools", StringTools );
hinterp.variables.set( "DateTools", DateTools );
hinterp.variables.set( "Lambda", Lambda );
hinterp.variables.set( "StringMap", haxe.ds.StringMap );
hinterp.variables.set( "IntMap", haxe.ds.IntMap );
hinterp.variables.set( "ObjectMap", haxe.ds.ObjectMap );
for( cname in stdClasses ){
var path = cname.split( "." );
hinterp.variables.set( path[ path.length - 1 ], Type.resolveClass( cname ) );
}
}
}

Expand Down Expand Up @@ -252,6 +249,18 @@ class Template {
#end
}

/*
* This function will add and keep all the std classes to be available at run-time (when addStd is set at true in the Template constructor)
*/

public static function addStd(){
#if macro
for( cname in stdClasses ){
haxe.macro.Compiler.addMetadata( "@:keep", cname );
}
#end
}

// Build macro

/*
Expand Down

0 comments on commit 55bd5b8

Please sign in to comment.