delphimvcframework/samples/serversideviewcustom/MVCFramework.View.Renderers.TemplatePro.pas

104 lines
2.9 KiB
ObjectPascal
Raw Normal View History

2017-10-09 10:41:38 +02:00
// ***************************************************************************
//
// Delphi MVC Framework
//
// Copyright (c) 2010-2023 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
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,
TemplateProU,
MVCFramework.Cache,
Data.DB, System.Rtti;
{$WARNINGS OFF}
procedure TMVCTemplateProViewEngine.Execute(const ViewName: string;
const OutputStream: TStream);
2017-10-09 10:41:38 +02:00
var
lTP: TTemplateProEngine;
lViewFileName: string;
2018-01-29 17:30:53 +01:00
lViewTemplate: UTF8String;
lCacheItem: TMVCCacheItem;
2017-10-09 10:41:38 +02:00
begin
lTP := TTemplateProEngine.Create;
try
lViewFileName := GetRealFileName(ViewName);
if not FileExists(lViewFileName) then
2018-01-29 17:30:53 +01:00
begin
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
if not TMVCCacheSingleton.Instance.ContainsItem(lViewFileName, lCacheItem)
then
2017-10-09 10:41:38 +02:00
begin
lViewTemplate := TFile.ReadAllText(lViewFileName, TEncoding.UTF8);
lCacheItem := TMVCCacheSingleton.Instance.SetValue(lViewFileName,
lViewTemplate);
end
else
begin
if lCacheItem.TimeStamp < TFile.GetLastWriteTime(lViewFileName) then
begin
lViewTemplate := TFile.ReadAllText(lViewFileName, TEncoding.UTF8);
TMVCCacheSingleton.Instance.SetValue(lViewFileName,
lViewTemplate);
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
lTP.Execute(lViewTemplate, TTPObjectListDictionary(ViewModel),
TTPDatasetDictionary(ViewDataSets), OutputStream);
2017-10-09 10:41:38 +02:00
except
2018-01-29 17:30:53 +01:00
on E: EParserException do
begin
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.