You are not logged in.
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
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.
With best regards
Thomas
Offline
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
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
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
Any news about this bug?
Offline
Any news about this bug?
I believe it still exists.
Offline
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
direct number eg. 2 did not work
Offline
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
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