mirror of
https://github.com/danieleteti/delphimvcframework.git
synced 2024-11-15 07:45:54 +01:00
Improved template engines mechanism
This commit is contained in:
parent
01da47ea76
commit
4c00333115
@ -106,7 +106,7 @@ begin
|
||||
begin
|
||||
var lJItm := lJDevices.AddObject;
|
||||
lJItm.S['name'] := lItem;
|
||||
lJItm.B['selected'] := TArray.BinarySearch<String>(lDevices, lItem, lIdx);
|
||||
lJItm.B['selected'] := TArray.BinarySearch<String>(lPerson.Devices, lItem, lIdx);
|
||||
end;
|
||||
Result := Page(['editperson'], lJObj);
|
||||
finally
|
||||
|
@ -29,19 +29,11 @@ interface
|
||||
uses
|
||||
System.Rtti;
|
||||
|
||||
//uses
|
||||
// mormot.core.mustache;
|
||||
|
||||
function MyHelper1(const aValue: TValue; const aParameters: TArray<string>): string;
|
||||
function MyHelper2(const aValue: TValue; const aParameters: TArray<string>): string;
|
||||
|
||||
implementation
|
||||
|
||||
uses
|
||||
MVCFramework.View.Renderers.Mustache, System.SysUtils;
|
||||
|
||||
{ TMyMustacheHelpers }
|
||||
|
||||
function MyHelper1(const aValue: TValue; const aParameters: TArray<string>): string;
|
||||
begin
|
||||
Result := aValue.ToString + ' (I''m The MyHelper1)';
|
||||
|
@ -12,6 +12,7 @@ uses
|
||||
Winapi.Windows,
|
||||
{$ENDIF }
|
||||
IdHTTPWebBrokerBridge,
|
||||
TemplatePro,
|
||||
MVCFramework.View.Renderers.TemplatePro,
|
||||
Web.WebReq,
|
||||
Web.WebBroker,
|
||||
@ -19,8 +20,7 @@ uses
|
||||
WebSiteControllerU in 'WebSiteControllerU.pas',
|
||||
DAL in 'DAL.pas',
|
||||
MyDataModuleU in '..\renders\MyDataModuleU.pas' {MyDataModule: TDataModule},
|
||||
CustomTemplateProFiltersU in 'CustomTemplateProFiltersU.pas',
|
||||
TemplatePro in '..\..\..\templatepro\TemplatePro.pas';
|
||||
CustomTemplateProFiltersU in 'CustomTemplateProFiltersU.pas';
|
||||
|
||||
{$R *.res}
|
||||
|
||||
@ -35,7 +35,7 @@ begin
|
||||
LServer.DefaultPort := APort;
|
||||
LServer.Active := True;
|
||||
{$IFDEF MSWINDOWS}
|
||||
//ShellExecute(0, 'open', PChar('http://localhost:' + inttostr(APort)), nil, nil, SW_SHOW);
|
||||
ShellExecute(0, 'open', PChar('http://localhost:' + inttostr(APort)), nil, nil, SW_SHOW);
|
||||
{$ENDIF}
|
||||
LogI('Ctrl+C to stop the server');
|
||||
WaitForTerminationSignal;
|
||||
|
@ -6,8 +6,8 @@
|
||||
<MainSource>ServerSideViewsTemplatePro.dpr</MainSource>
|
||||
<Base>True</Base>
|
||||
<Config Condition="'$(Config)'==''">Debug</Config>
|
||||
<Platform Condition="'$(Platform)'==''">Win32</Platform>
|
||||
<TargetedPlatforms>1</TargetedPlatforms>
|
||||
<Platform Condition="'$(Platform)'==''">Win64</Platform>
|
||||
<TargetedPlatforms>3</TargetedPlatforms>
|
||||
<AppType>Console</AppType>
|
||||
<ProjectName Condition="'$(ProjectName)'==''">ServerSideViewsTemplatePro</ProjectName>
|
||||
</PropertyGroup>
|
||||
@ -35,6 +35,12 @@
|
||||
<Cfg_1>true</Cfg_1>
|
||||
<Base>true</Base>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="('$(Platform)'=='Win64' and '$(Cfg_1)'=='true') or '$(Cfg_1_Win64)'!=''">
|
||||
<Cfg_1_Win64>true</Cfg_1_Win64>
|
||||
<CfgParent>Cfg_1</CfgParent>
|
||||
<Cfg_1>true</Cfg_1>
|
||||
<Base>true</Base>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Config)'=='Release' or '$(Cfg_2)'!=''">
|
||||
<Cfg_2>true</Cfg_2>
|
||||
<CfgParent>Base</CfgParent>
|
||||
@ -57,12 +63,20 @@
|
||||
<DCC_Namespace>Winapi;System.Win;Data.Win;Datasnap.Win;Web.Win;Soap.Win;Xml.Win;Bde;$(DCC_Namespace)</DCC_Namespace>
|
||||
<VerInfo_Locale>1033</VerInfo_Locale>
|
||||
<DCC_ExeOutput>.\bin</DCC_ExeOutput>
|
||||
<VerInfo_Keys>CompanyName=;FileVersion=1.0.0.0;InternalName=;LegalCopyright=;LegalTrademarks=;OriginalFilename=;ProductVersion=1.0.0.0;Comments=;ProgramID=com.embarcadero.$(ModuleName);FileDescription=$(ModuleName);ProductName=$(ModuleName)</VerInfo_Keys>
|
||||
<VerInfo_Keys>CompanyName=;FileVersion=1.0.0.0;InternalName=;LegalCopyright=;LegalTrademarks=;OriginalFilename=;ProductVersion=1.0.0.0;Comments=;ProgramID=com.embarcadero.$(MSBuildProjectName);FileDescription=$(MSBuildProjectName);ProductName=$(MSBuildProjectName)</VerInfo_Keys>
|
||||
<Manifest_File>None</Manifest_File>
|
||||
<DCC_UsePackage>DBXSqliteDriver;RESTComponents;DataSnapServerMidas;DBXDb2Driver;DBXInterBaseDriver;vclactnband;frxe23;vclFireDAC;emsclientfiredac;DataSnapFireDAC;svnui;tethering;FireDACADSDriver;DBXMSSQLDriver;DatasnapConnectorsFreePascal;FireDACMSSQLDriver;vcltouch;vcldb;bindcompfmx;svn;Intraweb;DBXOracleDriver;ipstudiowinwordxp;inetdb;FmxTeeUI;FireDACIBDriver;fmx;fmxdae;DelphiCookbookListViewAppearance;vclib;FireDACDBXDriver;dbexpress;IndyCore;vclx;dsnap;DataSnapCommon;emsclient;FireDACCommon;bdertl;RESTBackendComponents;DataSnapConnectors;VCLRESTComponents;soapserver;frxTee23;JclDeveloperTools;vclie;CPortLibDXE;bindengine;DBXMySQLDriver;FireDACOracleDriver;CloudService;FireDACMySQLDriver;DBXFirebirdDriver;FireDACCommonDriver;DataSnapClient;inet;bindcompdbx;IndyIPCommon;vcl;DBXSybaseASEDriver;IndyIPServer;IndySystem;FireDACDb2Driver;dsnapcon;FireDACMSAccDriver;fmxFireDAC;FireDACInfxDriver;vclimg;TeeDB;FireDAC;Jcl;FireDACSqliteDriver;FireDACPgDriver;ibmonitor;FireDACASADriver;DBXOdbcDriver;FireDACTDataDriver;FMXTee;ipstudiowinclient;soaprtl;DbxCommonDriver;ibxpress;Tee;DataSnapServer;xmlrtl;soapmidas;DataSnapNativeClient;fmxobj;vclwinx;ibxbindings;rtl;FireDACDSDriver;DbxClientDriver;DTKANPRPackage;DBXSybaseASADriver;CustomIPTransport;vcldsnap;CodeSiteExpressPkg;SampleListViewMultiDetailAppearancePackage;bindcomp;appanalytics;ipstudiowin;DBXInformixDriver;officeXPrt;IndyIPClient;bindcompvcl;frxDB23;vcldbx;TeeUI;vclribbon;dbxcds;VclSmp;adortl;FireDACODBCDriver;JclVcl;DataSnapIndy10ServerTransport;dsnapxml;DataSnapProviderClient;dbrtl;inetdbxpress;FireDACMongoDBDriver;IndyProtocols;frx23;JclContainers;fmxase;$(DCC_UsePackage)</DCC_UsePackage>
|
||||
<AppDPIAwarenessMode>none</AppDPIAwarenessMode>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Base_Win64)'!=''">
|
||||
<DCC_UsePackage>DBXSqliteDriver;RESTComponents;DataSnapServerMidas;DBXDb2Driver;DBXInterBaseDriver;vclactnband;vclFireDAC;emsclientfiredac;DataSnapFireDAC;tethering;FireDACADSDriver;DBXMSSQLDriver;DatasnapConnectorsFreePascal;FireDACMSSQLDriver;vcltouch;vcldb;bindcompfmx;Intraweb;DBXOracleDriver;inetdb;FmxTeeUI;FireDACIBDriver;fmx;fmxdae;vclib;FireDACDBXDriver;dbexpress;IndyCore;vclx;dsnap;DataSnapCommon;emsclient;FireDACCommon;RESTBackendComponents;DataSnapConnectors;VCLRESTComponents;soapserver;vclie;bindengine;DBXMySQLDriver;FireDACOracleDriver;CloudService;FireDACMySQLDriver;DBXFirebirdDriver;FireDACCommonDriver;DataSnapClient;inet;bindcompdbx;IndyIPCommon;vcl;DBXSybaseASEDriver;IndyIPServer;IndySystem;FireDACDb2Driver;dsnapcon;FireDACMSAccDriver;fmxFireDAC;FireDACInfxDriver;vclimg;TeeDB;FireDAC;FireDACSqliteDriver;FireDACPgDriver;ibmonitor;FireDACASADriver;DBXOdbcDriver;FireDACTDataDriver;FMXTee;ipstudiowinclient;soaprtl;DbxCommonDriver;ibxpress;Tee;DataSnapServer;xmlrtl;soapmidas;DataSnapNativeClient;fmxobj;vclwinx;ibxbindings;rtl;FireDACDSDriver;DbxClientDriver;DBXSybaseASADriver;CustomIPTransport;vcldsnap;bindcomp;appanalytics;ipstudiowin;DBXInformixDriver;officeXPrt;IndyIPClient;bindcompvcl;TeeUI;vclribbon;dbxcds;VclSmp;adortl;FireDACODBCDriver;DataSnapIndy10ServerTransport;dsnapxml;DataSnapProviderClient;dbrtl;inetdbxpress;FireDACMongoDBDriver;IndyProtocols;fmxase;$(DCC_UsePackage)</DCC_UsePackage>
|
||||
<DCC_Namespace>Winapi;System.Win;Data.Win;Datasnap.Win;Web.Win;Soap.Win;Xml.Win;$(DCC_Namespace)</DCC_Namespace>
|
||||
<BT_BuildType>Debug</BT_BuildType>
|
||||
<VerInfo_Keys>CompanyName=;FileDescription=$(MSBuildProjectName);FileVersion=1.0.0.0;InternalName=;LegalCopyright=;LegalTrademarks=;OriginalFilename=;ProgramID=com.embarcadero.$(MSBuildProjectName);ProductName=$(MSBuildProjectName);ProductVersion=1.0.0.0;Comments=</VerInfo_Keys>
|
||||
<VerInfo_Locale>1033</VerInfo_Locale>
|
||||
<DCC_ExeOutput>.\bin</DCC_ExeOutput>
|
||||
<Manifest_File>(None)</Manifest_File>
|
||||
<AppDPIAwarenessMode>none</AppDPIAwarenessMode>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Cfg_1)'!=''">
|
||||
<DCC_Define>DEBUG;$(DCC_Define)</DCC_Define>
|
||||
@ -80,8 +94,9 @@
|
||||
<DCC_DynamicBase>false</DCC_DynamicBase>
|
||||
<DCC_IntegerOverflowCheck>true</DCC_IntegerOverflowCheck>
|
||||
<DCC_RangeChecking>true</DCC_RangeChecking>
|
||||
<VerInfo_Keys>CompanyName=;FileVersion=1.0.0.0;InternalName=;LegalCopyright=;LegalTrademarks=;OriginalFilename=;ProductVersion=1.0.0.0;Comments=;ProgramID=com.embarcadero.$(MSBuildProjectName);FileDescription=$(MSBuildProjectName);ProductName=$(MSBuildProjectName)</VerInfo_Keys>
|
||||
<AppDPIAwarenessMode>none</AppDPIAwarenessMode>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Cfg_1_Win64)'!=''">
|
||||
<VerInfo_Locale>1033</VerInfo_Locale>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Cfg_2)'!=''">
|
||||
<DCC_LocalDebugSymbols>false</DCC_LocalDebugSymbols>
|
||||
@ -104,7 +119,6 @@
|
||||
<DesignClass>TDataModule</DesignClass>
|
||||
</DCCReference>
|
||||
<DCCReference Include="CustomTemplateProFiltersU.pas"/>
|
||||
<DCCReference Include="..\..\..\templatepro\TemplatePro.pas"/>
|
||||
<BuildConfiguration Include="Base">
|
||||
<Key>Base</Key>
|
||||
</BuildConfiguration>
|
||||
@ -140,6 +154,18 @@
|
||||
<Overwrite>true</Overwrite>
|
||||
</Platform>
|
||||
</DeployFile>
|
||||
<DeployFile LocalName="bin\ServerSideViewsTemplatePro.exe" Configuration="Debug" Class="ProjectOutput">
|
||||
<Platform Name="Win64">
|
||||
<RemoteName>ServerSideViewsTemplatePro.exe</RemoteName>
|
||||
<Overwrite>true</Overwrite>
|
||||
</Platform>
|
||||
</DeployFile>
|
||||
<DeployFile LocalName="bin\ServerSideViewsTemplatePro.rsm" Configuration="Debug" Class="DebugSymbols">
|
||||
<Platform Name="Win64">
|
||||
<RemoteName>ServerSideViewsTemplatePro.rsm</RemoteName>
|
||||
<Overwrite>true</Overwrite>
|
||||
</Platform>
|
||||
</DeployFile>
|
||||
<DeployClass Name="AdditionalDebugSymbols">
|
||||
<Platform Name="OSX32">
|
||||
<Operation>1</Operation>
|
||||
@ -1001,7 +1027,7 @@
|
||||
</Deployment>
|
||||
<Platforms>
|
||||
<Platform value="Win32">True</Platform>
|
||||
<Platform value="Win64">False</Platform>
|
||||
<Platform value="Win64">True</Platform>
|
||||
</Platforms>
|
||||
</BorlandProject>
|
||||
<ProjectFileVersion>12</ProjectFileVersion>
|
||||
|
@ -1 +1 @@
|
||||
[{"first_name":"Daniele","last_name":"Teti","age":43,"devices":[],"guid":"49E8419B66C744529D63DB292389D541"},{"first_name":"Peter","last_name":"Parker","age":23,"devices":[],"guid":"C5489969A04D4AE4B00D4FC50C8ADB5C"},{"first_name":"Bruce","last_name":"Banner","age":50,"devices":[],"guid":"B41D180F30584558B4F4A1AAF849FFA3"},{"first_name":"Sue","last_name":"Storm","age":33,"devices":[],"guid":"3F058118B8C6470D9684E127BC30A84A"},{"first_name":"Scott","last_name":"Summer","age":35,"devices":[],"guid":"3518D8C6F60E42D19C5A7250ADEADC33"},{"first_name":"Reed","last_name":"Richards","age":45,"devices":["smartphone","desktop"],"guid":"09C85C9DEB714476AADB9EB0AD689536"},{"first_name":"45","last_name":"45","age":45,"devices":["smartphone","laptop"],"guid":"A53742C5F79E4274ADBD61DE8E579368"}]
|
||||
[{"first_name":"Daniele","last_name":"Teti","age":43,"devices":[],"guid":"49E8419B66C744529D63DB292389D541"},{"first_name":"Peter","last_name":"Parker","age":23,"devices":[],"guid":"C5489969A04D4AE4B00D4FC50C8ADB5C"},{"first_name":"Bruce","last_name":"Banner","age":50,"devices":[],"guid":"B41D180F30584558B4F4A1AAF849FFA3"},{"first_name":"Sue","last_name":"Storm","age":33,"devices":[],"guid":"3F058118B8C6470D9684E127BC30A84A"},{"first_name":"Scott","last_name":"Summer","age":35,"devices":[],"guid":"3518D8C6F60E42D19C5A7250ADEADC33"},{"first_name":"Reed","last_name":"Richards","age":45,"devices":["smartphone","desktop"],"guid":"09C85C9DEB714476AADB9EB0AD689536"}]
|
@ -4,9 +4,6 @@
|
||||
</div>
|
||||
</div>
|
||||
<div class="row_fluid">
|
||||
<div class="col-sm-12">
|
||||
<span>N.B. All these views are UTF-8 encoded with BOM</span>
|
||||
</div>
|
||||
<div class="col-sm-8 bg-primary">
|
||||
<span>Powered by DMVCFramework</span>
|
||||
</div>
|
||||
|
@ -277,3 +277,4 @@ begin
|
||||
end;
|
||||
|
||||
end.
|
||||
|
||||
|
@ -52,7 +52,7 @@ type
|
||||
function RenderJSON(lViewEngine: TSynMustache; const JSON: UTF8String; Partials: TSynMustachePartials;
|
||||
Helpers: TSynMustacheHelpers; OnTranslate: TOnStringTranslate; EscapeInvert: boolean): UTF8String; virtual;
|
||||
public
|
||||
procedure Execute(const ViewName: string; const OutputStream: TStream); override;
|
||||
procedure Execute(const ViewName: string; const Builder: TStringBuilder); override;
|
||||
constructor Create(const AEngine: TMVCEngine; const AWebContext: TWebContext;
|
||||
const AViewModel: TMVCViewDataObject;
|
||||
const AContentType: string); override;
|
||||
@ -131,12 +131,11 @@ begin
|
||||
Result := lViewEngine.RenderJSON(JSON, Partials, Helpers, OnTranslate, EscapeInvert);
|
||||
end;
|
||||
|
||||
procedure TMVCMustacheViewEngine.Execute(const ViewName: string; const OutputStream: TStream);
|
||||
procedure TMVCMustacheViewEngine.Execute(const ViewName: string; const Builder: TStringBuilder);
|
||||
var
|
||||
lViewFileName: string;
|
||||
lViewTemplate: UTF8String;
|
||||
lViewEngine: TSynMustache;
|
||||
lSW: TStreamWriter;
|
||||
begin
|
||||
PrepareModels;
|
||||
lViewFileName := GetRealFileName(ViewName);
|
||||
@ -144,12 +143,7 @@ begin
|
||||
raise EMVCFrameworkViewException.CreateFmt('View [%s] not found', [ViewName]);
|
||||
lViewTemplate := StringToUTF8(TFile.ReadAllText(lViewFileName, TEncoding.UTF8));
|
||||
lViewEngine := TSynMustache.Parse(lViewTemplate);
|
||||
lSW := TStreamWriter.Create(OutputStream);
|
||||
try
|
||||
lSW.Write(UTF8Tostring(RenderJSON(lViewEngine, FJSONModelAsString, fPartials, fHelpers, nil, false)));
|
||||
finally
|
||||
lSW.Free;
|
||||
end;
|
||||
Builder.Append(UTF8Tostring(RenderJSON(lViewEngine, FJSONModelAsString, fPartials, fHelpers, nil, false)));
|
||||
end;
|
||||
|
||||
procedure TMVCMustacheViewEngine.LoadHelpers;
|
||||
|
@ -34,7 +34,7 @@ type
|
||||
{ This class implements the TemplatePro view engine for server side views }
|
||||
TMVCTemplateProViewEngine = class(TMVCBaseViewEngine)
|
||||
public
|
||||
procedure Execute(const ViewName: string; const OutputStream: TStream); override;
|
||||
procedure Execute(const ViewName: string; const Builder: TStringBuilder); override;
|
||||
end;
|
||||
|
||||
implementation
|
||||
@ -73,8 +73,7 @@ begin
|
||||
end;
|
||||
end;
|
||||
|
||||
procedure TMVCTemplateProViewEngine.Execute(const ViewName: string;
|
||||
const OutputStream: TStream);
|
||||
procedure TMVCTemplateProViewEngine.Execute(const ViewName: string; const Builder: TStringBuilder);
|
||||
var
|
||||
lTP: TTProCompiler;
|
||||
lViewFileName: string;
|
||||
@ -120,7 +119,7 @@ begin
|
||||
end;
|
||||
lCompiledTemplate.AddFilter('json', DumpAsJSONString);
|
||||
//lCompiledTemplate.DumpToFile(TPath.Combine(AppPath, 'TProDump.txt'));
|
||||
TStringStream(OutputStream).WriteString(lCompiledTemplate.Render);
|
||||
Builder.Append(lCompiledTemplate.Render);
|
||||
except
|
||||
on E: ETProException do
|
||||
begin
|
||||
|
@ -1286,7 +1286,7 @@ type
|
||||
const AContentType: string); overload; virtual;
|
||||
destructor Destroy; override;
|
||||
|
||||
procedure Execute(const ViewName: string; const OutputStream: TStream); virtual; abstract;
|
||||
procedure Execute(const ViewName: string; const Builder: TStringBuilder); virtual; abstract;
|
||||
|
||||
property ViewName: string read FViewName;
|
||||
property WebContext: TWebContext read FWebContext;
|
||||
@ -4149,9 +4149,9 @@ end;
|
||||
|
||||
function TMVCController.GetRenderedView(const AViewNames: TArray<string>; const JSONModel: TJSONObject): string;
|
||||
var
|
||||
lView: TMVCBaseViewEngine; lViewName: string; lStrStream: TStringStream;
|
||||
lView: TMVCBaseViewEngine; lViewName: string; lStrStream: TStringBuilder;
|
||||
begin
|
||||
lStrStream := TStringStream.Create('', TEncoding.UTF8);
|
||||
lStrStream := TStringBuilder.Create;
|
||||
try
|
||||
lView := FEngine.ViewEngineClass.Create(Engine, Context, FViewModel, JSONModel, ContentType);
|
||||
try
|
||||
@ -4162,8 +4162,7 @@ begin
|
||||
finally
|
||||
lView.Free;
|
||||
end;
|
||||
lStrStream.Position := 0;
|
||||
Result := lStrStream.DataString;
|
||||
Result := lStrStream.ToString;
|
||||
finally
|
||||
lStrStream.Free;
|
||||
end;
|
||||
@ -4792,9 +4791,9 @@ end;
|
||||
|
||||
function TMVCController.GetRenderedView(const AViewNames: TArray<string>): string;
|
||||
var
|
||||
lView: TMVCBaseViewEngine; lViewName: string; lStrStream: TStringStream;
|
||||
lView: TMVCBaseViewEngine; lViewName: string; lStrStream: TStringBuilder;
|
||||
begin
|
||||
lStrStream := TStringStream.Create('', TEncoding.UTF8);
|
||||
lStrStream := TStringBuilder.Create;
|
||||
try
|
||||
lView := FEngine.ViewEngineClass.Create(
|
||||
Engine, Context,
|
||||
@ -4808,8 +4807,7 @@ begin
|
||||
finally
|
||||
lView.Free;
|
||||
end;
|
||||
lStrStream.Position := 0;
|
||||
Result := lStrStream.DataString;
|
||||
Result := lStrStream.ToString;
|
||||
finally
|
||||
lStrStream.Free;
|
||||
end;
|
||||
|
Loading…
Reference in New Issue
Block a user