Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to the wizard to configure mustache engine #693

Closed
danieleteti opened this issue Sep 17, 2023 · 6 comments
Closed

Add option to the wizard to configure mustache engine #693

danieleteti opened this issue Sep 17, 2023 · 6 comments

Comments

@danieleteti
Copy link
Owner

Just add an option in the "New DMVCFramework Project" to add something like the following:

  FMVC.SetViewEngine(TMVCMustacheViewEngine);

MVCFramework.View.Renderers.Mustache.pas is required in the uses clause.

@fastbike
Copy link
Contributor

fastbike commented Oct 5, 2023

Is this to be able to handle some of the helper functionality that is defined by the underlying library ?
I notice that by default nil is passed through to the Mustache rendering engine for the helper collection

procedure TMVCMustacheViewEngine.Execute(const ViewName: string; const OutputStream: TStream);
// snip
    lSW.Write(UTF8Tostring(lViewEngine.RenderJSON(FJSONModel, lPartials, nil, nil)));
// snip

A simple change to

    lSW.Write(UTF8Tostring(lViewEngine.RenderJSON(FJSONModel, lPartials, TSynMustache.HelpersGetStandardList, nil)));

allows the use of inbuilt formatting functions etc so the following template will output a formatted date rather than an ISODateTime string

{{#Conditions}}
  <tr class="c-table__row">
    <td>{{OnsetDateTime}}</td>
    <td>{{DateFmt OnsetDateTime, "dd mmm yyyy"}}</td>
  </tr>
{{/Conditions}}

image

@fastbike
Copy link
Contributor

fastbike commented Oct 5, 2023

On a related note there is the ability to pass a translate callback to the TSynMustache renderJSON method. This too is not exposed.
Also on a more general note the TSynMustache class has the ability to register other helpers (aka Lamdba in the mustache world).

So the ability to configure the Mustache Engine before rendering templates would be very useful. I can have a go at some changes, as I have a new project where the ability to format ISO DateTimes via the template rather than via the json data would be extremely useful.

@fastbike
Copy link
Contributor

fastbike commented Oct 5, 2023

From an initial examination it looks like some type of dependency injection is required for the TMVCBaseViewEngine.
Exactly where is not at all clear - my normal patterns are:

  1. At the constructor level but this assumes you know what type of configuration helper to add for any given rendering engine at the moment it is constructed, which is unlikely given the class type is registered, not an instance.
  2. As an extra parameter on the method that does the action (Execute in this case). At render time we have an instance of the ViewEngine so would need to pass a handle out to the MVCEngine (some type of callback maybe) so this could be set in the web module immediately after SetViewEngine.
  3. As an additional property on the rendering engine class that can be assigned to run a callback at the appropriate time. This would need to be checked inside of the Execute method after the instance of the rendering view engine was created.

I'm probably going to subclass the TMVCMustacheViewEngine to see if I can work something into here.

@fastbike
Copy link
Contributor

fastbike commented Oct 5, 2023

My initial work is to add the following to the existing TMVCMustacheViewEngine class

  TMVCMustacheViewEngine = class(TMVCBaseViewEngine)
  protected
    function RenderJSON(lViewEngine: TSynMustache; const JSON: RawUTF8; Partials: TSynMustachePartials;
      Helpers: TSynMustacheHelpers; OnTranslate: TOnStringTranslate; EscapeInvert: boolean): RawUTF8; virtual;

and then change the Execute method to prepopulate the Helpers and make the call to the new method

procedure TMVCMustacheViewEngine.Execute(const ViewName: string; const OutputStream: TStream);
var
  // snip
  lPartials: TSynMustachePartials;
  lHelpers: TSynMustacheHelpers;
begin
  // snip
    lHelpers := TSynMustacheAccess.HelpersGetStandardList;
    lSW.Write(UTF8Tostring(RenderJSON(lViewEngine, FJSONModel, lPartials, lHelpers, nil, false)));
  finally
//snip
  end;
end;

// and a base implementation of the new virtual method
function TMVCMustacheViewEngine.RenderJSON(lViewEngine: TSynMustache; const JSON: RawUTF8; Partials: TSynMustachePartials; 
  Helpers: TSynMustacheHelpers; OnTranslate: TOnStringTranslate; EscapeInvert: boolean): RawUTF8;
begin
  Result := lViewEngine.RenderJSON(JSON, Partials, Helpers, OnTranslate, EscapeInvert);
end;

@fastbike
Copy link
Contributor

fastbike commented Oct 5, 2023

And I can create a new subclass if I wanted to add additional features to the Mustache syntax e.g.

type
  TMVCMustacheViewEnginePlus = class(TMVCMustacheViewEngine)
    procedure Translate(var English: string);
    procedure Test(const Value: variant; out result: variant);
  protected
    function RenderJSON(lViewEngine: TSynMustache; const JSON: RawUTF8; Partials: TSynMustachePartials;
      Helpers: TSynMustacheHelpers; OnTranslate: TOnStringTranslate; EscapeInvert: boolean): RawUTF8; override;
  end;

function TMVCMustacheViewEnginePlus.RenderJSON(lViewEngine: TSynMustache; const JSON: RawUTF8; Partials: TSynMustachePartials;
  Helpers: TSynMustacheHelpers; OnTranslate: TOnStringTranslate; EscapeInvert: boolean): RawUTF8;
begin
  TSynMustacheAccess.HelperAdd(Helpers, 'Test', Test);
  OnTranslate := Translate;
  result := inherited;
end;

// make the text Italien i.e. {{"class}} is ouput as clazz LOL
procedure TMVCMustacheViewEnginePlus.Translate(var English: string);
begin
  English := English.Replace('s', 'z', [rfReplaceAll, rfIgnoreCase]);
end;

@fastbike
Copy link
Contributor

fastbike commented Oct 5, 2023

Just added some proposed code changes. I see you've been busy on the Mustache Rendering logic too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants