# Conditional Rendering
# 1. wx:if
In the framework, use wx:if="" to determine whether to render the code block:
<view wx:if="{{condition}}"> True </view>
You can also use wx:elif and wx:else to add an else block:
<view wx:if="{{length > 5}}"> 1 </view>
<view wx:elif="{{length > 2}}"> 2 </view>
<view wx:else> 3 </view>
# 2. block wx:if
Since wx:if is a control attribute, it needs to be added to a tag. If you need to conditionally render multiple component tags at once, you can wrap them with a <block/> tag and apply the wx:if control attribute on it.
<block wx:if="{{true}}">
<view> view1 </view>
<view> view2 </view>
</block>
Note
<block/> is not a component; it is merely a wrapper element that does not render anything on the page and only accepts control attributes.
# 3. wx:if vs hidden
Since templates within wx:if may contain data bindings, when the condition value of wx:if changes, the framework performs a partial rendering process to ensure the conditional block is destroyed or re-rendered upon switching.
Additionally, wx:if is lazy. If the initial rendering condition is false, the framework does nothing. It only starts partial rendering when the condition becomes true for the first time.
In contrast, hidden is much simpler. The component is always rendered, and hidden merely controls its visibility.
Generally, wx:if has higher switching costs, while hidden has higher initial rendering costs. Therefore, hidden is preferable for scenarios requiring frequent toggling, whereas wx:if is better if the condition is unlikely to change at runtime.