combyne icon indicating copy to clipboard operation
combyne copied to clipboard

Object properties

Open chikamichi opened this issue 9 years ago • 4 comments

Hi (again),

More or less on the same topic as #61, I wonder whether it would be nice to add support for hash properties. A use-case to illustrate this:

# data
fullname: @user.fullname() # for instance ^^

# template
<span class="greeting">{{ i18n "ui.msg.welcome" {name: fullname} }}</span>

where the "ui.msg.welcome" I18N key would typically be defined as:

# en.coffee
ui:
  msg:
    welcome: "Welcome %{name}"

# fr.coffee
ui:
  msg:
    welcome: "Bienvenue %{name}"

That is, given a fullname property available in the rendering context (data), make it possible to pass it along through an object.

The current parser can't handle that just yet, but thanks to #62, one can workaround this by wrapping the object:

# data
fullname: {fullname: @user.fullname()}

# template
<span class="greeting">{{ i18n "ui.msg.welcome" fullname }}</span>

Between those two options, I don't know which one reads better but, altough I have the feeling that the second option is "fine", the wrapping is cumbersome and somehow less standard than the first option where the object is written in the template.

In the simple cases where there is only one variable to pass around, this wrapping can be automated, but if several variables must be merged in the same object (for instance, {name: fullname, gender: gender, vip: isVIP} to interpolate a complex sentence), then automation is not an option anymore. It is still possible to manually wrap the object beforehand, but being able to write the object within the template may feel more straightforward.

So it basically boils down to template management preferences :hocho:

What do you think?

chikamichi avatar Dec 19 '14 05:12 chikamichi

Have you tried using a filter for this? Seems like it might work better than wrapping your properties as objects in the template data.

tbranyen avatar Dec 19 '14 12:12 tbranyen

I must say I did not yet.

chikamichi avatar Dec 19 '14 12:12 chikamichi

Although whether or not filters will work as arguments is also a good question. They probably don't :-/

tbranyen avatar Dec 19 '14 13:12 tbranyen

At the moment, you'd have to do this in the business layer. In the future (say version 1.0), I'd expect to see parenthesis added to preserve context. So something like this would be possible:

{{ "ui.msg.welcome"|i18n(fullname|withKeyName('name')) }}

tbranyen avatar May 03 '15 04:05 tbranyen