delphimvcframework/sources/MVCFramework.View.Renderers.Mustache.pas

114 lines
3.1 KiB
ObjectPascal
Raw Normal View History

// ***************************************************************************
//
// Delphi MVC Framework
//
2019-01-08 12:48:27 +01:00
// Copyright (c) 2010-2019 Daniele Teti and the DMVCFramework Team
//
// 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;
interface
uses
MVCFramework, System.Generics.Collections, System.SysUtils,
2018-01-29 17:30:53 +01:00
MVCFramework.Commons, System.IOUtils, System.Classes;
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
FJSONModel: string;
public
2018-01-29 17:30:53 +01:00
procedure Execute(const ViewName: string; const OutputStream: TStream);
override;
end;
implementation
uses
SynMustache,
2017-05-25 16:57:49 +02:00
SynCommons,
MVCFramework.Serializer.Defaults,
MVCFramework.Serializer.Intf,
MVCFramework.DuckTyping;
{$WARNINGS OFF}
2018-01-29 17:30:53 +01:00
procedure TMVCMustacheViewEngine.Execute(const ViewName: string; const OutputStream: TStream);
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;
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
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
2018-02-20 22:36:18 +01:00
lSW.Write(UTF8Tostring(lViewEngine.RenderJSON(FJSONModel)));
2018-01-29 17:30:53 +01:00
finally
lSW.Free;
end;
2017-05-25 16:57:49 +02:00
end;
{$WARNINGS ON}
procedure TMVCMustacheViewEngine.PrepareModels;
var
lFirst: Boolean;
lList: IMVCList;
DataObj: TPair<string, TObject>;
lSJSON: string;
lJSON: string;
lSer: IMVCSerializer;
begin
if (FJSONModel <> '{}') and (not FJSONModel.IsEmpty) then
Exit;
FJSONModel := '{}';
if Assigned(ViewModel) then
begin
2017-05-25 16:57:49 +02:00
lSer := GetDefaultSerializer;
lSJSON := '{';
lFirst := True;
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);
if not lFirst then
lSJSON := lSJSON + ',';
2017-05-25 16:57:49 +02:00
lSJSON := lSJSON + '"' + DataObj.Key + '":' + lJSON;
lFirst := False;
end;
lSJSON := lSJSON + '}';
2017-05-25 16:57:49 +02:00
FJSONModel := lSJSON;
end;
end;
end.