sempare / sempare-delphi-template-engine

Sempare Template (scripting) Engine for Delphi allows for flexible dynamic text generation. It can be used for generating email, html, reports, source code, xml, configuration, etc.
Apache License 2.0
144 stars 18 forks source link

error when calling AddFunctions #58

Closed edwinyzh closed 1 year ago

edwinyzh commented 3 years ago

I'm trying to use custom utility functions, but stuck at the AddFunctions stage and have no clue...

The details of the error: main thread ($bbc): 005392a0 +000 prog1.exe System.Rtti 4643 +0 TRttiType.GetHandle 0053a194 +004 prog1.exe System.Rtti 4841 +1 TRttiType.GetTypeKind 014b0467 +067 prog1.exe Sempare.Template.Functions 105 +1 TTemplateFunctions.Add 014b0682 +062 prog1.exe Sempare.Template.Functions 131 +3 TTemplateFunctions.AddFunctions 01fa3fca +11a prog1.exe SempareTemplateSiteCompilerImplU 826 +16 TSempareTemplateSiteCompilerImpl.AfterConstruction

The class in question:

  // Custom utility functions for the template script
  TTmplFunctions = class
  public
    class function InsertImage(aSiteRootRelativeImgPath, aAlt, aWidth, aHeight: string): string; static;
    class procedure SetThemeData(const aThemeData: TThemeData); static;
  end;
darnocian commented 3 years ago
var lctx:=template.context;
lctx.addfunctions(TTmplFunctions);

ltpl := template.parse(lctx, '....');

writeln(Template.Eval(lCtx, lTpl, ldata));

does the above make sense?

darnocian commented 3 years ago

Ah, sorry, I didn't appreciate the issue you highlighted...

All helpers are expected to be functions... so they should return something.

maybe change SetThemeData to something like: class function SetThemeData(const aThemeData: TThemeData) : string; static;

and return empty string for now.

I think I can fix this to do so automatically.

edwinyzh commented 3 years ago

Making SetThemeData to return string helped. Maybe in the future to remove this limit ;) Thanks!

sempare commented 2 years ago

The issue in supporting procedures in the template engine is that it starts to violate the core principle. Data should be presented to the template engine rather than the template engine mutating and updating data. Functions should generally be used for formatting purposes.

Supporting procedures can be done however.

sempare commented 1 year ago

Support will be released shortly