2024-10-11 16:02:29 +02:00
|
|
|
// ***************************************************************************
|
|
|
|
//
|
|
|
|
// Delphi MVC Framework
|
|
|
|
//
|
|
|
|
// Copyright (c) 2010-2024 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.WebStencils;
|
|
|
|
|
2024-10-14 09:21:56 +02:00
|
|
|
{$I dmvcframework.inc}
|
|
|
|
|
2024-10-11 16:02:29 +02:00
|
|
|
interface
|
|
|
|
|
2024-10-14 09:21:56 +02:00
|
|
|
{$if Defined(WEBSTENCILS)}
|
|
|
|
|
|
|
|
//This unit is usable from Delphi 12.2+
|
|
|
|
|
2024-10-11 16:02:29 +02:00
|
|
|
uses
|
|
|
|
MVCFramework, System.Generics.Collections, System.SysUtils,
|
2024-10-14 09:21:56 +02:00
|
|
|
MVCFramework.Commons, System.IOUtils, System.Classes, Web.Stencils;
|
2024-10-11 16:02:29 +02:00
|
|
|
|
|
|
|
type
|
2024-10-14 09:21:56 +02:00
|
|
|
TMVCWebStencilsEvent = reference to procedure(const WebStencilsProcessor: TWebStencilsProcessor);
|
|
|
|
|
2024-10-11 16:02:29 +02:00
|
|
|
{ This class implements the WebStencils view engine for server side views }
|
|
|
|
TMVCWebStencilsViewEngine = class(TMVCBaseViewEngine)
|
|
|
|
public
|
|
|
|
procedure Execute(const ViewName: string; const Builder: TStringBuilder); override;
|
|
|
|
end;
|
|
|
|
|
2024-10-14 09:21:56 +02:00
|
|
|
TMVCWebStencilsConfiguration = class sealed
|
|
|
|
private
|
|
|
|
class var fOnProcessorConfiguration: TMVCWebStencilsEvent;
|
|
|
|
public
|
|
|
|
class property OnProcessorConfiguration: TMVCWebStencilsEvent
|
|
|
|
read fOnProcessorConfiguration
|
|
|
|
write fOnProcessorConfiguration;
|
|
|
|
end;
|
|
|
|
|
|
|
|
{$endif}
|
|
|
|
|
2024-10-11 16:02:29 +02:00
|
|
|
implementation
|
|
|
|
|
2024-10-14 09:21:56 +02:00
|
|
|
|
|
|
|
{$if Defined(WEBSTENCILS)}
|
2024-10-11 16:02:29 +02:00
|
|
|
uses
|
|
|
|
MVCFramework.Serializer.Defaults,
|
|
|
|
MVCFramework.Serializer.Intf,
|
|
|
|
MVCFramework.DuckTyping,
|
|
|
|
System.Bindings.EvalProtocol,
|
|
|
|
System.Bindings.Methods,
|
|
|
|
MVCFramework.Cache,
|
|
|
|
Data.DB,
|
|
|
|
System.Rtti,
|
|
|
|
JsonDataObjects;
|
|
|
|
|
|
|
|
{$WARNINGS OFF}
|
|
|
|
|
|
|
|
var
|
|
|
|
gFunctionInitialized: Boolean = False;
|
|
|
|
gWSLock: TObject = nil;
|
|
|
|
|
|
|
|
function GetDataSetOrObjectListCount(const aValue: TValue; const aParameters: TArray<string>): TValue;
|
|
|
|
var
|
|
|
|
lWrappedList: IMVCList;
|
|
|
|
begin
|
|
|
|
if not aValue.IsObject then
|
|
|
|
begin
|
|
|
|
Result := False;
|
|
|
|
end;
|
|
|
|
|
|
|
|
if Length(aParameters) <> 0 then
|
|
|
|
begin
|
|
|
|
Result := '(Error: Expected 0 params, got ' + Length(aParameters).ToString + ')';
|
|
|
|
end;
|
|
|
|
|
|
|
|
if aValue.AsObject is TDataSet then
|
|
|
|
begin
|
|
|
|
Result := TDataSet(aValue.AsObject).RecordCount;
|
|
|
|
end
|
|
|
|
else if aValue.AsObject is TJsonArray then
|
|
|
|
begin
|
|
|
|
Result := TJsonArray(aValue.AsObject).Count;
|
|
|
|
end
|
|
|
|
else if aValue.AsObject is TJsonObject then
|
|
|
|
begin
|
|
|
|
Result := TJsonObject(aValue.AsObject).Count;
|
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
if (aValue.AsObject <> nil) and TDuckTypedList.CanBeWrappedAsList(aValue.AsObject, lWrappedList) then
|
|
|
|
begin
|
|
|
|
Result := lWrappedList.Count;
|
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
Result := False;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function DumpAsJSONString(const aValue: TValue; const aParameters: TArray<string>): TValue;
|
|
|
|
var
|
|
|
|
lWrappedList: IMVCList;
|
|
|
|
begin
|
|
|
|
if not aValue.IsObject then
|
|
|
|
begin
|
|
|
|
Result := '(Error: Cannot serialize non-object as JSON)';
|
|
|
|
end;
|
|
|
|
|
|
|
|
if TDuckTypedList.CanBeWrappedAsList(aValue.AsObject, lWrappedList) then
|
|
|
|
begin
|
|
|
|
Result := GetDefaultSerializer.SerializeCollection(lWrappedList)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
if aValue.AsObject is TDataSet then
|
|
|
|
Result := GetDefaultSerializer.SerializeDataSet(TDataSet(aValue.AsObject))
|
|
|
|
else
|
|
|
|
Result := GetDefaultSerializer.SerializeObject(aValue.AsObject);
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
2024-10-14 09:21:56 +02:00
|
|
|
function MakeMethodJSON: IInvokable;
|
|
|
|
begin
|
|
|
|
Result := MakeInvokable(function(Args: TArray<IValue>): IValue
|
|
|
|
begin
|
|
|
|
Result := TValueWrapper.Create(DumpAsJSONString(Args[0].GetValue.AsObject, []));
|
|
|
|
end)
|
|
|
|
end;
|
|
|
|
|
2024-10-11 16:02:29 +02:00
|
|
|
procedure RegisterWSFunctions(WSProcessor: TWebStencilsProcessor);
|
|
|
|
begin
|
|
|
|
if gFunctionInitialized then Exit;
|
|
|
|
TMonitor.Enter(gWSLock);
|
|
|
|
try
|
|
|
|
if gFunctionInitialized then Exit;
|
|
|
|
gFunctionInitialized := True;
|
2024-10-14 09:21:56 +02:00
|
|
|
|
|
|
|
|
2024-10-11 16:02:29 +02:00
|
|
|
TBindingMethodsFactory.RegisterMethod(
|
|
|
|
TMethodDescription.Create(
|
|
|
|
MakeInvokable(function(Args: TArray<IValue>): IValue
|
|
|
|
begin
|
2024-10-14 09:21:56 +02:00
|
|
|
if Length(Args) <> 1 then
|
|
|
|
begin
|
|
|
|
raise EMVCSSVException.Create(500, 'Expected 1 parameter in "JSON" function, got ' + Length(Args).ToString);
|
|
|
|
end;
|
2024-10-11 16:02:29 +02:00
|
|
|
Result := TValueWrapper.Create(DumpAsJSONString(Args[0].GetValue.AsObject, []));
|
2024-10-14 09:21:56 +02:00
|
|
|
end) as IInvokable,
|
2024-10-11 16:02:29 +02:00
|
|
|
'json', 'json', '', True, 'Serialize an object to JSON', nil));
|
2024-10-14 09:21:56 +02:00
|
|
|
|
2024-10-11 16:02:29 +02:00
|
|
|
finally
|
|
|
|
TMonitor.Exit(gWSLock);
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TMVCWebStencilsViewEngine.Execute(const ViewName: string; const Builder: TStringBuilder);
|
|
|
|
var
|
|
|
|
lViewFileName: string;
|
|
|
|
lWebStencilsProcessor: TWebStencilsProcessor;
|
|
|
|
lPair: TPair<String, TValue>;
|
|
|
|
begin
|
|
|
|
lViewFileName := GetRealFileName(ViewName);
|
|
|
|
if lViewFileName.IsEmpty then
|
2024-10-14 09:21:56 +02:00
|
|
|
raise EMVCSSVException.CreateFmt('View [%s] not found', [ViewName]);
|
2024-10-11 16:02:29 +02:00
|
|
|
|
|
|
|
lWebStencilsProcessor := TWebStencilsProcessor.Create(nil);
|
|
|
|
try
|
|
|
|
RegisterWSFunctions(lWebStencilsProcessor);
|
|
|
|
try
|
2024-10-14 09:21:56 +02:00
|
|
|
if Assigned(TMVCWebStencilsConfiguration.fOnProcessorConfiguration) then
|
|
|
|
begin
|
|
|
|
TMVCWebStencilsConfiguration.OnProcessorConfiguration(lWebStencilsProcessor);
|
|
|
|
end;
|
|
|
|
//lWebStencilsProcessor.OnFile := Self.OnFile; {12.2, any filename starting with ..\ is not read correctly by the parser. Is it a feature? }
|
2024-10-11 16:02:29 +02:00
|
|
|
lWebStencilsProcessor.InputFileName := lViewFileName;
|
2024-10-14 09:21:56 +02:00
|
|
|
lWebStencilsProcessor.PathTemplate := Config[TMVCConfigKey.ViewPath];
|
2024-10-11 16:02:29 +02:00
|
|
|
if Assigned(ViewModel) then
|
|
|
|
begin
|
|
|
|
for lPair in ViewModel do
|
|
|
|
begin
|
|
|
|
if ViewModel[lPair.Key].IsObject then
|
|
|
|
lWebStencilsProcessor.AddVar(lPair.Key, ViewModel[lPair.Key].AsObject, False);
|
|
|
|
end;
|
|
|
|
end;
|
2024-10-14 09:21:56 +02:00
|
|
|
if Assigned(FBeforeRenderCallback) then
|
|
|
|
begin
|
|
|
|
FBeforeRenderCallback(lWebStencilsProcessor);
|
|
|
|
end;
|
2024-10-11 16:02:29 +02:00
|
|
|
Builder.Append(lWebStencilsProcessor.Content);
|
|
|
|
except
|
|
|
|
on E: EWebStencilsException do
|
|
|
|
begin
|
|
|
|
raise EMVCViewError.CreateFmt('View [%s] error: %s (%s)',
|
|
|
|
[ViewName, E.Message, E.ClassName]);
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
finally
|
|
|
|
lWebStencilsProcessor.Free;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
initialization
|
|
|
|
|
|
|
|
gWSLock := TObject.Create;
|
|
|
|
|
|
|
|
finalization
|
|
|
|
|
|
|
|
FreeAndNil(gWSLock);
|
|
|
|
|
2024-10-14 09:21:56 +02:00
|
|
|
{$endif}
|
2024-10-11 16:02:29 +02:00
|
|
|
|
|
|
|
end.
|