2017-04-14 16:43:31 +02:00
|
|
|
// ***************************************************************************
|
|
|
|
//
|
|
|
|
// Delphi MVC Framework
|
|
|
|
//
|
2023-01-17 08:52:26 +01:00
|
|
|
// Copyright (c) 2010-2023 Daniele Teti and the DMVCFramework Team
|
2017-04-14 16:43:31 +02:00
|
|
|
//
|
|
|
|
// https://github.com/danieleteti/delphimvcframework
|
|
|
|
//
|
|
|
|
// ***************************************************************************
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
//
|
|
|
|
// ***************************************************************************
|
|
|
|
|
|
|
|
unit MVCFramework.View.Renderers.Mustache;
|
|
|
|
|
2020-05-25 19:34:14 +02:00
|
|
|
{$IFDEF LINUX}
|
|
|
|
This unit is not compatible with Linux
|
|
|
|
{$ENDIF}
|
2023-09-22 09:43:35 +02:00
|
|
|
interface
|
2020-05-25 19:34:14 +02:00
|
|
|
|
2023-09-22 09:43:35 +02:00
|
|
|
uses
|
|
|
|
MVCFramework, System.SysUtils, System.Generics.Collections,
|
|
|
|
MVCFramework.Commons, System.IOUtils, System.Classes, Data.DB, SynMustache;
|
2017-04-14 16:43:31 +02:00
|
|
|
|
|
|
|
type
|
|
|
|
{ This class implements the mustache view engine for server side views }
|
|
|
|
TMVCMustacheViewEngine = class(TMVCBaseViewEngine)
|
2017-05-25 16:57:49 +02:00
|
|
|
strict private
|
|
|
|
procedure PrepareModels;
|
|
|
|
private
|
2023-09-22 09:43:35 +02:00
|
|
|
class var fPartials: TSynMustachePartials;
|
|
|
|
var FJSONModel: string;
|
|
|
|
procedure LoadPartials;
|
2017-04-14 16:43:31 +02:00
|
|
|
public
|
2023-09-22 09:43:35 +02:00
|
|
|
procedure Execute(const ViewName: string; const OutputStream: TStream); override;
|
|
|
|
constructor Create(const AEngine: TMVCEngine; const AWebContext: TWebContext;
|
|
|
|
const AViewModel: TMVCViewDataObject;
|
|
|
|
const AViewDataSets: TObjectDictionary<string, TDataSet>;
|
|
|
|
const AContentType: string); override;
|
|
|
|
class destructor Destroy;
|
2017-04-14 16:43:31 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
|
|
|
uses
|
2017-05-25 16:57:49 +02:00
|
|
|
SynCommons,
|
2021-12-30 00:36:35 +01:00
|
|
|
JsonDataObjects,
|
2017-05-25 16:57:49 +02:00
|
|
|
MVCFramework.Serializer.Defaults,
|
|
|
|
MVCFramework.Serializer.Intf,
|
2021-03-03 23:16:55 +01:00
|
|
|
MVCFramework.DuckTyping,
|
|
|
|
MVCFramework.Serializer.JsonDataObjects.OptionalCustomTypes,
|
|
|
|
MVCFramework.Serializer.JsonDataObjects;
|
2017-04-14 16:43:31 +02:00
|
|
|
|
|
|
|
{$WARNINGS OFF}
|
|
|
|
|
2021-03-03 23:16:55 +01:00
|
|
|
|
2020-11-05 17:14:39 +01:00
|
|
|
type
|
|
|
|
TSynMustacheAccess = class(TSynMustache)
|
|
|
|
end;
|
2020-04-13 16:22:15 +02:00
|
|
|
|
2023-09-22 09:43:35 +02:00
|
|
|
var
|
|
|
|
gPartialsLoaded : Boolean = False;
|
|
|
|
|
|
|
|
constructor TMVCMustacheViewEngine.Create(const AEngine: TMVCEngine;
|
|
|
|
const AWebContext: TWebContext; const AViewModel: TMVCViewDataObject;
|
|
|
|
const AViewDataSets: TObjectDictionary<string, TDataSet>;
|
|
|
|
const AContentType: string);
|
|
|
|
begin
|
|
|
|
inherited;
|
|
|
|
LoadPartials;
|
|
|
|
end;
|
|
|
|
|
|
|
|
class destructor TMVCMustacheViewEngine.Destroy;
|
|
|
|
begin
|
|
|
|
fPartials.Free;
|
|
|
|
end;
|
|
|
|
|
2018-01-29 17:30:53 +01:00
|
|
|
procedure TMVCMustacheViewEngine.Execute(const ViewName: string; const OutputStream: TStream);
|
2017-04-14 16:43:31 +02:00
|
|
|
var
|
2018-02-20 22:36:18 +01:00
|
|
|
lViewFileName: string;
|
|
|
|
lViewTemplate: RawUTF8;
|
|
|
|
lViewEngine: TSynMustache;
|
2018-01-29 17:30:53 +01:00
|
|
|
lSW: TStreamWriter;
|
2017-04-14 16:43:31 +02:00
|
|
|
begin
|
2017-05-25 16:57:49 +02:00
|
|
|
PrepareModels;
|
2018-02-20 22:36:18 +01:00
|
|
|
lViewFileName := GetRealFileName(ViewName);
|
|
|
|
if not FileExists(lViewFileName) then
|
2017-04-14 16:43:31 +02:00
|
|
|
raise EMVCFrameworkViewException.CreateFmt('View [%s] not found', [ViewName]);
|
2018-02-20 22:36:18 +01:00
|
|
|
lViewTemplate := StringToUTF8(TFile.ReadAllText(lViewFileName, TEncoding.UTF8));
|
|
|
|
lViewEngine := TSynMustache.Parse(lViewTemplate);
|
2018-01-29 17:30:53 +01:00
|
|
|
lSW := TStreamWriter.Create(OutputStream);
|
|
|
|
try
|
2023-09-22 09:43:35 +02:00
|
|
|
lSW.Write(UTF8Tostring(lViewEngine.RenderJSON(FJSONModel, fPartials, nil, nil)));
|
|
|
|
finally
|
|
|
|
lSW.Free;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TMVCMustacheViewEngine.LoadPartials;
|
|
|
|
var
|
|
|
|
lViewsExtension: string;
|
|
|
|
lViewPath: string;
|
|
|
|
lPartialFileNames: TArray<string>;
|
|
|
|
I: Integer;
|
|
|
|
begin
|
|
|
|
if gPartialsLoaded then
|
|
|
|
begin
|
|
|
|
Exit
|
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
TMonitor.Enter(gLock);
|
|
|
|
try
|
|
|
|
if not gPartialsLoaded then
|
2020-11-05 17:14:39 +01:00
|
|
|
begin
|
2023-09-22 09:43:35 +02:00
|
|
|
lViewsExtension := Config[TMVCConfigKey.DefaultViewFileExtension];
|
|
|
|
lViewPath := Config[TMVCConfigKey.ViewPath];
|
|
|
|
lPartialFileNames := TDirectory.GetFiles(lViewPath, '*.' + lViewsExtension);
|
|
|
|
fPartials := TSynMustachePartials.Create;
|
|
|
|
for I := 0 to High(lPartialFileNames) do
|
|
|
|
begin
|
|
|
|
fPartials.Add(TPath.GetFileNameWithoutExtension(lPartialFileNames[i]), TFile.ReadAllText(lPartialFileNames[i]));
|
|
|
|
end;
|
|
|
|
gPartialsLoaded := True;
|
2020-11-05 17:14:39 +01:00
|
|
|
end;
|
2023-09-22 09:43:35 +02:00
|
|
|
finally
|
|
|
|
TMonitor.Exit(gLock);
|
2020-11-05 17:14:39 +01:00
|
|
|
end;
|
2018-01-29 17:30:53 +01:00
|
|
|
end;
|
2017-05-25 16:57:49 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
{$WARNINGS ON}
|
|
|
|
|
2020-04-13 16:22:15 +02:00
|
|
|
|
2017-05-25 16:57:49 +02:00
|
|
|
procedure TMVCMustacheViewEngine.PrepareModels;
|
|
|
|
var
|
|
|
|
lFirst: Boolean;
|
|
|
|
lList: IMVCList;
|
|
|
|
DataObj: TPair<string, TObject>;
|
2020-04-13 16:22:15 +02:00
|
|
|
lDSPair: TPair<string, TDataSet>;
|
2017-05-25 16:57:49 +02:00
|
|
|
lSJSON: string;
|
|
|
|
lJSON: string;
|
|
|
|
lSer: IMVCSerializer;
|
|
|
|
begin
|
2021-03-03 23:16:55 +01:00
|
|
|
{TODO -oDanieleT -cGeneral : Quite inefficient to generate JSON in this way. Why don't use a JSONObject directly?}
|
2017-05-25 16:57:49 +02:00
|
|
|
if (FJSONModel <> '{}') and (not FJSONModel.IsEmpty) then
|
|
|
|
Exit;
|
|
|
|
FJSONModel := '{}';
|
2020-04-13 16:22:15 +02:00
|
|
|
|
2021-03-03 23:16:55 +01:00
|
|
|
lSer := TMVCJsonDataObjectsSerializer.Create;
|
|
|
|
RegisterOptionalCustomTypesSerializers(lSer);
|
2020-04-13 16:22:15 +02:00
|
|
|
lSJSON := '{';
|
|
|
|
lFirst := True;
|
|
|
|
|
2017-04-14 16:43:31 +02:00
|
|
|
if Assigned(ViewModel) then
|
|
|
|
begin
|
|
|
|
for DataObj in ViewModel do
|
|
|
|
begin
|
2017-05-25 16:57:49 +02:00
|
|
|
lList := TDuckTypedList.Wrap(DataObj.Value);
|
|
|
|
if lList <> nil then
|
|
|
|
lJSON := lSer.SerializeCollection(DataObj.Value)
|
|
|
|
else
|
|
|
|
lJSON := lSer.SerializeObject(DataObj.Value);
|
2017-04-14 16:43:31 +02:00
|
|
|
if not lFirst then
|
|
|
|
lSJSON := lSJSON + ',';
|
2017-05-25 16:57:49 +02:00
|
|
|
lSJSON := lSJSON + '"' + DataObj.Key + '":' + lJSON;
|
2017-04-14 16:43:31 +02:00
|
|
|
lFirst := False;
|
|
|
|
end;
|
|
|
|
end;
|
2020-04-13 16:22:15 +02:00
|
|
|
|
|
|
|
if Assigned(ViewDataSets) then
|
|
|
|
begin
|
|
|
|
for lDSPair in ViewDataSets do
|
|
|
|
begin
|
|
|
|
lJSON := lSer.SerializeDataSet(lDSPair.Value);
|
|
|
|
if not lFirst then
|
|
|
|
lSJSON := lSJSON + ',';
|
|
|
|
lSJSON := lSJSON + '"' + lDSPair.Key + '":' + lJSON;
|
|
|
|
lFirst := False;
|
|
|
|
end;
|
|
|
|
end;
|
2021-12-30 00:36:35 +01:00
|
|
|
|
2020-04-13 16:22:15 +02:00
|
|
|
lSJSON := lSJSON + '}';
|
|
|
|
FJSONModel := lSJSON;
|
2017-04-14 16:43:31 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
end.
|