Daniele Teti 2023-11-06 11:13:57 +01:00
parent 9296a57711
commit 017442f3be
3 changed files with 52 additions and 7 deletions

View File

@ -39,14 +39,21 @@ type
implementation
uses
MVCFramework.View.Renderers.Mustache, System.SysUtils;
MVCFramework.View.Renderers.Mustache, System.SysUtils, MVCFramework.Commons;
{ TMyMustacheHelpers }
class procedure TMyMustacheHelpers.MyHelper1(const Value: variant;
out Result: variant);
var
lParams: TMVCStringDictionary;
begin
Result := Value + ' (I''m The MyHelper1)';
lParams := TMVCMustacheHelpers.GetParams(Value);
try
Result := Value + ' (I''m The MyHelper1)';
finally
lParams.Free;
end;
end;
class procedure TMyMustacheHelpers.MyHelper2(const Value: variant; out Result: variant);

View File

@ -129,16 +129,19 @@ This page is a showcase for all the mustache features usable from DMVCFramework
<p>
{{=<% %>=}} {{SnakeCase "daniele teti"}} <%={{ }}=%> outputs: {{SnakeCase "daniele teti"}}
</p>
<p>
{{=<% %>=}} {{i18n "daniele teti"}} <%={{ }}=%> outputs: {{i18n "ita","daniele teti"}}
</p>
</div>
<div>
<h2 class="section">Using Project Specific Helpers</h2>
<h3>Any project can define its own custom helpers</h3>
<p>
{{MyHelper1 "this is a text"}}
{{MyHelper1 content,"browserhasnoemoji",nohtmlescape}}
</p>
<p>
{{MyHelper2 "this is another text"}}
{{MyHelper2 "this is another text"}}
</p>
</div>

View File

@ -31,8 +31,8 @@ interface
uses
MVCFramework, System.SysUtils, System.Generics.Collections,
MVCFramework.Commons, System.IOUtils, System.RTTI,
System.Classes, Data.DB, SynMustache, SynCommons;
MVCFramework.Commons, MVCFramework.Serializer.Commons, System.IOUtils,
System.RTTI, System.Classes, Data.DB, SynMustache, SynCommons;
type
{ This class implements the mustache view engine for server side views }
@ -48,6 +48,7 @@ type
protected
function RenderJSON(lViewEngine: TSynMustache; const JSON: RawUTF8; Partials: TSynMustachePartials;
Helpers: TSynMustacheHelpers; OnTranslate: TOnStringTranslate; EscapeInvert: boolean): RawUTF8; virtual;
procedure Translate(var Text: string);
public
procedure Execute(const ViewName: string; const OutputStream: TStream); override;
constructor Create(const AEngine: TMVCEngine; const AWebContext: TWebContext;
@ -68,17 +69,19 @@ type
class procedure ToUpperCase(const Value: variant; out Result: variant);
class procedure Capitalize(const Value: variant; out Result: variant);
class procedure SnakeCase(const Value: variant; out Result: variant);
class procedure i18n(const Value: variant; out Result: variant);
public
class property OnLoadCustomHelpers: TLoadCustomHelpersProc read fOnLoadCustomHelpers write fOnLoadCustomHelpers;
class function GetParams(const Value: Variant): TMVCStringDictionary;
end;
implementation
uses
JsonDataObjects,
MVCFramework.Serializer.Defaults,
MVCFramework.Serializer.Intf,
MVCFramework.Serializer.Commons,
MVCFramework.DuckTyping,
MVCFramework.Serializer.JsonDataObjects.OptionalCustomTypes,
MVCFramework.Serializer.JsonDataObjects;
@ -115,9 +118,15 @@ end;
function TMVCMustacheViewEngine.RenderJSON(lViewEngine: TSynMustache; const JSON: RawUTF8; Partials: TSynMustachePartials;
Helpers: TSynMustacheHelpers; OnTranslate: TOnStringTranslate; EscapeInvert: boolean): RawUTF8;
begin
OnTranslate := Translate;
Result := lViewEngine.RenderJSON(JSON, Partials, Helpers, OnTranslate, EscapeInvert);
end;
procedure TMVCMustacheViewEngine.Translate(var Text: string);
begin
Text := '*' + Text + '*';
end;
procedure TMVCMustacheViewEngine.Execute(const ViewName: string; const OutputStream: TStream);
var
lViewFileName: string;
@ -270,12 +279,38 @@ begin
Result := MVCFramework.Commons.CamelCase(Value, True);
end;
class function TMVCMustacheHelpers.GetParams(const Value: Variant): TMVCStringDictionary;
var
d: PDocVariantData;
I: Integer;
begin
d := _Safe(Value); // {{{SimpleToHtml content,browserhasnoemoji,nohtmlescape}}}
if (dvoIsArray in d^.Options) then
begin
var s: String := d^.Value[0];
var lPieces := s.Split([sLineBreak]);
Result := TMVCStringDictionary.Create;
for I := 0 to Length(lPieces) - 1 do
begin
var lP := lPieces[I].Split(['=']);
Result.Add(lP[0], lP[1]);
end;
end;
end;
class procedure TMVCMustacheHelpers.i18n(const Value: variant;
out Result: variant);
begin
Result := '*' + value + '*';
end;
class procedure TMVCMustacheHelpers.RegisterHandlers(var MustacheHelpers: TSynMustacheHelpers);
begin
TSynMustache.HelperAdd(MustacheHelpers, 'UpperCase', TMVCMustacheHelpers.ToUpperCase);
TSynMustache.HelperAdd(MustacheHelpers, 'LowerCase', TMVCMustacheHelpers.ToLowerCase);
TSynMustache.HelperAdd(MustacheHelpers, 'Capitalize', TMVCMustacheHelpers.Capitalize);
TSynMustache.HelperAdd(MustacheHelpers, 'SnakeCase', TMVCMustacheHelpers.SnakeCase);
TSynMustache.HelperAdd(MustacheHelpers, 'i18n', TMVCMustacheHelpers.SnakeCase);
if Assigned(fOnLoadCustomHelpers) then
begin
fOnLoadCustomHelpers(MustacheHelpers);