diff --git a/README.md b/README.md index f3e66ce..0647108 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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 )` @@ -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. \ No newline at end of file diff --git a/ftk/format/template/Parser.hx b/ftk/format/template/Parser.hx index 0645e83..33e59ce 100644 --- a/ftk/format/template/Parser.hx +++ b/ftk/format/template/Parser.hx @@ -3,7 +3,7 @@ package ftk.format.template; using StringTools; /** - * @version 1.2.3 + * @version 1.2.4 * @author filt3rek */ diff --git a/ftk/format/template/Template.hx b/ftk/format/template/Template.hx index ff19bb8..dc9b00d 100644 --- a/ftk/format/template/Template.hx +++ b/ftk/format/template/Template.hx @@ -21,7 +21,7 @@ using hscript.Tools; #end /** - * @version 1.2.3 + * @version 1.2.4 * @author filt3rek */ @@ -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 @@ -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 ) ); + } } } @@ -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 /*