accordproject / template-archive

Smart Legal Contracts & Templating System
https://accordproject.org/projects/cicero/
Apache License 2.0
281 stars 119 forks source link

Allow Conditional Blocks to evaluate conditional expressions. #588

Open martinhalford opened 4 years ago

martinhalford commented 4 years ago

Is your feature request related to a problem? Please describe.

Conditional blocks enable text which depends on a value of a Boolean variable in a model. For example: {{#if forceMajeure}}This is a force majeure{{/if}}

However, this requires a specific boolean variable to be used as input to the #if condition, which is fine if the model has a boolean variable available. It would be useful if the #if could evaluate a condition expression.

Describe the solution you'd like Can we please have syntax like... {{#if conditionExpr}} some value {{else}} some other value {{/if}}

For example: {{#if numZombiesAttacking > 0}}This is a force majeure due to zombie apocalypse! {{else}} All good. Zombie free today.{{/if}}

dselman commented 4 years ago

The challenge with this is when parsing, as the presence of "zombie apocalypse" is now posting a constraint on the value of a variable, rather than defining the value unambiguously.

The ability to use natural language for persistence is a key capability imo and I'd be very, very reluctant to lose it.

dselman commented 4 years ago

Also, note that Formulas can return text, which is inserted into the clause. I think there's already an issue to support returning markdown.

martinhalford commented 4 years ago

HI @dselman, Not sure I follow. If numZombiesAttacking is an integer, then the expression numZombiesAttacking > 0 will evaluate to a boolean. Isn't that just an extension of using a boolean variable directly?

martinhalford commented 4 years ago

Also, note that Formulas can return text, which is inserted into the clause. I think there's already an issue to support returning markdown.

The challenge with formulas is that we can't apply markdown formatting inside them.

dselman commented 4 years ago

With the current Boolean variable binding when the variable is true, the text is present - AND vice versa. If you introduce complex expressions in bindings then we will only know that x > 0 when the text is present, but won't know the value of x, so we can no longer build a JSON representation of that field. It works for Formulas because they aren't parsed and the result of the Formula is not represented in the template model...

martinhalford commented 4 years ago

OK. I can see that and appreciate why you'd not want to break the bi-directional flow. It could be caveat emptor, a bit like how we do for "unquoteVariables"?

dselman commented 4 years ago

Yes, I think supporting Formulas that returns markdown is the right way to solve this. E.g. https://github.com/accordproject/markdown-transform/issues/274

This sends a clear message to users that this text doesn't roundtrip and that they are "punching a hole" in their template to insert dynamic content, which will be ignored during parsing.