This page is about version 1.1.6 of the app, which is not the most recent version. The latest stable version is 2.8.2.
The condition layout app allows to conditionally render a block given certain conditions.
Configuration
Step 1 - Adding the Condition Layout app to your theme's dependencies
In your theme's manifest.json
, add the Product Price app as a dependency:
_10"dependencies": {_10 "vtex.condition-layout": "1.x"_10}
Step 2 - Adding the Condition Layout's blocks to your theme's templates
The condition-layout
app exports two kinds of block: condition-layout.{context}
and condition.{context}
.
condition-layout.{context}
- These blocks are responsible for holding your condition blocks and providing the appropriate context.condition.{context}
- These blocks hold the condition logic and children to be displayed given the condition resolves to true.condition.else
- This block can be used to render some content if nocondition
block was matched inside acondition.layout
. The usage of this block is optional. If no condition is matched and there's nocondition.else
declared, nothing will be shown.
condition-layout
Each variant of a condition-layout
is responsible for providing the values to be used in comparisons by its subsequent condition
blocks.
You should never use condition-layout
directly. Make sure to always use a context variant, such as condition-layout.product
.
condition-layout.product
The condition-layout.product
block provides the subjects below to use in condition.{context}
blocks.
Subject | Type | Description |
---|---|---|
productId | value | Id of the current product. |
categoryId | value | Id of the current category. |
brandId | value | Id of the current brand. |
selectedItemId | value | Id of the current selected SKU |
productClusters | array | List of product clusters. |
categoryTree | array | List of categories. |
condition.{context}
Prop name | Type | Description |
---|---|---|
conditions | ConditionArray | A list of conditions to be resolved |
match | MatchType | The condition list match type |
children | ReactNode | The children component(s) to be rendered when the condition matches. |
Types
ConditionArray
Prop name | Type | Description |
---|---|---|
subject | string | Subject ID to get its value. |
verb | ConditionVerb | The condition operator. |
object | string | The value to be compared with the subject's value. |
ConditionVerb
For subjects of type value
, there are two verbs available:
Type | Description |
---|---|
is | Checks for equality (default for value subjects). |
is-not | Checks for inequality. |
For subjects of type array
, there are two verbs available:
Type | Description |
---|---|
contains | Checks if the subject's value contains the defined object (default for array subjects). |
does-not-contain | Checks if the subject's value does NOT contain the defined object . |
MatchType
Type | Description |
---|---|
all | Resolves a list of conditions to true only if all conditions match (default). |
any | Resolves a list of conditions to true if any of the conditions match. |
none | Resolves a list of conditions to true if NONE condition match. |
Example
Checking for a specific product ID
_39{_39 "store.product": {_39 "children": ["condition-layout.product"]_39 },_39 "condition-layout.product": {_39 "children": [_39 "condition#custom-pdp-12",_39 "condition#custom-pdp-20",_39 "condition.else"_39 ]_39 },_39 "condition#custom-pdp-12": {_39 "props": {_39 "conditions": [_39 {_39 "subject": "productId",_39 "verb": "is",_39 "object": "12"_39 },_39 ]_39 },_39 "children": ["flex-layout.row#custom-pdp-layout-12"]_39 },_39 "condition#custom-pdp-20": {_39 "props": {_39 "conditions": [_39 {_39 "subject": "productId",_39 "verb": "is",_39 "object": "20"_39 },_39 ]_39 },_39 "children": ["flex-layout.row#custom-pdp-layout-20"]_39 },_39 _39 "condition.else": {_39 "children": ["flex-layout.row#default"]_39 }