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
Returning template variable names #538
Comments
If I'm understanding you correctly you want something like this var myTemplate = "{{foo}} is {{bar}}";
var variableNames = Mustache.VariableNames(myTemplate) // ['foo', 'bar'] That's assuming Here's some code to get you started: var results = Mustache.parse('{{foo}} is {{bar}}')
.filter(function(v) { return v[0] === 'name' })
.map(function(v) { return v[1]; });
console.log(results) // ["foo", "bar"] |
Note that while it's possible to implement a naive version of this, there is no way to unambiguously extract all tag names, because things like this: {{# foo }}
* {{ bar }}
{{/ foo }} … could mean either |
@bobthecow is entirely right in this circumstance. The example i gave would only pull out all the identifier nodes, it would also remove all structure effectively flattening the tree. |
@Romanx @bobthecow thanks for your help! |
No problem. Good luck :) |
I had a similar request, but needed to traverse the tree to find all variable names. Thought I'd share the solution I used if anyone needs a reference.
|
@nicluo does this also suffer from the ambiguity problem? |
Yep. It's inherent in the language spec. |
The ambiguity problem is interesting, the docs mention that it would try to find the value in this context and then search the parent's context if no values are found. A bit of investigation got me into this:
The example is very contrived, and there are many tricks used to stay concise, but it should show how difficult I find it is to reinvent the wheel. Cheers |
@Immortalin Can you elaborate / provide a better definition of the problem? What happens with properties nested in objects? Can you provide a more complete input and output? |
@dasilvacontin the project that requires this feature is currently on hiatus so I am going to close this for the time being |
Simple solution to get just the top level: Mustache.parse(template).filter(function(v) { return v[0] === 'name' || v[0] === '#' || v[0] === '&' }).map(function(v) { return v[1]; }); |
Hi! This is a rather weird request but is there anyway to return the list of of input variables as an array of string or as an object? For a code generation function in my code, I need to map a series of inputs into
a mustache-templated string. The input variables are named exactly the same as the templating variables and will shadow/map to them perfectly and because this needs to be done automatically, hence the strange request. Here's an example of what I mean:
Assuming a function Mustache.getTemplateVariablesListAsObject
I have been thinking over this for more than an hour and I still can't find a better way to do this. Would be really grateful if you can offer some alternative methods etc.
The text was updated successfully, but these errors were encountered: