-
Notifications
You must be signed in to change notification settings - Fork 223
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
Support for sub-expressions #48
Comments
This should be possible. Looks like a bug |
Hm, I ran the below test case without any problems. Can you compare? One thing I noticed is that your code snippet doesn't include the name of the object you're each-ing. string source = "{{#each people}}{{myHelper @index name}}{{/each}}";
Handlebars.RegisterHelper("myHelper", (output, context, arguments) =>
{
output.Write("{0}. {1} ", arguments[0], arguments[1]);
});
var template = Handlebars.Compile(source);
var data = new {
people = new [] {
new {
name = "Bill"
},
new {
name = "Mary"
}
}
};
var result = template(data);
Assert.AreEqual("0. Bill 1. Mary ", result); |
Sorry I omitted some of the code to be brief. It was actually in a sub expression I was trying to use it in. {{#each InsuranceItems}} InsuranceItems is an IList of custom objects and mathHelper is just a custom helper that takes 3 arguments to execute a math function (in this case mod). IfCond is just an enhanced IF that can take a condition and test value. |
The parens are causing the issue. The parser does not understand those characters, and the compiler doesn't support sub-expressions generally. Sent from my iPhone
|
Ah ok, is sub expressions something on the table to implement in the future? |
My desire is to support those in v2, but there's no timeframe for that yet. If someone took on that feature it'd get us a lot closer sooner. |
Support for sub-expressions is desired for v2. See the Subexpressions section on the handlebarsjs documentation: http://handlebarsjs.com/expressions.html |
This will require 2 main changes:
Open to suggestions in how to handle the return value! |
I wanted to add that I have had a very similar experience that I just referenced in a pull request for another project. (https://github.com/bvcms/bvcms/pull/81) I have written several helpers including: Compare in the form of {{Compare lval oper rval}} Math in form of {{Math lval oper rval "fmt?"}} So when I added the following line to the template, I expected the Math sub-expression to be calculated first and the result returned (from writer.Write(...) statement) and used as an input in the Compare Helper.
returns an error...while
returns "True" Similarly If I used the Compare helper for a logical operation (AND)
returns "True" ... while
returns an error. Unfortunately, I don't have a solution... so just adding my name to those interested. |
Kind of an interesting approach that doesn't break the existing contracts to try to do a type coercion on the value written to the stream out of a helper. |
@hkouns @breenyoung basic sub-expressions are now supported in 1.5.0. I appreciate any help you can provide to give it a try in your projects and feedback here. Usage is as @hkouns described:
In other words, if a helper is put inside a sub-expression (parentheticals), the output written to the buffer will be captured independently and passed as an argument into the outer helper expression.
The inner |
Test for SubExpressions are failing under Visual Studio 2015. Could it be an issue with .NET 4.6? This is the StackTrace:
|
Seems to be a difference between mono and Microsoft... fixed and pushed to Nuget 1.5.1 |
Haven't gotten any feedback... going to assume this is good. If you run into any problems, please open a new issue. |
I realize this is a closed issue, apologies if I should open a new one (will do if requested). I have a helper "ResourceLookup" used to look up a resource string. But I need to build the name of the resource string from my object tree. What syntax would I use? In this example I need to get myObj.Type formatted as an argument to the TypeName_ resource string, but I have to have the quotes around TypeName or it doesn't pass it as a string to my helper.
What I get right now is a string that directly contains "TypeName_(myObj.Type)" Any help is appreciated, thanks. |
Please open a new issue so we can keep the discussion focused. When you do that, can you please include the model you are using in this example? |
Thank you @rexm for adding the Sub-Expression functionality. I just started using it and it works great! |
Well, you could have two HandlebarsHelper delegates:
This would not introduce any breaking changes. |
If you continue the rest of the thread, you'll see we came to a solution and implemented it in 1.5 |
I have a {{#each}} loop and within each iteration I have a helper being called I'd like to pass the @index property to. Is it possible? Template compilation fails when I try to pass it into a helper (without the curly braces:
{{#each }}
{{myHelper @index arg2}}
{{/each}}
The text was updated successfully, but these errors were encountered: