#1 2023-02-26 08:11:27

suoke
Member
Registered: 2023-02-09
Posts: 8
Website

Suggest adding for update Partial when mustache render.

In order to detect the update of Partial contained in Mustache when I access it, I made the following changes:
for core.mustache

  TGetPartialNotify = function(const PartialName: RawUtf8;
    var partial: TSynMustache): boolean of object; //add

and

  TSynMustache = class protected fTemplate: RawUtf8;

  fGetPartialNotify: TGetPartialNotify;
  property GetPartialNotify: TGetPartialNotify read fGetPartialNotify
    write fGetPartialNotify;
        mtPartial:
          begin
            partial := nil;
            if fInternalPartials <> nil then
              partial := fInternalPartials.GetPartial(Value);
            if (partial = nil) and (Context.fOwner <> self) then
              // recursive call
              partial := Context.fOwner.fInternalPartials.GetPartial(Value);
            if (partial = nil) and (Context.Partials <> nil) then
              partial := Context.Partials.GetPartial(Value);
           
            if partial <> nil then
            begin
              //add here
              if Assigned(fGetPartialNotify) then
              begin
                if fGetPartialNotify(Value, partial) then
                begin
                  Context.Partials.AddOrReplaceObject(Value, partial);//!
                end;
              end;
              //end.
              partial.RenderContext(Context, 0, high(partial.fTags));
            end;
          end;
procedure TSynMustachePartials.AddOrReplaceObject(const aName: RawUtf8;
  oMustache: TSynMustache);
begin
  fList.AddObject(aName, oMustache, { raiseexisting= } false, nil,
    { replace= } true, false);

end;

Specify the method when creating mustache, and borrow the implementation method of TMvcViewsMustache. When calling mustache, you can detect the change of partia and update it.

            mustache := TSynMustache.Parse(Template);
            mustache.GetPartialNotify := mustacheParitalgetNotify;
function TViewsMustache.mustacheParitalgetNotify(const PartialName: RawUtf8;
  var partial: TSynMustache): boolean;
var
  vP: TViewPartial;
  age: TUnixTime;
begin
  Result := false;
  vP := FPartials.Items[PartialName];
  if vP <> nil then
  begin
    vP.Locker.ProtectMethod;
    if (vP.partial = nil) or ((fViewTemplateFileTimestampMonitor <> 0) and
      (vP.FileAgeCheckTick < GetTickCount64)) then
    begin
      age := GetTemplateAge(vP.ShortFileName);
      if (age <> vP.FileAgeLast) then
      begin
        Result := true;
        partial := TSynMustache.Parse(GetTemplate(vP.ShortFileName));

        if fViewTemplateFileTimestampMonitor <> 0 then
          vP.FileAgeCheckTick := GetTickCount64 +
            int64(fViewTemplateFileTimestampMonitor) * int64(1000);
      end;
    end;
  end;
end;

Last edited by suoke (2023-02-26 08:13:08)

Offline

Board footer

Powered by FluxBB