#1 2017-08-03 11:03:43

Chaa
Member
Registered: 2011-03-26
Posts: 245

Virtual methods for loading templates in MVC Web App

In my TMVCApplication I need to load template files from application resources.
So I created patch that add some virtual functions to TMVCViewsMustache.
Source files:
mORMotMVC-20170801.zip

And now we can change file loading process.
For example, we need to load all template and static files from zip archive.
Zip archive can be stored in resources or on disk in separate file.

First, add TMVCViewsMustache descendant and override file loading methods:

type
    TMyViews = class(TMVCViewsMustache)
        FZip: TZipRead;
        function FindTemplates(const Mask: TFileName): TFileNameDynArray; override;
        function GetStaticFile(const aFileName: TFileName): RawByteString; override;
        function GetTemplate(const aFileName: TFileName): RawUTF8; override;
        function GetTemplateAge(const aFileName: TFileName): TDateTime; override;
    end;

function TMyViews.FindTemplates(const Mask: TFileName): TFileNameDynArray;
var
    i, c: Integer;
begin
    SetLength(Result, 0);
    for i := 0 to FZip.Count - 1 do
    begin
        if IsMatch(StringToUTF8(Mask), StringToUTF8(FZip.Entry[i].zipName), True) then
        begin
            c := Length(Result);
            SetLength(Result, c + 1);
            Result[c] := FZip.Entry[i].zipName;
        end;
    end;
end;

function TMyViews.GetStaticFile(const aFileName: TFileName): RawByteString;
var
    LIndex: Integer;
begin
    LIndex := FZip.NameToIndex(AFileName);
    if LIndex >= 0 then
        Result := FZip.Unzip(LIndex);
end;

function TMyViews.GetTemplate(const aFileName: TFileName): RawUTF8;
var
    LIndex: Integer;
begin
    LIndex := FZip.NameToIndex(AFileName);
    if LIndex >= 0 then
        Result := FZip.Unzip(LIndex);
end;

function TMyViews.GetTemplateAge(const aFileName: TFileName): TDateTime;
begin
    Result := 0.0;
end;

And then change Start method:

procedure TBlogApplication.Start(aServer: TSQLRestServer);
begin
  ...
  // publish IBlogApplication using Mustache Views
  fMainRunner := TMVCRunOnRestServer.Create(Self, nil, '',
    TMyViews.Create(Factory.InterfaceTypeInfo, (RestModel as TSQLRestServer).LogClass));
  ...
end;

Now files will be taken from zip archive (see SynZip.pas on how to create TZipRead instance).

Offline

#2 2018-02-27 11:18:15

Chaa
Member
Registered: 2011-03-26
Posts: 245

Re: Virtual methods for loading templates in MVC Web App

Offline

#3 2018-02-27 11:37:16

mpv
Member
From: Ukraine
Registered: 2012-03-24
Posts: 1,564
Website

Re: Virtual methods for loading templates in MVC Web App

@Chaa - did you see @Eugene Ilyin Boilerplate HTTP Server. It embed resources also

Offline

#4 2018-02-27 11:49:28

Chaa
Member
Registered: 2011-03-26
Posts: 245

Re: Virtual methods for loading templates in MVC Web App

Yes, I see it.
Boilerplate server extracts all resources in application working folder on startup.
In my case there is no file writes at all - template and static files extracted from ZIP on demand.

But this is only one use case.

My requirements is to allow customizing some templates/static files, while others templates/files can not be changed.

Offline

#5 2018-02-28 11:03:05

Chaa
Member
Registered: 2011-03-26
Posts: 245

Re: Virtual methods for loading templates in MVC Web App

ab, thanks for applying patch.

Continue playing with GitHub, forks and pull requests.

https://github.com/synopse/mORMot/pull/89

Offline

#6 2020-05-03 14:08:17

talexone
Member
Registered: 2013-08-21
Posts: 21

Re: Virtual methods for loading templates in MVC Web App

Since GetStaticFile was removed, is there another method to change static file loading?

Offline

#7 2020-05-04 06:43:07

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,359
Website

Re: Virtual methods for loading templates in MVC Web App

Using a method-based service?

Or please check https://synopse.info/fossil/info/1eb998e68a

Offline

#8 2020-05-04 13:09:40

talexone
Member
Registered: 2013-08-21
Posts: 21

Re: Virtual methods for loading templates in MVC Web App

Thanks ab.

I need to share a static content between controllers. When a first controller's Views contains templates like header.partial or footer.partial and I used it from another controller through {{>../header.partial}} there is problem with a static content due to relative paths (blog/sub-uri/.static instead of blog/.static/)

Offline

Board footer

Powered by FluxBB