问题
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