Display Handlebars.js context in a template

冷暖自知 提交于 2019-12-23 09:10:52

问题


Is there a variable passed into every handlebar.js template that contains all the context content that is accessible by the template?

e.g. I'm creating a template, but I don't know all the context content accessible by the template. I want to be able to type into the template {{ debug }} and handlebars.js will spit out all the context content into the HTML


回答1:


You can use the following code to iterate through this object:

{{#each this}}
  {{@key}}: {{this}}
{{/each}}

or a similar piece of code iterating through @root object:

{{#each @root}}
  {{@key}}: {{this}}
{{/each}}



回答2:


Handlebars has built-in helper log.

You just need to set logging level to DEBUG.

Handlebars.logger.level = 0;

Then use helper:

{{log this}}

EDIT: Sorry, this will not write context to HTML, helper uses console.log. For outputting to HTML you need to write custom helper that will use for example JSON.stringify.




回答3:


Though this question is somewhat old, someone might find this usefull. You can just dump the handlebars current context into plain text with;

{{{.}}}


来源:https://stackoverflow.com/questions/11231633/display-handlebars-js-context-in-a-template

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!