#1 2019-02-06 14:17:41

array81
Member
From: Italy
Registered: 2010-07-23
Posts: 411

Change view path on fly for MVC Web Application

On my MVC Web Application I have a folder structure similar to the example 30:

ROOT
 |-views
    |-.static

Now I need add a billing system on my application. I don't want to create a rigid system.
My idea is create a "template system", the user can be edit (and if he want add) 2 files for any bill type: un javascript file for "logical" (I use spidermonkey for it) and a html file for "model" to use with mustache.
I'd like create a folder on ROOT with these files, in alternative in fact the user should change "views" folder with the risk of doing damage:

ROOT
 |-views
    |-.static
 |-templates
    |-.static

So I should customize the VIEW path and VIEW filename for a specific command, for example when the user call "CreateInvoice" command the application should check invoice templete used by user on database and then use the specific VIEW from "templates" folder.

Is this is possible? How?

Offline

#2 2019-02-07 04:11:13

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

Re: Change view path on fly for MVC Web Application

Class TMVCViewsMustache has virtual methods FindTemplates, GetTemplate and GetStaticFile.
So you can customize process of reading templates and static files.

For example, see Virtual methods for loading templates in MVC Web App.

Offline

#3 2019-02-07 11:55:42

array81
Member
From: Italy
Registered: 2010-07-23
Posts: 411

Re: Change view path on fly for MVC Web Application

Chaa wrote:

Class TMVCViewsMustache has virtual methods FindTemplates, GetTemplate and GetStaticFile.
So you can customize process of reading templates and static files.

For example, see Virtual methods for loading templates in MVC Web App.

If I understand your post, in this way I need "handle" ALL views and not only a part of them, right?
In my case I should handle (I think by filename) both "template" views and "basic" views, right?

Offline

#4 2019-09-09 17:23:06

array81
Member
From: Italy
Registered: 2010-07-23
Posts: 411

Re: Change view path on fly for MVC Web Application

With TMVCViewsMustache I can customize folder templates by template filename.
But I can customize filename view from command of the web application?

I have a command like "ShowInvoicePreview" to create an invoice preview, however the user can choose between more invoice template (I use mustache and "view style" saved on custom path) besides user can create more of them. So the "ShowInvoicePreview" should read from db the invoice template for current user and use the right view (saved on custom path).

By default mORMot use a view with the same name of command, is there a way to customize the view name from code?

Offline

#5 2019-09-10 03:47:36

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

Re: Change view path on fly for MVC Web Application

If you need per-user template, you can do it in that way:
1. Define ShowInvoicePreview.html like
{{#Scope}}{{{Result}}}{{/Scope}}
2. In ShowInvoicePreview function call GetUserInvoicePreview.
3. Create GetUserInvoicePreview as in example:

type
    TMyAppViews = class(TMVCViewsMustache);

function TMyApp.GetUserInvoicePreview(var Scope: Variant): RawUTF8;
var
    LTemplate: RawUTF8;
    LMustache: TSynMustache;
    LContext: Variant;
begin
    LTemplate := <Load from DB...>;
    LContext := _ObjFast(['main', GetViewInfo(-1), 'Scope', Scope]);
    // TSynMustache maintain internal template cache
    LMustache := TSynMustache.Parse(LTemplate);
    Result := LMustache.Render(LContext,
        TMyViews(FViews).fViewPartials,
        TMyViews(FViews).fViewHelpers);
end;

procedure TMyApp.ShowInvoicePreview(var Scope: Variant);
var
    LContent: RawUTF8;
begin
    Scope := _ObjFast([...]);
    LContent := GetUserInvoicePreview(Scope);
    _ObjAddProps(['Result', LContent], Scope);
end;

Offline

#6 2019-09-13 17:18:22

array81
Member
From: Italy
Registered: 2010-07-23
Posts: 411

Re: Change view path on fly for MVC Web Application

This is almost perfect, I have only a problem: pass the data from function to method.

The user call NewInvoice method, so he can use a form the set all data of new invoice.
There are 2 buttons, with the first button he can call SaveInvoice function and save the new invoice with all data from html form. With second button he can call InvoicePreview function to have a preview ON AN OTHER browser tab.
With you code InvoicePreview function can call ShowInvoicePreview method and then show custom invoice but how can pass html data sent with InvoicePreview function to ShowInvoicePreview method?

I hope I explained myself.

Offline

Board footer

Powered by FluxBB