Skip to content
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

Dynamic variables in key names #40

Closed
wants to merge 6 commits into from

Conversation

rkaw92
Copy link
Contributor

@rkaw92 rkaw92 commented Jul 9, 2019

This change adds support for resolving key names in objects using variables. For example, if the data passed to carbone.render contains:

{
  "language": "de",
  "item": {
    "names": { "en": "Washing machine", de: "Waschmaschine" }
  }
}

Then, in the template, we can use a key from item.names that depends on the passed language by using a Carbone variable (also called "alias" in the Designer documentation) with a new de-referencing syntax:

{#lang = d.language}
{d.item.names.$$lang}

Under the hood, this key reference translated to an IIFE that evaluates _root.d.language or returns an empty string on error (like when some object along the path is null/undefined).

Only references to known variables are replaced with dynamically-computed keys, so this change preserves compatibility for users of key names with $$ in them (they work as usual) - unless you define a variable of the same name.

Square brackets were considered to give developers more familiarity (would have been similar to obj[variable] in JS), but ultimately a "$$" syntax was chosen that requires minimal changes to the parser/renderer logic.

Note that this function only supports plain, parameter-less aliases/variables so far (no array filters), and the variable code (the part after =) must be eval-able.

Basic tests for this renderer feature are provided.

rkaw92 added 6 commits July 9, 2019 15:01
This patch adds support for referring to arbitrary keys in objects using
variables, such as "item.names.$$language". It provides an alternative to
passing arrays of objects for cases when the input data is a flat
key-value map, or when the object is complex and the key to get
data from depends on some other data (currencies, environment, etc.).
This fixes an issue introduced when implementing dynamic keys that would
cause empty/falsy values to appear as non-empty to the XML builder,
which would in turn render superfluous rows and columns even though
no values were present.
This fixes a problem where nulls would break XML data generation.
@dcdamien
Copy link

dcdamien commented Dec 6, 2019

@dgrelaud is there anything that should be done to merge this change?

@dgrelaud
Copy link
Member

dgrelaud commented Dec 6, 2019

is there anything that should be done to merge this change?

Yes, this our top priority next feature.
I think it will be available at the end of december (with other great features! )

The solution provided by rkaw92 is great but it does not work in all cases with nested arrays for example.

Stay tuned!

@steevepay steevepay mentioned this pull request Dec 18, 2019
21 tasks
@dgrelaud
Copy link
Member

The new v2.0 includes this feature now.
Thank you for your help.

@dgrelaud dgrelaud closed this Jun 28, 2020
@Alexanderdunlop
Copy link

Does this work on an array.
For example:

{#lang = d.language}
{d.items[i].$$lang}

image

I am trying this and everything is rendering apart from {d.E.1[i].$$customer}

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants