-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
A way to get the list of variables from template? #1207
Comments
The closest thing to what you're looking for is using the log helper to output the a particular context (set of template variables): E.g. If you know you've got a set of The docs are here: http://handlebarsjs.com/builtin_helpers.html#log |
Are you looking for a way to build something like a JSON-schema from a Handlebars template to validate the input? I don't think that is possible. Since you can create arbitrary block-helpers, you can never be sure what the context inside a helper-block will actually be and how the expected sub-object for this helper will look like. |
You can try to compile a template with your own helperMissing helper to help you collect used variables (you may need to render it with null input) . But, as previous comment said, you may get different results when your templates behaviors against different context. |
Thanks for the answers. Yes, I'm trying to build something like a JSON schema from a template to validate the input. I know it's not possible in general. But if I only use the standard helpers and helpers I've written ( I thought Handlebars might provide a 'standard' (albeit low-level) way to do so. Say, an API for helpers to tell the world what they expect as input, or something like this. Judging by your answers, there is no such way. I'd like this to be considered as a feature request :) |
@dsavenko You can parse the template and scan the AST. That technique is used to preload partials in this requirejs plugin, by searching the AST for partial nodes: There is documentation on the AST structure here: |
@jbboehr thank you, this looks promising! |
Did you find a solution for this ? |
@abumalick, no, I didn't. The AST parsing approach could work. But it was easier for me to just redesign my flow in a way it doesn't require this feature. |
For anyone finding this thread, I used a quick and dirty way to check for contained variables const getHBVars = value => {
const ast = hb.parse(value)
let keys = {}
for (let i in ast.body) {
if (ast.body[i].type === 'MustacheStatement') {
keys[ast.body[i].path.original] = true
}
}
return keys
}
const activeKeys = getHBVars('Hello {{person}}! Today is {{day}}')
console.log(activeKeys)
// --> { person: true, day: true } |
...but a faster way would probably be to use regex on the raw content.
https://gist.github.com/etiennemarais/2597e33168a07a16a5d541db2a991005 |
I created Barhandles a couple of years ago. It's certainly not perfect, but it's better than regex parsing. You simply feed it the Handlebars template, and it will use the AST to find variable references. http://nxt.flotsam.nl/barhandles |
Closing due to inactivity. |
@wspringer You are the man! |
I know this is old, but I was able to achieve this with the gist below and thought it could be some use to others. It's still in development, but I am getting some success from it. If you notice any errors, please leave as a comment. https://gist.github.com/imryanjay/1d91b2c9f768af7c1b1877a51a4a75f0 |
Hi everyone,
Is there a way to get the list of variables used in template? I mean if a template has
{{varName}}
, I'd like to know it. If a template has something likeI'd like to know the template expects a list called
listName
with objects with fieldsfield1
andfield2
. Is there any API for this?Thanks,
Dmitry.
The text was updated successfully, but these errors were encountered: