I might get fair bit of criticism for writing this, but here it goes
I remember the days when I used to go through a lot of expressions in Visualforce Pages, and it is REALLY hard to read/debug such inline expressions.
I feel it is quite helpful as LWC forces us to separate logic from HTML by using getters:
get someCondition() {
return this.someVar == 5;
}
It also helps the framework to get rid of parsing completely different syntax, implementing/maintaining code to keep it working.
By utilizing the getters we get to use Javascript to apply any sort of operations before evaluating the condition output.
BUT
There could be situations/implementations where you might end up writing a lot of getters to get the work done. That's where you'll want to write inline expressions as it adds the ease while implementing.
below example illustrates how I implemented this with also utilizing JS, but this does involve some performance kicks as well:
<c-lwc-if condition="(this.a == this.b) && (this.c == 6 || this.b == 'easypeasy')" scope={scope}>
<div slot="if">
Render some HTML
</div>
<div slot="else">
Render some other HTML
</div>
</c-lwc-if>
The condition property is actually JavaScript code but is limited to access the passed scope.
I don't really use the one mentioned above & lean towards the getters for implementing expressions-like use cases
One of the major use cases involved rendering different components based on a condition, which would require code something like:
<template>
<template if:true={condition1}>
component 1
</template>
<template if:true={condition2}>
component 2
</template>
<template if:true={condition3}>
component 3
</template>
... and so on
</template>
Which within Aura and LWC would be easily implemented using expressions and one would not need to write multiple boolean properties/getters to get the job done.
You could also use multiple component templates to implement a solution for this, but wouldn't that require a new implementation for each time you had such requirement.
So I took some inspiration from classic switch-case control flow statements in varous languages and implemented something :
markup:
<c-lwc-switch expression="this.a" scope={scope}>
<c-lwc-case value="4">
<div>Print Something</div>
</c-lwc-case>
<c-lwc-case value="5">
<div>Print Something else</div>
</c-lwc-case>
<c-lwc-case value="'test'">
<div>Print test</div>
</c-lwc-case>
<c-lwc-case default>
<div>Print Default</div>
</c-lwc-case>
</c-lwc-switch>
js:
get scope(){
return {
a : this.b
}
}
Above solution is reusable and solves the problem declarative and helps us with lack of expressions without actually using expressions.
The switch-case reusable solution helps implement the mentioned use case quickly and reduces my efforts, though it still might require some security checks.
here's the implementation:
https://github.com/prashantk0001/lwc-if-expressions
https://github.com/prashantk0001/lwc-switch-expressions