-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Adding in a way to not explicitly set context #303
base: master
Are you sure you want to change the base?
Conversation
.replace(/\t/g, "\\t"); | ||
|
||
return new Function('it', (function (str) { | ||
return 'return (new Function("{" + ' + "Object.keys(it).join(', ')" + ' + "}", "' + str + '"))(it)'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"{" + ' + "Object.keys(it).join(', ')" + ' + "}"
Here we're getting all the keys in the object and exploding them so that we end up with new Function({foo, bar, baz}, "escapedFunctionBody")
, which is what allows us to use the template in a non-context way. Object.keys
is called when the outer function is called, as part of the process of building the inner function because it's not technically wrapped in "
's.
str
is called when the inner function is executed because it is wrapped in "
's
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return (new Function("{" + ' + "Object.keys(it).join(', ')" + ' + "}", "' + str + '"))(it)
Since we want to perform the explosion at runtime, our outside function returns a self-calling function so that the user doesn't have to do something like doT.template(template)(data)()
; This way when they do doT.template(template)(data)
the inner function created and called at the same time
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Essentially this created a function that created a new function with the definition of automatically spreading out the passed in arguments so that you can have context-less templates. I'm aware that this is a feature in v2, but I thought it might be useful to add to v1, especially since v2 is in beta
@Garethp, having this implementation would cause doT v1 to fail on older browsers which are not es5 compliant
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@elcharitas True, but I think that it should still be backwards compatible with older v1 versions. All of the ES6 syntax exists inside the function that gets built at run-time only if you use the no-context option. Anyone should be able to upgrade to this change without affecting older browsers, it just means that the new feature wouldn't work on older browsers.
Is having a feature that's only available to browsers in the last 5-ish years a problem if it doesn't stop the package from working on older browsers the way it did previously?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wish i had a say on this as you've fully convinced me.
I was of the opinion that new features should just be implemented in v2 but taking a look at v2 again, i see a lot of breakage :(
Also, i think it's noteworthy that v1 hasn't been updated in a while. Hopefully this merge would be accepted as I'd love to see that happen
Since it was a bit of a mindtwist trying to write the code, I thought I'd leave a couple of comments on how it works |
Essentially this created a function that created a new function with the definition of automatically spreading out the passed in arguments so that you can have context-less templates. I'm aware that this is a feature in v2, but I thought it might be useful to add to v1, especially since v2 is in beta