Search code examples
javascriptjsrender

JSRender pass two "datasets" to template


I have this working template:

<script id="UpdateTemplate" type="text/x-jsrender">

<div class="ms-PanelPoultry">
<button class="ms-Button" id="*****" style="visibility: hidden";>
<span class="ms-Button-label">Open Panel</span> 
</button>
<div class="ms-Panel ****">
<div class="ms-Panel-contentInner">
  <p class="ms-Panel-headerText"></p>
  <div class="ms-Panel-content">
    <span class="ms-font-m">
        <span style="color:#006; font-size:large">***</span>
        <hr>
        <form id="*****">
        <table width="100%" border="0">

                {{for}}
                    {{if (#index) % 3 === 0 }}
                     </tr>
                     <tr>
                     <td>
                        <div class="form-group">
                            <label for={{>name}}>{{>label}}</label>
                            <input type={{>type}} class="form-control" id={{>name}}>
                        </div>
                    </td>

                    {{else}}
                        {{if #index === 0 }}
                            <tr>
                                <td>
                                    <div class="form-group">
                                        <label for={{>name}}>{{>label}}</label>
                                        <input type={{>type}} class="form-control" id={{>name}}>
                                    </div>
                                </td>
                        {{else}}
                            <td style="padding-left:15px;">
                                <div class="form-group">
                                    <label for={{>name}}>{{>label}}</label>
                                    <input type={{>type}} class="form-control" id={{>name}}>
                                </div>
                            </td>
                        {{/if}}
                    {{/if}} 
                {{/for}} 
            </table>
            <hr>
            <table>
            <tr>
                <td>
                 <button type="submit" id="EditProductiebedrijfButton" class="btn btn-primary">Submit</button>
                 <button class="btn btn-secondary" type="Cancel" onClick="panelInstance.dismiss();">Cancel</button>
               </td>
            </tr>
        </table>            
    </form>
    </span> 
  </div>
</div>
</div>
</div>
</script>

It creates an form in an ms-panel with this data:

var Updatefields = [
        { name: "field1", type: "text", label: "blabla" },
        { name: "field2", type: "text", label: "bla" },
    { name: "field3", type: "date", label: "blablaaa" }
   ];

This is all working fine and rendering my form. But I want to pass some extra data to the "header of the template". Where the "*****" are now. For example the "form id".

How can I achieve that?

Also I would like to use a "prefix" for all my "name" values. For example name is "field3" as id for the input field I would like to have "field3Update"

I tried to do some string concat but that failed sofar.

Edit: Last question was very simple. Turned out to do this: id={{>name}}Update


Solution

  • Either pass in additional data as data, along with the fields array:

    $("#xxx").render({
      fields: fields,
      other: otherData
    });
    

    Where otherData is

    {
      whatever: ...,
      ...
    }
    

    and then write

    <form id="{{>other.whatever...}}...">
      <table...>
        {{for fields}}
    

    or pass in additional data as helpers (http://www.jsviews.com/#tmplrender@helpers):

    $("#xxx").render(fields, otherData, true);
    

    and then write

    <form id="{{>~whatever...}}...">
      <table...>
        {{for}}