You are not logged in.
Pages: 1
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
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
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
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
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
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
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
Thanks Thomas, I have read and applied them, it really works well. descargar tonos para celular
Last edited by ShelleyBarton (2024-04-22 12:59:55)
Offline
Pages: 1