How to use variable sent from modelmap in js render iteration loop

457 views Asked by At

I am sending a variable in a modelmap from java controller to a view like this :

@RequestMapping(value = "/someAction", method = RequestMethod.GET)
public String search(ModelMap model) {
  model.addAttribute("myVariable",true);
  return "myView"; //returns the view 
}

Now i have this view file "myView.html" where i've used jsrender and that has a data and i iterate it like given below.Also i need to use the "myVariable" inside that iteration like this :

<script id="iterData" type="text/x-jsrender">
{^{for #data}}
    {{if Name != '-'}}
            {{if Age =='20'}}
               {{if myVariable??}} //This part doesnt work.I wonder how to use it here

                    //Some codes                     

               {{/if}}

            {{/if}}
    {{/if}}
{{/for}}
</script>

Can anyone help me how i could use the "myvariable" inside the jsrender iteration??

1

There are 1 answers

0
BorisMoore On

I'm not sure about your use of {{for #data}} - hence my question in a comment.

But one way of passing in variables to nested template contexts is as by passing in a template variable from the outer context to the inner contexts:

For example, if myVariable is a property on any outer context, you can write:

{{!-- myVariable and someArray are properties on the data at this level--}}

{{for someArray ~myVar=myVariable}} 
  {{if Name != '-'}}
    {{if Age =='20'}}
      {{if ~myVar}}
         ...                     
      {{/if}}
    {{/if}}
  {{/if}}
{{/for}}