#1 2023-04-15 21:28:28

Bsaidus
Member
From: Algeria
Registered: 2018-12-16
Posts: 50

SynMustache and logical conditions.

Hello.
I wonder if there is a possibility in SynMustache to do some controls based on condition to display the wanted value.
Ex:
  I have to render an html page that containe a <Select> tag with 3 <Option>, but I want to make one <option selected > for a given one depending on value in .json.
is it possible to do this on SynMustache.
The Mustache engine comming from Freepascal trunk is buggy

EX: from the fcl-mustache.

// json 
{
  "host":{
    "name"     : "uc1.uccenos.net",
    "ip"       : "123.33.32.1",
    "mask"     : "222.222.222.0",
    "gateway"  : "123.33.23.3" 
  }
}

//template 
// depending on the mask value from json & make the adequate <Option> selected
<br/>
<ul>
  <li>hostnam : {{host.name}}</li>
  <li>ip      : {{host.ip}}</li>
  <li>mask    : {{host.mask}}</li>
  <li>gateway : {{host.gateway}}</li>
 <select name="themask">
    <option value="111.222.333.111" {{[IF(mask='111.222.333.111', 'selected','')]}}> 111.222.333.111 </option>
    <option value="222.222.222.0"   {{[IF(mask='222.222.222.0', 'selected','')]}}> 222.222.222.0   </option>  
    <option value="333.222.333.111" {{[IF(mask='333.222.333.111', 'selected','')]}}> 333.222.333.111 </option>
</select>

Thanks  you.

Offline

#2 2023-04-15 22:34:37

igors233
Member
Registered: 2012-09-10
Posts: 233

Re: SynMustache and logical conditions.

Use:
<option value="111.222.333.111" {{#if host.mask="111.222.333.111"}} selected {{/if}}> 111.222.333.111 </option>
<option value="222.222.222.0" {{#if host.mask="222.222.222.0"}} selected {{/if}}>222.222.222.0</option>

Offline

#3 2023-04-15 23:18:08

Bsaidus
Member
From: Algeria
Registered: 2018-12-16
Posts: 50

Re: SynMustache and logical conditions.

igors233 wrote:

Use:
<option value="111.222.333.111" {{#if host.mask="111.222.333.111"}} selected {{/if}}> 111.222.333.111 </option>
<option value="222.222.222.0" {{#if host.mask="222.222.222.0"}} selected {{/if}}>222.222.222.0</option>

Thanks igor for help, but it do not work for me.

// lazarus code 
procedure TForm1.Button2Click(Sender: TObject);
var
  Template,
  Context,
  Result: RawUTF8;
  mustache: TSynMustache;
begin

  Template := StringToUTF8(mmTemp.Text);            // mmTemp = memo for template
  Context := StringToUTF8(mmJson.Text);               // mmJson = memo for json
  mmRes.Append( UTF8ToString(Context) );            // mmRes = memo for resulting rendring

  mustache := TSynMustache.Parse(Template);

  mmRes.Append('-----------------------------------------------------------');

  mmRes.Append(mustache.RenderJSON(Context));

end;  

Offline

#4 2023-04-16 17:26:53

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

Re: SynMustache and logical conditions.

Try to use the debugger to find out why it did not work.
For instance, put a breakpoint into the "if" helper, i.e. TSynMustache.If_().

You will learn a lot about how it works.

Offline

#5 2023-04-16 21:41:36

Bsaidus
Member
From: Algeria
Registered: 2018-12-16
Posts: 50

Re: SynMustache and logical conditions.

ab wrote:

Try to use the debugger to find out why it did not work.
For instance, put a breakpoint into the "if" helper, i.e. TSynMustache.If_().

You will learn a lot about how it works.

I did, but it do not even enter there.

Offline

#6 2023-04-16 21:48:23

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

Re: SynMustache and logical conditions.

Bsaidus wrote:

Thanks igor for help, but it do not work for me.

You do not register the default helper, so "IF" is not available to you. You have to write it like this:

mustache.RenderJSON(Context, Nil, TSynMustache.HelpersGetStandardList)

All in all, I would not solve this during generation, but calculate it outside and then pass it. How to use Mustache is described in this article with source code.

With best regards
Thomas

Offline

#7 2023-04-16 22:06:27

Bsaidus
Member
From: Algeria
Registered: 2018-12-16
Posts: 50

Re: SynMustache and logical conditions.

tbo wrote:
Bsaidus wrote:

Thanks igor for help, but it do not work for me.

You do not register the default helper, so "IF" is not available to you. You have to write it like this:

mustache.RenderJSON(Context, Nil, TSynMustache.HelpersGetStandardList)

All in all, I would not solve this during generation, but calculate it outside and then pass it. How to use Mustache is described in this article with source code.

With best regards
Thomas

Thank you Thomas, I'll read.

Offline

#8 2023-05-18 02:14:31

EloisePalmer
Member
From: mexico
Registered: 2023-05-13
Posts: 1
Website

Re: SynMustache and logical conditions.

I was searching and came across the line you shared here. Thank you so much, it really helped me a lot


Canva Pro Modilimitado

Offline

#9 2023-07-03 13:49:53

ShelleyBarton
Member
Registered: 2023-06-14
Posts: 1

Re: SynMustache and logical conditions.

Thanks Thomas, I have read and applied them, it really works well

Offline

Board footer

Powered by FluxBB