#1 2025-07-14 06:21:48

Alek
Member
From: Russia
Registered: 2014-07-04
Posts: 66

MVC rendering

Hello everyone

Please tell me if it is possible to force the page rendering if the "mustache partial" has changed, but the html file itself has not?

Offline

#2 2025-07-14 07:40:15

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 15,107
Website

Re: MVC rendering

TMvcApplication.FlushAnyCache should do the trick as far as I can tell.

Offline

#3 2025-07-14 08:06:25

Alek
Member
From: Russia
Registered: 2014-07-04
Posts: 66

Re: MVC rendering

1 I start the МVС server, FlushAnyCache starts (reads and downloads all partials)
2 I start the browser
3 I open the page (partial is loaded and displayed on the page)
4 I change the html text in partial on the МVС server
5 ???
6 When updating the page, the data does not change (even if the cache is disabled in the browser and on the МVС server)
7 When the МVС server is restarted, everything is displayed perfectly when the browser page is refreshed.

I'm not really understanding where I need to call FlushAnyCache?

UPDATED: and I also don't quite understand the meaning of the variables.

  TMvcRendererCachePolicy = (
    cacheNone,
    cacheRootIgnoringSession,
    cacheRootIfSession,
    cacheRootIfNoSession,
    cacheRootWithSession,
    cacheWithParametersIgnoringSession,
    cacheWithParametersIfSession,
    cacheWithParametersIfNoSession);

Last edited by Alek (2025-07-14 08:34:17)

Offline

#4 2025-07-14 09:13:10

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 15,107
Website

Re: MVC rendering

You need to call FlushAnyCache when you know that the partial have changed.

I have forced the reload of the partials from this method:
https://github.com/synopse/mORMot2/commit/83dc72825

Offline

#5 2025-07-14 11:04:03

flydev
Member
From: France
Registered: 2020-11-27
Posts: 101
Website

Re: MVC rendering

@Alek, to force partial changes to be reloaded, apply theses modifications:

on mormot.rest.mvc.pas#L181 add:

FViewPath: RawUtf8;

and the public property:

property ViewPath: RawUtf8 read FViewPath write FViewPath;

on line mormot.rest.mvc.pas#L1245 add:

{$ifdef DEBUG}
fViewPartials.ViewPath := folder;
{$endif}

replace mormot.core.mustache.pas#L1317-L1332 with:

function TSynMustachePartials.GetPartial(
  const PartialName: RawUTF8): TSynMustache;
var
  i: PtrInt;
  Temp: TSynMustache;
begin
  if self=nil then begin
    result := nil;
    exit;
  end;
  i := fList.IndexOf(PartialName);
  if i<0 then
    result := nil
  else 
  begin
    // Partial files by default in mORMot are loaded just once and then cached which is very bad
    // during debugging when files are changed and it requires constant restart of the application.
    // With this change we're disabling cache in debugMode: -dDEBUG on fpc
    Result := TSynMustache(fList.Objects[i]);
    {$ifdef debug}
    begin
      Temp := TSynMustache.Parse(AnyTextFileToRawUTF8(UTF8ToString(MakePath([FViewPath, PartialName + '.partial'])), true));
      if Temp.Template <> '' then
        Result := Temp;
    end;
    {$endif debug}
  end;

@ab, any chance you consider adding it or similar - or did we missed something?

---
Edit: I missed last post of @ab - watching for files changes and triggering FlushAnyCache() should be the right way to do it while developping. Anyway, the method presented here still make getting partial markup changes easier.
---


Alek wrote:

UPDATED: and I also don't quite understand the meaning of the variables.

I made a gist trying to explain it (should be correct), read there: https://gist.github.com/flydev-fr/e0f0a … c2ac78f2bc


Call FlushAnyCache when you change data in your model. If I am correct, on the blog sample, the default data is cleared and filled with new data again if empty on GetViewInfo(). The method is virtual so you can implement your own flush logic there.

Last edited by flydev (2025-07-14 11:20:32)

Offline

#6 2025-07-16 04:48:43

Alek
Member
From: Russia
Registered: 2014-07-04
Posts: 66

Re: MVC rendering

Thank you @flydev for the detailed explanation. I think using the GetViewInfo function will be the easiest way for me.

Offline

#7 2025-07-16 18:20:54

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 15,107
Website

Re: MVC rendering

I tried to add some more documentation about caching policy in the official documentation:
https://github.com/synopse/mORMot2/commit/c5cb0ba80eb
(with a link to your great gist - thanks @flydev!)

I also made some refactoring and optimizations on the core MVC rendering process.

Offline

Board footer

Powered by FluxBB