2017-10-09 10:41:38 +02:00
|
|
|
// ***************************************************************************
|
|
|
|
//
|
|
|
|
// Delphi MVC Framework
|
|
|
|
//
|
2024-01-02 17:04:27 +01:00
|
|
|
// Copyright (c) 2010-2024 Daniele Teti and the DMVCFramework Team
|
2017-10-09 10:41:38 +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.TemplatePro;
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
|
|
|
MVCFramework, System.Generics.Collections, System.SysUtils,
|
2018-01-29 17:30:53 +01:00
|
|
|
MVCFramework.Commons, System.IOUtils, System.Classes;
|
2017-10-09 10:41:38 +02:00
|
|
|
|
|
|
|
type
|
2017-10-09 16:17:12 +02:00
|
|
|
{ This class implements the TemplatePro view engine for server side views }
|
2017-10-09 10:41:38 +02:00
|
|
|
TMVCTemplateProViewEngine = class(TMVCBaseViewEngine)
|
|
|
|
public
|
2019-02-01 20:10:51 +01:00
|
|
|
procedure Execute(const ViewName: string;
|
|
|
|
const OutputStream: TStream); override;
|
2017-10-09 10:41:38 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
|
|
|
uses
|
|
|
|
MVCFramework.Serializer.Defaults,
|
|
|
|
MVCFramework.Serializer.Intf,
|
|
|
|
MVCFramework.DuckTyping,
|
2024-08-12 10:38:16 +02:00
|
|
|
TemplatePro,
|
2017-10-09 10:41:38 +02:00
|
|
|
MVCFramework.Cache,
|
|
|
|
Data.DB, System.Rtti;
|
|
|
|
|
|
|
|
{$WARNINGS OFF}
|
|
|
|
|
2019-02-01 20:10:51 +01:00
|
|
|
procedure TMVCTemplateProViewEngine.Execute(const ViewName: string;
|
|
|
|
const OutputStream: TStream);
|
2017-10-09 10:41:38 +02:00
|
|
|
var
|
2024-08-12 10:38:16 +02:00
|
|
|
lTP: TTProCompiler;
|
2017-10-09 10:41:38 +02:00
|
|
|
lViewFileName: string;
|
2018-01-29 17:30:53 +01:00
|
|
|
lViewTemplate: UTF8String;
|
|
|
|
lCacheItem: TMVCCacheItem;
|
2024-08-12 10:38:16 +02:00
|
|
|
lCompiledTemplate: ITProCompiledTemplate;
|
|
|
|
lPair: TPair<String, TValue>;
|
2017-10-09 10:41:38 +02:00
|
|
|
begin
|
2024-08-12 10:38:16 +02:00
|
|
|
lTP := TTProCompiler.Create;
|
2017-10-09 10:41:38 +02:00
|
|
|
try
|
|
|
|
lViewFileName := GetRealFileName(ViewName);
|
|
|
|
if not FileExists(lViewFileName) then
|
2018-01-29 17:30:53 +01:00
|
|
|
begin
|
2019-02-01 20:10:51 +01:00
|
|
|
raise EMVCFrameworkViewException.CreateFmt('View [%s] not found',
|
|
|
|
[ViewName]);
|
2018-01-29 17:30:53 +01:00
|
|
|
end;
|
2017-10-09 10:41:38 +02:00
|
|
|
|
2019-02-01 20:10:51 +01:00
|
|
|
if not TMVCCacheSingleton.Instance.ContainsItem(lViewFileName, lCacheItem)
|
|
|
|
then
|
2017-10-09 10:41:38 +02:00
|
|
|
begin
|
|
|
|
lViewTemplate := TFile.ReadAllText(lViewFileName, TEncoding.UTF8);
|
2019-02-01 20:10:51 +01:00
|
|
|
lCacheItem := TMVCCacheSingleton.Instance.SetValue(lViewFileName,
|
|
|
|
lViewTemplate);
|
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
if lCacheItem.TimeStamp < TFile.GetLastWriteTime(lViewFileName) then
|
|
|
|
begin
|
|
|
|
lViewTemplate := TFile.ReadAllText(lViewFileName, TEncoding.UTF8);
|
2024-08-12 10:38:16 +02:00
|
|
|
TMVCCacheSingleton.Instance.SetValue(lViewFileName, lViewTemplate);
|
2019-02-01 20:10:51 +01:00
|
|
|
end;
|
2017-10-09 10:41:38 +02:00
|
|
|
end;
|
|
|
|
|
2018-01-29 17:30:53 +01:00
|
|
|
lViewTemplate := lCacheItem.Value.AsString;
|
2017-10-09 10:41:38 +02:00
|
|
|
try
|
2024-08-12 10:38:16 +02:00
|
|
|
lCompiledTemplate := lTP.Compile(lViewTemplate, lViewFileName);
|
|
|
|
if Assigned(ViewModel) then
|
|
|
|
begin
|
|
|
|
for lPair in ViewModel do
|
|
|
|
begin
|
|
|
|
lCompiledTemplate.SetData(lPair.Key, ViewModel[lPair.Key]);
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
//lCompiledTemplate.DumpToFile(TPath.Combine(AppPath, 'TProDump.txt'));
|
|
|
|
TStringStream(OutputStream).WriteString(lCompiledTemplate.Render);
|
2017-10-09 10:41:38 +02:00
|
|
|
except
|
2024-08-12 10:38:16 +02:00
|
|
|
on E: ETProException do
|
2018-01-29 17:30:53 +01:00
|
|
|
begin
|
2019-02-01 20:10:51 +01:00
|
|
|
raise EMVCViewError.CreateFmt('View [%s] error: %s (%s)',
|
|
|
|
[ViewName, E.Message, E.ClassName]);
|
2018-01-29 17:30:53 +01:00
|
|
|
end;
|
2017-10-09 10:41:38 +02:00
|
|
|
end;
|
|
|
|
finally
|
|
|
|
lTP.Free;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
end.
|