Search code examples
javascriptjsrenderjsviews

How can I use javascript variable in Jsrender template


I am new to JsRender, Please help me with below issue...

I am stuck with one silly issue, I have spent lots of time but can't find a relevant article for help.

Inside my I have made a javascript variable (s_name) and want to use it in my for loop as name for my radio button. But I am unable to do so. Please find below the code.. Need your help guys..

{{for Product}}

            <div class="leftOptInner1 my_left_{{:Id}}" id="my_left_inneropt">

                <div class="leftIconInner1 my_{{:Id}}" name="{{:Name}}" id="{{:Id}}" >
                    <!-- <div class="leftIcon1 designer_left_icon "> -->
                    <div class="designer_left_icon ">

                        <img src="{{:ImageSource}}" width="100%" height="100%">
                    </div>
                    <p >{{:Name}}</p>
                    <span class="name" id="{{:Id}}"></span>
                    {{:"<scr" + "ipt type=&quot;text/javascript&quot;>"}}
                        var s_name = "{{:Name}}";
                        alert(s_name); 
                       // s_name.render();

                    {{:"</scr" + "ipt>"}}

                </div>

                    <div class="rightIconOptOuter" style="width: 100% !important; float: left;">
                  {{for Options}}

                    {{for Features}}

                    <div class="rightIconOptInnerBox">

                        <div class="tick">
                              <div class="single_set_designer col-md-2">

                      <div class="fav-heart1 fav-heart_deisgner" id="{{:Id}}">
                      <span class="name" id="{{:Id}}"></span>
                          <div class="checkbox" id="{{:Id}}">
                                      <label>
                                        <input class="myChk designer_chk" type="checkbox" id="{{:Id}}"  data-textronic_id="{{:Id}}" data-textronic_name="{{:Name}}" name="check[]" />
                                      </label>
                                    </div>
                        </div>


                      <div class="element_wrp text-center">
                        <img src="{{:ImageSource}}">
                        <p>{{:Name}}</p>
                        <div class="price_hide">
                        <a class="show_price" id="{{:Id}}"><i class="fa fa-inr" aria-hidden="true"></i> Add Cost</a>
                          <input class="cost_associated form-control text-center showdesign{{:Id}}" type="text" value="" name="cost" style="display: none;">
                      </div>


                       <input type="radio" name="WE_WANT_s_name_HERE" id="{{:Id}}" value="{{:Name}}">
                      </div>
                    </div>
                        </div>
                    </div>




                {{/for}}


                  {{/for}} 
                       {{/for}} //products

Solution

  • Helpers

    You can pass the s_name variable as a helper.

    See http://www.jsviews.com/#helpers

    For example:

    var helpers = {
        s_name_Hlpr: s_name,
      ...};
    
    myTmpl.render(data, helpers);
    

    Then write:

    <input type="radio" name="{{:s_name_Hlpr}}" ... />
    

    Allow Code

    Alternatively (but better avoided) you can allow code access in your template:

    See http://www.jsviews.com/#allowcodetag

    First set:

    $.views.settings.allowCode(true);
    

    Then write:

    <input type="radio" name="{{*:s_name}}" ... />
    

    Contextual parameters

    That said, it looks like you're are trying to initialize your s_name from data.

    var s_name = "{{:Name}}";
    

    You may really be wanting to pass in the Name from the product, which you can do like this:

    {{for Product}}
      ...
      {{for Options ~prod_name=Name}}
        ...
        {{for Features}}
          ...
          <input type="radio" name="{{:~prod_name}}" .../>
          ...
        {{/for}} 
        ...
      {{/for}} 
      ...
    {{/for}} //products
    

    (See http://www.jsviews.com/#contextualparams)