Code Monkey home page Code Monkey logo

Comments (6)

fastbike avatar fastbike commented on May 30, 2024

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

from delphimvcframework.

fastbike avatar fastbike commented on May 30, 2024

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.

from delphimvcframework.

fastbike avatar fastbike commented on May 30, 2024

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.

from delphimvcframework.

fastbike avatar fastbike commented on May 30, 2024

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;

from delphimvcframework.

fastbike avatar fastbike commented on May 30, 2024

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;

from delphimvcframework.

fastbike avatar fastbike commented on May 30, 2024

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

from delphimvcframework.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.