Trunk underline checkbox passing cost issue

I have a very simple problem, but I do not see a solution. Maybe I need to change my mindset. Basically, I am returning the model to a JSON file, but cannot figure out how to convert true to checked . For example, I know that {{-done}} is true or false .

 input type="checkbox" id="chkName" <%= {{-done}} ? "checked" : "" %> 

I see many examples like this, but it does not work for me. This is because I returned true / false.

Any help would be appreciated

+4
source share
2 answers

Your problem is that you are using the wrong type tag for the Underscore.js template, the tag you are using is designed to interpolate the value and its HTML escape.

To explain, there are three types of tags in underscore templates,

  • <%= %> - Interpolate - evaluates these properties and prints it.
  • <%- %> Escape . Calculates data and displays it using HTML.
  • <% %> Rate . Allows you to execute javascript in your template.

In addition, when running JavaScript inside your template, you can use the Print method to print some text (for example, attribute results).

Based on the foregoing, in your case, you need to run the conditional expression in your template in order to print the checked expression, if it is executed, to true. So you can do the following:

 <% if(done) { %> 'checked="checked" <% } %> 

Or you can use the Print method

 <% print(done ? 'checked="checked"' : '') %> 

Edit:

Based on the template you posted, the following template should work

  <script id="item-template" type="text/template"> <input type="checkbox" id="toggle" <% print( done ? 'checked="checked"' : '') %> /> <span class="text">{{-done}}</span> <a id="dele" data-role="button" data-inline="true" data-icon='delete' >Delete</a> </script> 

Here is a link to jsBin using ERB style default delimiters

And here's the second one using mustache style dividers.

+13
source

I did it, but he just always entered the first condition. This is what I have. As you can see, I print Done on the next line, which shows true or false. Your code makes sense, just doesn't want to play ball. Is this related to the script declaration?

  <div id="items"> <script id="item-template" type="text/template"> <input type="checkbox" id="toggle" <% print( done ? '' : checked) %> <span class="text">{{-done}}</span> <a id="dele" data-role="button" data-inline="true" data-icon='delete' >Delete</a> </script> </div> 
0
source

Source: https://habr.com/ru/post/1441394/


All Articles