#1 2023-01-30 11:19:41

dcoun
Member
From: Crete, Greece
Registered: 2020-02-18
Posts: 430

Mustache: Helper can have more than one arguments

Mustache in Mormot is really helpful, and a not so well documented feature is even more helpful:
In mormot's mustache we could have helpers where a function in our compiled code is called when the helper found in the template like the following:

{{helpername value_todealwith}}

A procedure like the following is called that it is predefined and must be added/delete with TSynMustache.HelperAdd/HelperDelete or better with TSynMustache.HelpersGetStandardList that will add also the standard helpers.

procedure(const Value: variant; out Result: variant) of object;

The exciting feature shown in the mormot's source is that the variant value passed to the helper can be a comma delimited array of values:

{{helpername value1,value2,value3}}

An easy way to parse them, as you can read it in the source for existing standard helpers is:

procedure healper(const Value: variant; out Result: variant);
var dv: PDocVariantData; wasString: boolean; work2do:RawUtf8;
begin SetVariantNull(result{%H-});
  if not _SafeArray(Value, dv) then exit;
  if dv^.count=2 then
  VariantToUtf8(dv^.Values[1], work2do, wasString);
...

Thank you a lot @ab for this framework

Offline

#2 2023-01-30 13:43:59

tbo
Member
Registered: 2015-04-20
Posts: 353

Re: Mustache: Helper can have more than one arguments

dcoun wrote:

Mustache in Mormot is really helpful, and a not so well documented feature is even more helpful:

Or you haven't studied all the available examples, e.g. WebMustache here. wink

With best regards
Thomas

Offline

#3 2023-01-30 15:45:30

dcoun
Member
From: Crete, Greece
Registered: 2020-02-18
Posts: 430

Re: Mustache: Helper can have more than one arguments

I can not miss your tutorials and I enjoy reading and testing them.
The exciting that lead me to start this thread is that a helper can take an array of arguments and that helps a lot because with just one helper you can include optional "modifiers" arguments to the response of the helper

Offline

#4 2023-01-31 13:43:22

dcoun
Member
From: Crete, Greece
Registered: 2020-02-18
Posts: 430

Re: Mustache: Helper can have more than one arguments

I was wondering if there is a way to pass the content of a template variable as one argument in a helper.
Something like the following:

{{helpername value1,value2,{{otherVariable}}}}

it just works like that:

{{helpername value1,value2,otherVariable}}

where otherVariable exists in the json context as name in a name:value pair

Last edited by dcoun (2023-01-31 13:46:02)

Offline

#5 2023-01-31 14:03:03

dcoun
Member
From: Crete, Greece
Registered: 2020-02-18
Posts: 430

Re: Mustache: Helper can have more than one arguments

I find I find a small bug:
The first argument in a helper MUST be a variable from Json.
If not, only the first argument exists in the Variant passed to the helper

Offline

#6 2023-11-06 10:12:52

danielet79
Member
Registered: 2023-11-06
Posts: 1

Re: Mustache: Helper can have more than one arguments

Any news about this bug?

Offline

#7 2023-11-07 07:42:05

dcoun
Member
From: Crete, Greece
Registered: 2020-02-18
Posts: 430

Re: Mustache: Helper can have more than one arguments

danielet79 wrote:

Any news about this bug?

I believe it still exists.

Offline

#8 2023-11-07 08:36:30

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

Re: Mustache: Helper can have more than one arguments

dcoun wrote:

I find I find a small bug:
The first argument in a helper MUST be a variable from Json.
If not, only the first argument exists in the Variant passed to the helper

I am not sure I can find this restriction in TSynMustacheContext.ProcessHelper.
It calls GetVariantFromContext() on all items of the CSV.

Which kind of value are you using?

Offline

#9 2023-11-07 08:40:17

dcoun
Member
From: Crete, Greece
Registered: 2020-02-18
Posts: 430

Re: Mustache: Helper can have more than one arguments

direct number eg. 2 did not work

Offline

#10 2023-11-07 09:37:49

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

Re: Mustache: Helper can have more than one arguments

It should.

Please try to debug why it does not for you.

Offline

#11 2023-11-07 10:31:28

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

Re: Mustache: Helper can have more than one arguments

For example, this works:

{{#Equals Category,"Admin"}}Welcome, Mister Administrator!{{/Equals}}

But this doesn't work:

{{#Equals "Admin",Category}}Welcome, Mister Administrator!{{/Equals}}

I think problem at line 705 in mormot.core.mustache.pas.
In case when value starts from digit, '"', '[' or '{' it is being interpreted as single JSON value.

Offline

#12 2023-11-07 14:39:10

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

Re: Mustache: Helper can have more than one arguments

I just allow {{helper ,"constant1","constant2",123}} Mustache help syntax
to circumvent "constant1" not recognized as CSV.
See https://github.com/synopse/mORMot2/commit/2a39c508

This is far from perfect, but at least it works.
In the future, we may need to have a full rewrite of the helper parameters parsing but it is a bit complex to mix CSV, JSON and Mustache values in the expression.

Offline

Board footer

Powered by FluxBB