简体   繁体   English

如何调试Mustache js模板?

[英]How to debug Mustache js template?

I am using mustache.js to render client . 我正在使用mustache.js来呈现client。 I defined tempalte script and passing model object(array). 我定义了临时脚本并传递了模型对象(数组)。 Sometimes , I am not seeing the object values in UI . 有时,我没有在UI中看到对象值。 How to debug this . 如何调试这个。

I am iterating the "modules" and creating a table row . 我正在迭代“模块”并创建一个表行。 There are some cases where the GUI becomes empty but the model is actually having data. 在某些情况下,GUI会变空,但模型实际上具有数据。 In this cases , I want to debug here . 在这种情况下,我想在这里调试。 How to debug this template. 如何调试此模板。

<script id="SomeTemplate" type="x-tmpl-mustache">
   {{#modules}}
                    <tr>
                        <td class="test">{{Name}}</td>
                        <td class="test">{{label}}</td>
                        <td class="{{XClass}}">{{Voltage}}</td>
                        <td class="{{YClass}}">{{Current}}</td>
                        <td class="{{ZClass}}">{{power}}</td>
                    </tr>
   {{/modules}}
</script>

Thanks. 谢谢。

The template you provide is quite straightforward, there is nothing obviously wrong with it. 您提供的模板非常简单,显然没有错。 You also state that it works for some cases, which indicates that the template itself is not the problem. 您还指出它在某些情况下有效,这表明模板本身不是问题。

From the info you provide, there is not much we can do to debug except to test that the template actually works with Mustache.js by rendering some dummy data that fit the template: 根据您提供的信息,除了通过渲染一些适合模板的虚拟数据来测试模板是否与Mustache.js一起实际工作以外,我们无法进行其他调试:

 var template = document.getElementById('SomeTemplate').innerHTML; console.log(Mustache.render(template, { modules: [ { 'Name': 'someName', 'label': 'someLabel', 'XClass': 'someXClass', 'Voltage': 'someVoltage', 'YClass': 'someYClass', 'Current': 'someCurrent', 'ZClass': 'someZClass', 'power': 'somePower' } ] })); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/mustache.js/2.2.1/mustache.js"></script> <script id="SomeTemplate" type="x-tmpl-mustache"> {{#modules}} <tr> <td class="test">{{Name}}</td> <td class="test">{{label}}</td> <td class="{{XClass}}">{{Voltage}}</td> <td class="{{YClass}}">{{Current}}</td> <td class="{{ZClass}}">{{power}}</td> </tr> {{/modules}} </script> 

This works fine, which tells us that the problem is elsewhere, possibly that some of the data in the actually provided modules array differ from what the template expects. 这很好,它告诉我们问题出在其他地方,可能实际提供的modules数组中的某些数据与模板期望的有所不同。

Thus, to debug further, I would look at some of the actual data from the cases that fail to render as expected and test those data in the console as in the script above. 因此,为了进一步调试,我将查看未能按预期呈现的情况下的一些实际数据,并按照上述脚本在控制台中测试这些数据。

Some possible guesses about what could trigger failure (only speculation, needs to be tested against actually failing cases): 关于可能触发故障的一些可能猜测(仅推测,需要针对实际失败的案例进行测试):

  • The variable names have varying capitalization, label is all lowercase, Current is firstcaps. 变量名称的大小写各不相同, label均为小写, Current为firstcaps。 In Mustache.js , case matters, so unless this is consistent in the provided data, it could cause variables not to render. Mustache.js ,大小写很重要,因此,除非在提供的数据中保持一致,否则可能导致变量无法呈现。
  • Mustache.js may not render variables with falsy values. Mustache.js可能不会使用falsy值来呈现变量。 What is considered truthy or falsy is not always clear, see What is truthy or falsy in Mustache and Handlebars? 被认为是真实还是虚假的东西并不总是很清楚,请参阅《胡子和把手》中的什么是真实或虚假?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM