Allow with
helper accept named arguments
#1
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Rendered original; copy below for convenience.
Summary
Allow the
with
helper accept named arguments (key/value pairs similar to the hash syntax):Motivation
When there's a need to define multiple local variables in a template, multiple positional arguments are used:
The problem with this code is that it's difficult to read and edit. In order to understand which which value belongs to which variable, you have to count both names and values.
The common workaround is to pass a hash instead:
Much better, but now we have to reference every variable via the
locals.
prefix which is tedious. Some developers prefer to reduce it to one character, e. g.l.
, which does not contribute to code clarity.And when you have nested
with
s, you have to come up with unique hash names. "Was this property I need inlocals2
orlocals3
? I'll have to scroll up to find out."That said, **passing a hash to
with
is such a common trick in the Ember that we should consider making it official: allow passing named arguments to thewith
helper while removing all the crust.This RFC is also intended as a lightweight alternative to
let
it be: bind names to values in templates that stays true to the Ember way and doesn't violate the declarative nature of templates.Detailed design
The
with
helper should accept named (non-positional, hash-like arguments).In order to avoid ambiguity, the compiler should forbid mixing positional and named arguments together.
When at least one named argument is provided, the
as |foo|
part should be left out.Here's the initial example again:
How We Teach This
The terminology is already established: named arguments (named parameters). We can also refer to this technique as "hash-like syntax".
Publishing a blog post and updating the guides should be pretty straightforward. This proposal doesn't change anything in Ember, just makes a small addition to the Handlebars syntax.
Drawbacks
The only caveat is that variables can now be introduced into the template scope without the
as |foo|
construct.This shouldn't be a big deal as the whole point of the
with
block helper is to introduce new variables, thus code understandability does not suffer.Alternatives
1. Positional arguments
Bulky, difficult to read and update as it forces you to count both names and values, and is prone to introducing errors when updating.
2. Nested
with
sThis makes variable names and values appear together, but at ridiculous price.
3. RFC emberjs#200
let
it be: bind names to values in templatesThat RFC proposal raised concern in discussion.