#1 2014-06-03 12:43:46

d7371
Member
Registered: 2014-06-03
Posts: 1

SynMustache and filters

Hi all,
some days ago I talked with Arnaud about the possiblity to add into SynMustache some, so called, "filters" just like the angualrjs ones or the handlebarsjs ones. HEre the proposal.

"I'm wondering if in SynMustache does exists the possibility to call a formatting function inside the template.
This is because if I need presentation logic implemented in Delphi (because mustache is logicless) I have to prepare data before to send it to the renders.

Something like:

in Delphi
  Mustache.AddTemplateFunction('dollars', function (Values: TArray<TValue>): String   
                                          begin 
                                            Result := Format(Values[1].AsString, [Values[0].AsDouble]);
                                          end);

and in the template
  
 {{mydata|dollars:'$ ###0.00'}}

N.B. In the template I've used the angularjs filters syntax, but just to be clear (https://docs.angularjs.org/api/ng/filter/filter)

I know there isn't such concept in Mustache, but this is a nice addition to the standard IMHO, and doesn't break it."

Arnaud suggest that handlebars syntax could be also a viable solution.
Both angularjs and handlebars syntax are nice, coherent and powerful. As my personal favourite, I'd like to have angularjs syntax, but probably makes sense to implement handlebars syntax. So my vote goes for Handlebars syntax.

Any other POV?

Last edited by ab (2014-06-04 07:41:14)

Offline

#2 2014-06-04 07:46:06

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

Re: SynMustache and filters

My only concern is that full Handlebars syntax would be difficult to implement in Delphi, since it is more tied to JavaScript, AFAIR.

Filters are convenient, and perhaps easier to implement.

But is client-side templating really needed for us?
If we want the same exact templates on both client & server, then SynMustache + mustache.js should be good.
Then we may be able to add any custom syntax on the server side. If we can be somewhat compatible with Handlebars, without breaking Mustach syntax, it could be good enough.

Could be something similar to https://github.com/jehugaleahsa/mustache-sharp#readme

What do you think?

Offline

#3 2014-08-04 09:05:49

AntonE
Member
Registered: 2012-02-03
Posts: 74

Re: SynMustache and filters

I was thinking to use Translate to generate custom values.
e.g. in template have : {{"MyVar}}
then

procedure MyTranslate(var English:String);
begin;
 if SameText(English,'MyVar')
    then begin;
             English:=CalcIt;
           end;

end;

?

Then it can even insert more mustache code and run a second parse/render cycle to render that.

Regards

Last edited by AntonE (2014-08-04 09:27:08)

Offline

#4 2018-02-14 21:37:15

otomazeli
Member
Registered: 2018-02-14
Posts: 1

Re: SynMustache and filters

Hi I'm just writing to report a bug related to data format
if you try to format your template using this format
<td>{{DateFmt removaldate, "mmm dd, yyyy"}}</td>
the output won't be rendered because of the comma on the date format.

Thanks

Offline

Board footer

Powered by FluxBB