sempare-delphi-template-engine icon indicating copy to clipboard operation
sempare-delphi-template-engine copied to clipboard

error when calling AddFunctions

Open edwinyzh opened this issue 4 years ago • 4 comments

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;

edwinyzh avatar Oct 03 '21 17:10 edwinyzh

var lctx:=template.context;
lctx.addfunctions(TTmplFunctions);
 
ltpl := template.parse(lctx, '....');

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

does the above make sense?

darnocian avatar Oct 03 '21 18:10 darnocian

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.

darnocian avatar Oct 03 '21 20:10 darnocian

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

edwinyzh avatar Oct 04 '21 03:10 edwinyzh

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 avatar Sep 20 '22 17:09 sempare

Support will be released shortly

sempare avatar Jan 01 '23 12:01 sempare