Break up long expressions into several lines

Is there a way of segmenting a long expression across several lines, like this?

- component: oh-icon
  config:
    icon: =(props.prop1 == "1")?whatever1:(
             (props.prop1 == "2")?whatever2:(
               (props.prop1 == "3")?whatever3:whatever5
             )
           )
    color: ...

A workaround is to use state descriptions for this but I specifically look a solution like this

In general you should be able to use the yaml multiline indicators (| or >).

  title: >
    ="this is " + 
    "a really long line"

But I admit, I haven’t tried it with a ternary statement, so I don’t know if the : is going to cause a problem with that.

Also, you might be interested in the the example in the widget objects docs about simplifying long nested expressions:

1 Like

Hi Justin,
thank you very much. Before posting I have tried a lot of variations wih the multiline indicators as well as some others like {}, [], &&, ,… but I precisely did not hit the right solution :slight_smile:
I can confirm that : does not make any problems even with space around them.

And thanks again for the hint about referencing objects in expressions. That one is really great.

One of the problems though is the actual data is stored as JSON behind the scenes. So what you are seeing in MainUI is the JSON converted to YAML and then it gets converted back to JSON for saving. Because of differences in what each can represent, we end up with the lowest common denominator and that means formatting things like this you might be able to do in the YAML will almost certainly get lost when it’s converted to JSON and back again.

It’s the same reason we can’t have comments: JSON doesn’t support them.

Hi Rich,
could you explain the conditions a bit more in detail as to when you see the “almost certain” loss of data?
I have been using this multiline indicator already for stylesheets for months and from now on I’ll use them for complex expressions. So far I had no problems with Main UI (which does a constant conversion between json and yaml)

True, but yaml should be 100% interoperable with JSON and the multi-line syntax is actually how the UI stores long strings such as scripts, so I don’t think that’s a concern here, but I could be wrong.

I know it doesn’t work with comments which are supported in yaml and not supported in json.

I also know that in rules all the newlines get converted to URL encoded lines in the JSON, making the whole script appear on one line in the JSON.

I imagine the same happens in widgets too.

I don’t think I said "almost certain loss of data. Just that there are some minor differences between YAML and JSON which can cause things to not come back the same way they were sent. Goo example, YAML comments on their own line will be lost. Sometimes quotes around values get lost. Stuff like that.